From 0ccd0b569ad4df7acc37a14531606ba6f74c69f4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:02:22 -0700 Subject: Fix doc typo. Move extend self so it's more immediately obvious. Require inflections from load path. --- activesupport/lib/active_support/inflector.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index fc88d80cdb..9d549eaae1 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -10,6 +10,8 @@ module ActiveSupport # If you discover an incorrect inflection and require it for your application, you'll need # to correct it yourself (explained below). module Inflector + extend self + # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. Examples: # @@ -91,8 +93,6 @@ module ActiveSupport end end - extend self - # Yields a singleton instance of Inflector::Inflections so you can specify additional # inflector rules. # @@ -134,7 +134,7 @@ module ActiveSupport # "posts".singularize # => "post" # "octopi".singularize # => "octopus" # "sheep".singluarize # => "sheep" - # "word".singluarize # => "word" + # "word".singularize # => "word" # "the blue mailmen".singularize # => "the blue mailman" # "CamelOctopi".singularize # => "CamelOctopus" def singularize(word) @@ -307,4 +307,4 @@ module ActiveSupport end end -require File.dirname(__FILE__) + '/inflections' +require 'active_support/inflections' -- cgit v1.2.3 From c1a98209da7422965f5dd4f475603b8a3cc887e4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:03:30 -0700 Subject: Cache RecordIdentifier methods in Class#model_name wrapper --- .../lib/action_controller/record_identifier.rb | 45 ++++++++++++++++------ 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 643ff7e5f4..77fe5cbe2a 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/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 @@ -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 @@ -56,7 +75,8 @@ module ActionController # 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. @@ -69,8 +89,11 @@ module ActionController # # 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: @@ -78,7 +101,7 @@ module ActionController # 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: @@ -86,12 +109,12 @@ module ActionController # 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 \ No newline at end of file +end -- cgit v1.2.3 From 566d717d783f56563cd602198be2177c972c9a81 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:38:05 -0700 Subject: Move Class::ModelName to Active Support module core_ext --- .../lib/action_controller/record_identifier.rb | 20 ++------------------ .../lib/active_support/core_ext/module.rb | 5 +++++ .../active_support/core_ext/module/model_naming.rb | 22 ++++++++++++++++++++++ .../test/core_ext/module/model_naming_test.rb | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/module/model_naming.rb create mode 100644 activesupport/test/core_ext/module/model_naming_test.rb diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 77fe5cbe2a..f69c3d6163 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,19 +1,3 @@ -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 @@ -59,9 +43,9 @@ module ActionController name = model_name_from_record_or_class(record_or_class) if controller_path && controller_path.include?("/") - "#{File.dirname(controller_path)}/#{name.path}" + "#{File.dirname(controller_path)}/#{name.partial_path}" else - name.path + name.partial_path end end diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index da8d5b3762..34fcbd124b 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -6,3 +6,8 @@ require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/loading' require 'active_support/core_ext/module/aliasing' +require 'active_support/core_ext/module/model_naming' + +class Module + include ActiveSupport::CoreExt::Module::ModelNaming +end diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb new file mode 100644 index 0000000000..26e76ab556 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb @@ -0,0 +1,22 @@ +module ActiveSupport + class ModelName < String + attr_reader :singular, :plural, :partial_path + + def initialize(name) + super + @singular = underscore.tr('/', '_').freeze + @plural = @singular.pluralize.freeze + @partial_path = "#{tableize}/#{demodulize.underscore}".freeze + end + end + + module CoreExt + module Module + module ModelNaming + def model_name + @model_name ||= ModelName.new(name) + end + end + end + end +end diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb new file mode 100644 index 0000000000..fc73fa5c36 --- /dev/null +++ b/activesupport/test/core_ext/module/model_naming_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' + +class ModelNamingTest < Test::Unit::TestCase + def setup + @name = ActiveSupport::ModelName.new('Post::TrackBack') + end + + def test_singular + assert_equal 'post_track_back', @name.singular + end + + def test_plural + assert_equal 'post_track_backs', @name.plural + end + + def test_partial_path + assert_equal 'post/track_backs/track_back', @name.partial_path + end +end -- cgit v1.2.3