root/trunk/actionpack/lib/action_controller/record_identifier.rb
| Revision 8734, 3.5 kB (checked in by nzkoz, 7 months ago) |
|---|
| Line | |
|---|---|
| 1 | module ActionController |
| 2 | # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or |
| 3 | # Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate |
| 4 | # the view actions to a higher logical level. Example: |
| 5 | # |
| 6 | # # routes |
| 7 | # map.resources :posts |
| 8 | # |
| 9 | # # view |
| 10 | # <% div_for(post) do %> <div id="post_45" class="post"> |
| 11 | # <%= post.body %> What a wonderful world! |
| 12 | # <% end %> </div> |
| 13 | # |
| 14 | # # controller |
| 15 | # def destroy |
| 16 | # post = Post.find(params[:id]) |
| 17 | # post.destroy |
| 18 | # |
| 19 | # respond_to do |format| |
| 20 | # format.html { redirect_to(post) } # Calls polymorphic_url(post) which in turn calls post_url(post) |
| 21 | # format.js do |
| 22 | # # Calls: new Effect.fade('post_45'); |
| 23 | # render(:update) { |page| page[post].visual_effect(:fade) } |
| 24 | # end |
| 25 | # end |
| 26 | # end |
| 27 | # |
| 28 | # As the example above shows, you can stop caring to a large extent what the actual id of the post is. You just know |
| 29 | # that one is being assigned and that the subsequent calls in redirect_to and the RJS expect that same naming |
| 30 | # convention and allows you to write less code if you follow it. |
| 31 | module RecordIdentifier |
| 32 | extend self |
| 33 | |
| 34 | # Returns plural/singular for a record or class. Example: |
| 35 | # |
| 36 | # partial_path(post) # => "posts/post" |
| 37 | # partial_path(Person) # => "people/person" |
| 38 | def partial_path(record_or_class) |
| 39 | klass = class_from_record_or_class(record_or_class) |
| 40 | "#{klass.name.tableize}/#{klass.name.demodulize.underscore}" |
| 41 | end |
| 42 | |
| 43 | # The DOM class convention is to use the singular form of an object or class. Examples: |
| 44 | # |
| 45 | # dom_class(post) # => "post" |
| 46 | # dom_class(Person) # => "person" |
| 47 | # |
| 48 | # If you need to address multiple instances of the same class in the same view, you can prefix the dom_class: |
| 49 | # |
| 50 | # dom_class(post, :edit) # => "edit_post" |
| 51 | # dom_class(Person, :edit) # => "edit_person" |
| 52 | def dom_class(record_or_class, prefix = nil) |
| 53 | [ prefix, singular_class_name(record_or_class) ].compact * '_' |
| 54 | end |
| 55 | |
| 56 | # The DOM id convention is to use the singular form of an object or class with the id following an underscore. |
| 57 | # If no id is found, prefix with "new_" instead. Examples: |
| 58 | # |
| 59 | # dom_id(Post.new(:id => 45)) # => "post_45" |
| 60 | # dom_id(Post.new) # => "new_post" |
| 61 | # |
| 62 | # If you need to address multiple instances of the same class in the same view, you can prefix the dom_id: |
| 63 | # |
| 64 | # dom_id(Post.new(:id => 45), :edit) # => "edit_post_45" |
| 65 | def dom_id(record, prefix = nil) |
| 66 | prefix ||= 'new' unless record.id |
| 67 | [ prefix, singular_class_name(record), record.id ].compact * '_' |
| 68 | end |
| 69 | |
| 70 | # Returns the plural class name of a record or class. Examples: |
| 71 | # |
| 72 | # plural_class_name(post) # => "posts" |
| 73 | # plural_class_name(Highrise::Person) # => "highrise_people" |
| 74 | def plural_class_name(record_or_class) |
| 75 | singular_class_name(record_or_class).pluralize |
| 76 | end |
| 77 | |
| 78 | # Returns the singular class name of a record or class. Examples: |
| 79 | # |
| 80 | # singular_class_name(post) # => "post" |
| 81 | # singular_class_name(Highrise::Person) # => "highrise_person" |
| 82 | def singular_class_name(record_or_class) |
| 83 | class_from_record_or_class(record_or_class).name.underscore.tr('/', '_') |
| 84 | end |
| 85 | |
| 86 | private |
| 87 | def class_from_record_or_class(record_or_class) |
| 88 | record_or_class.is_a?(Class) ? record_or_class : record_or_class.class |
| 89 | end |
| 90 | end |
| 91 | end |
Note: See TracBrowser for help on using the browser.