Skip to content

Commit

Permalink
Cache RecordIdentifier methods in Class#model_name wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jun 7, 2008
1 parent 782b054 commit fc6385f
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions actionpack/lib/action_controller/record_identifier.rb
@@ -1,3 +1,19 @@
class Class
def model_name
@model_name ||= ModelName.new(name)
end

class ModelName
attr_reader :singular, :plural, :path

def initialize(name)
@singular = name.underscore.tr('/', '_').freeze
@plural = @singular.pluralize.freeze
@path = "#{name.tableize}/#{name.demodulize.underscore}".freeze
end
end
end

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
Expand Down Expand Up @@ -31,18 +47,21 @@ module ActionController
module RecordIdentifier
extend self

JOIN = '_'.freeze
NEW = 'new'.freeze

# Returns plural/singular for a record or class. Example:
#
# partial_path(post) # => "posts/post"
# partial_path(Person) # => "people/person"
# partial_path(Person, "admin/games") # => "admin/people/person"
def partial_path(record_or_class, controller_path = nil)
klass = class_from_record_or_class(record_or_class)
name = model_name_from_record_or_class(record_or_class)

if controller_path && controller_path.include?("/")
"#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
"#{File.dirname(controller_path)}/#{name.path}"
else
"#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
name.path
end
end

Expand All @@ -56,7 +75,8 @@ def partial_path(record_or_class, controller_path = nil)
# 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 * '_'
singular = singular_class_name(record_or_class)
prefix ? "#{prefix}#{JOIN}#{singular}" : singular
end

# The DOM id convention is to use the singular form of an object or class with the id following an underscore.
Expand All @@ -69,29 +89,32 @@ def dom_class(record_or_class, prefix = nil)
#
# dom_id(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 * '_'
if record_id = record.id
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
else
dom_class(record, prefix || NEW)
end
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
model_name_from_record_or_class(record_or_class).plural
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('/', '_')
model_name_from_record_or_class(record_or_class).singular
end

private
def class_from_record_or_class(record_or_class)
record_or_class.is_a?(Class) ? record_or_class : record_or_class.class
def model_name_from_record_or_class(record_or_class)
(record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
end
end
end
end

0 comments on commit fc6385f

Please sign in to comment.