123456789_123456789_123456789_123456789_123456789_

Module: ActionView::RecordIdentifier

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
ModelNaming, self
Instance Chain:
self, ModelNaming
Defined in: actionview/lib/action_view/record_identifier.rb

Overview

The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or pretty much any other model type that has an id. These patterns are then used to try elevate the view actions to a higher logical level.

# routes
resources :posts

# view
<%= div_for(post) do %>    <div id="post_45" class="post">
  <%= post.body %>           What a wonderful world!
<% end %>                  </div>

# controller
def update
  post = Post.find(params[:id])
  post.update(params[:post])

  redirect_to(post) # Calls polymorphic_url(post) which in turn calls post_url(post)
end

As the example above shows, you can stop caring to a large extent what the actual id of the post is. You just know that one is being assigned and that the subsequent calls in redirect_to expect that same naming convention and allows you to write less code if you follow it.

Constant Summary

Class Method Summary

ModelNaming - Extended

convert_to_model

Converts the given object to an ::ActiveModel compliant one.

model_name_from_record_or_class

Instance Method Summary

ModelNaming - Included

#convert_to_model

Converts the given object to an ::ActiveModel compliant one.

#model_name_from_record_or_class

Instance Method Details

#dom_class(record_or_class, prefix = nil)

The DOM class convention is to use the singular form of an object or class.

dom_class(post)   # => "post"
dom_class(Person) # => "person"

If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:

dom_class(post, :edit)   # => "edit_post"
dom_class(Person, :edit) # => "edit_person"
[ GitHub ]

  
# File 'actionview/lib/action_view/record_identifier.rb', line 46

def dom_class(record_or_class, prefix = nil)
  singular = model_name_from_record_or_class(record_or_class).param_key
  prefix ? "#{prefix}#{JOIN}#{singular}" : singular
end

#dom_id(record, prefix = nil)

The DOM id convention is to use the singular form of an object or class with the id following an underscore. If no id is found, prefix with “new_” instead.

dom_id(Post.find(45))       # => "post_45"
dom_id(Post.new)            # => "new_post"

If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:

dom_id(Post.find(45), :edit) # => "edit_post_45"
dom_id(Post.new, :custom)    # => "custom_post"
[ GitHub ]

  
# File 'actionview/lib/action_view/record_identifier.rb', line 61

def dom_id(record, prefix = nil)
  if record_id = record_key_for_dom_id(record)
    "#{dom_class(record, prefix)}#{JOIN}#{record_id}"
  else
    dom_class(record, prefix || NEW)
  end
end