aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/record_identifier.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-04-30 01:17:56 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-04-30 01:17:56 +0000
commitda257eb81ba1eab76ef2c1a256916193858418d4 (patch)
tree3b472ae6730ce177c6a496da0ea2b15ab988942d /actionpack/lib/action_controller/record_identifier.rb
parent0dc70383097f1212fa7d9b21c57e31cfd4b7204c (diff)
downloadrails-da257eb81ba1eab76ef2c1a256916193858418d4.tar.gz
rails-da257eb81ba1eab76ef2c1a256916193858418d4.tar.bz2
rails-da257eb81ba1eab76ef2c1a256916193858418d4.zip
Added the first part of Simply Helpful to core. The rest is pending a clean integartion of polymorphic urls [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6633 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/record_identifier.rb')
-rw-r--r--actionpack/lib/action_controller/record_identifier.rb91
1 files changed, 91 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb
new file mode 100644
index 0000000000..1247665a8b
--- /dev/null
+++ b/actionpack/lib/action_controller/record_identifier.rb
@@ -0,0 +1,91 @@
+module ActionController
+ # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
+ # Active Resources 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. Example:
+ #
+ # # routes
+ # map.resources :posts
+ #
+ # # view
+ # <% div_for(post) do %> <div id="post_45" class="post">
+ # <%= post.body %> What a wonderful world!
+ # <% end %> </div>
+ #
+ # # controller
+ # def destroy
+ # post = Post.find(params[:id])
+ # post.destroy
+ #
+ # respond_to do |format|
+ # format.html { redirect_to(post) } # Calls polymorphic_url(post) which in turn calls post_url(post)
+ # format.js do
+ # # Calls: new Effect.fade('post_45');
+ # render(:update) { |page| page[post].visual_effect(:fade) }
+ # end
+ # end
+ # end
+ #
+ # As the example above shows, you can stop caring to a large extend what the actual id of the post is. You just know
+ # that one is being assigned and that the subsequent calls in redirect_to and the RJS expect that same naming
+ # convention and allows you to write less code if you follow it.
+ module RecordIdentifier
+ extend self
+
+ # Returns plural/singular for a record or class. Example:
+ #
+ # partial_path(post) # => "posts/post"
+ # partial_path(Person) # => "people/person"
+ def partial_path(record_or_class)
+ klass = class_from_record_or_class(record_or_class)
+ "#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
+ end
+
+ # The DOM class convention is to use the singular form of an object or class. Examples:
+ #
+ # 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"
+ def dom_class(record_or_class, prefix = nil)
+ [ prefix, singular_class_name(record_or_class) ].compact * '_'
+ end
+
+ # The DOM class 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. Examples:
+ #
+ # dom_class(Post.new(:id => 45)) # => "post_45"
+ # dom_class(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_class(Post.new(:id => 45), :edit) # => "edit_post_45"
+ def dom_id(record, prefix = nil)
+ prefix ||= 'new' unless record.id
+ [ prefix, singular_class_name(record), record.id ].compact * '_'
+ end
+
+ # Returns the plural class name of a record or class. Examples:
+ #
+ # plural_class_name(post) # => "posts"
+ # plural_class_name(Highrise::Person) # => "highrise_people"
+ def plural_class_name(record_or_class)
+ singular_class_name(record_or_class).pluralize
+ end
+
+ # Returns the singular class name of a record or class. Examples:
+ #
+ # singular_class_name(post) # => "post"
+ # singular_class_name(Highrise::Person) # => "highrise_person"
+ def singular_class_name(record_or_class)
+ class_from_record_or_class(record_or_class).name.underscore.tr('/', '_')
+ end
+
+ private
+ def class_from_record_or_class(record_or_class)
+ record_or_class.is_a?(Class) ? record_or_class : record_or_class.class
+ end
+ end
+end \ No newline at end of file