# patterns

A list of patterns I learned that come around useful. Incomplete and wildy unorganised.

## Returning early from controller actions

There are certain situations in which you might want to return early from a controller actions, for example, if you want to make sure a user is allowed to see a certain resource. If a user is not allowed to see said resource, you may want to redirect them somewhere else. `redirect_to` does not stop the execution of a function though, so this has to be done manually.\
The most basic way to do that is to use `and return`, however, `validate_access_rights and return` does not read very well.

Robert Pankowecki introduces 4 possible ways to return early in [his article](https://blog.arkency.com/2014/07/4-ways-to-early-return-from-a-rails-controller/), the most elegant of which is the foruth: `extracted_method; return if performed?`.

```ruby
class Controller
  def show
    verify_something; return if performed?
    @instance_var = Model.find(params[:id])
  end

  private

  def verify_something
    if not_valid?
      redirect_to some_path and return
    end
  end
```

**Further Reading**

* [Rails redirect\_to documentation](https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to)
* [preformed? documentation](https://apidock.com/rails/ActionController/Metal/performed%3F)

## Memoization

The basic idea here is to cache results of methods to that these methods do not have to be executed over and over again, yielding the same results.\
Basic memoization can look like this:

```ruby
class Article < ActiveRecord::Base
  def comments
    @comments ||= article.comments
  end
end
```

*Obviously, this example is very constructed and just used to display the syntax*

**Further Reading**

* [4 Simple Memoization Patterns in Ruby (And One Gem)](https://www.justinweiss.com/articles/4-simple-memoization-patterns-in-ruby-and-one-gem/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.christianpoplawski.de/ruby-on-rails/patterns.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
