Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, the pattern on operating on collections and their entries is part of <a href="https://en.wikipedia.org/wiki/Representational_state_transfer" rel="nofollow">REST</a> which is one of the pillars of Rails. What you've identified is essentially what REST calls <strong>collections</strong> and <strong>entries</strong>. In theory, you can apply <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods" rel="nofollow">the most common HTTB verbs</a> on either the collection, or an entry within that collection, where: <strong>GET</strong>; retrieve, <strong>PUT</strong>; replace, <strong>POST</strong>; create and <strong>DELETE</strong>; destroy. Although it's a little unclear what POST would do on a single entry.</p> <p>In practise, and in Rails, however, you usually use those you've identified:</p> <ul> <li>Collections <ul> <li>Create entry (<strong>POST</strong>)</li> <li>View entries (<strong>GET</strong>)</li> </ul></li> <li>Entry <ul> <li>Update entry (<strong>PUT</strong>)</li> <li>Show entry (<strong>GET</strong>)</li> <li>Delete entry (<strong>DELETE</strong>)</li> </ul></li> </ul> <p>These are the RESTful routes Rails adds. However, as you know Rails also boxes two other actions to implement a user page which can provide the necessary data to these endpoints. (<strong>new</strong>, <strong>edit</strong>),</p> <p>It's rare I see people coming down to these definitions. As I see it there's at least two ways to go about this problem if you really want to solve it (see bottom of the post):</p> <p>You could monkey-patch <a href="https://github.com/rails/rails/blob/d42d97d2e3f0bdef2163cc14daa181903e0b198d/actionpack/lib/abstract_controller/callbacks.rb#L33-42" rel="nofollow">"_normalize_callback_options"</a> to get the exact syntax you proposed. This is relatively straight-forward, but if upstream changes anything about the structure, you have to change your patch, thus it's not very sustainable. Therefore I would not recommend that approach, no matter how tasteful the syntastic sugar looks. Of course you could also try to commit this upstream, but it's unlikely to be accepted due to the reasons mentioned below. :-)</p> <p>Instead I'd put your definitions in an initializer: <code>config/initializer/rest.rb</code></p> <pre><code>ENTRY_ACTIONS = [:edit, :new, :show, :delete, :update] COLLECTION_ACTIONS = [:create, :index] </code></pre> <p>Then use them in your filters like:</p> <pre><code>before_filter :find_project, only: ENTRY_ACTIONS </code></pre> <p>For access in your views, add an <code>entry_action?</code> and <code>collection_action?</code> method to your application helper:</p> <pre><code>module ApplicationHelper def entry_action? ENTRY_ACTIONS.include?(controller.action_name.to_sym) end def collection_action? COLLECTION_ACTIONS.include?(controller.action_name.to_sym) end end </code></pre> <p>Then in your views:</p> <pre><code>&lt;% if entry_action? %&gt; # code &lt;% end %&gt; </code></pre> <p>I wouldn't extract this to constants myself, but just write the array directly since it's much more clear in my opinion to read when I come back later. Furthermore, most people would have to think twice every time they encounter these methods which is far from optimal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload