Skip to content
This repository
Browse code

Merge branch 'master' into erbout

  • Loading branch information...
commit 49c4c7b997c9e55907c78723c73f537087b1497d 2 parents 9c9da5d + 566d717
Jeremy Kemper authored June 06, 2008
29  actionpack/lib/action_controller/record_identifier.rb
@@ -31,18 +31,21 @@ module ActionController
31 31
   module RecordIdentifier
32 32
     extend self
33 33
 
  34
+    JOIN = '_'.freeze
  35
+    NEW = 'new'.freeze
  36
+
34 37
     # Returns plural/singular for a record or class. Example:
35 38
     #
36 39
     #   partial_path(post)                   # => "posts/post"
37 40
     #   partial_path(Person)                 # => "people/person"
38 41
     #   partial_path(Person, "admin/games")  # => "admin/people/person"
39 42
     def partial_path(record_or_class, controller_path = nil)
40  
-      klass = class_from_record_or_class(record_or_class)
  43
+      name = model_name_from_record_or_class(record_or_class)
41 44
 
42 45
       if controller_path && controller_path.include?("/")
43  
-        "#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
  46
+        "#{File.dirname(controller_path)}/#{name.partial_path}"
44 47
       else
45  
-        "#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
  48
+        name.partial_path
46 49
       end
47 50
     end
48 51
 
@@ -56,7 +59,8 @@ def partial_path(record_or_class, controller_path = nil)
56 59
     #   dom_class(post, :edit)   # => "edit_post"
57 60
     #   dom_class(Person, :edit) # => "edit_person"
58 61
     def dom_class(record_or_class, prefix = nil)
59  
-      [ prefix, singular_class_name(record_or_class) ].compact * '_'
  62
+      singular = singular_class_name(record_or_class)
  63
+      prefix ? "#{prefix}#{JOIN}#{singular}" : singular
60 64
     end
61 65
 
62 66
     # The DOM id convention is to use the singular form of an object or class with the id following an underscore.
@@ -69,8 +73,11 @@ def dom_class(record_or_class, prefix = nil)
69 73
     #
70 74
     #   dom_id(Post.new(:id => 45), :edit) # => "edit_post_45"
71 75
     def dom_id(record, prefix = nil) 
72  
-      prefix ||= 'new' unless record.id
73  
-      [ prefix, singular_class_name(record), record.id ].compact * '_'
  76
+      if record_id = record.id
  77
+        "#{dom_class(record, prefix)}#{JOIN}#{record_id}"
  78
+      else
  79
+        dom_class(record, prefix || NEW)
  80
+      end
74 81
     end
75 82
 
76 83
     # Returns the plural class name of a record or class. Examples:
@@ -78,7 +85,7 @@ def dom_id(record, prefix = nil)
78 85
     #   plural_class_name(post)             # => "posts"
79 86
     #   plural_class_name(Highrise::Person) # => "highrise_people"
80 87
     def plural_class_name(record_or_class)
81  
-      singular_class_name(record_or_class).pluralize
  88
+      model_name_from_record_or_class(record_or_class).plural
82 89
     end
83 90
 
84 91
     # Returns the singular class name of a record or class. Examples:
@@ -86,12 +93,12 @@ def plural_class_name(record_or_class)
86 93
     #   singular_class_name(post)             # => "post"
87 94
     #   singular_class_name(Highrise::Person) # => "highrise_person"
88 95
     def singular_class_name(record_or_class)
89  
-      class_from_record_or_class(record_or_class).name.underscore.tr('/', '_')
  96
+      model_name_from_record_or_class(record_or_class).singular
90 97
     end
91 98
 
92 99
     private
93  
-      def class_from_record_or_class(record_or_class)
94  
-        record_or_class.is_a?(Class) ? record_or_class : record_or_class.class
  100
+      def model_name_from_record_or_class(record_or_class)
  101
+        (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
95 102
       end
96 103
   end
97  
-end
  104
+end
5  activesupport/lib/active_support/core_ext/module.rb
@@ -6,3 +6,8 @@
6 6
 require 'active_support/core_ext/module/introspection'
7 7
 require 'active_support/core_ext/module/loading'
8 8
 require 'active_support/core_ext/module/aliasing'
  9
+require 'active_support/core_ext/module/model_naming'
  10
+
  11
+class Module
  12
+  include ActiveSupport::CoreExt::Module::ModelNaming
  13
+end
22  activesupport/lib/active_support/core_ext/module/model_naming.rb
... ...
@@ -0,0 +1,22 @@
  1
+module ActiveSupport
  2
+  class ModelName < String
  3
+    attr_reader :singular, :plural, :partial_path
  4
+
  5
+    def initialize(name)
  6
+      super
  7
+      @singular = underscore.tr('/', '_').freeze
  8
+      @plural = @singular.pluralize.freeze
  9
+      @partial_path = "#{tableize}/#{demodulize.underscore}".freeze
  10
+    end
  11
+  end
  12
+
  13
+  module CoreExt
  14
+    module Module
  15
+      module ModelNaming
  16
+        def model_name
  17
+          @model_name ||= ModelName.new(name)
  18
+        end
  19
+      end
  20
+    end
  21
+  end
  22
+end
8  activesupport/lib/active_support/inflector.rb
@@ -10,6 +10,8 @@ module ActiveSupport
10 10
   # If you discover an incorrect inflection and require it for your application, you'll need
11 11
   # to correct it yourself (explained below).
12 12
   module Inflector
  13
+    extend self
  14
+
13 15
     # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
14 16
     # inflection rules. Examples:
15 17
     #
@@ -91,8 +93,6 @@ def clear(scope = :all)
91 93
       end
92 94
     end
93 95
 
94  
-    extend self
95  
-
96 96
     # Yields a singleton instance of Inflector::Inflections so you can specify additional
97 97
     # inflector rules.
98 98
     #
@@ -134,7 +134,7 @@ def pluralize(word)
134 134
     #   "posts".singularize            # => "post"
135 135
     #   "octopi".singularize           # => "octopus"
136 136
     #   "sheep".singluarize            # => "sheep"
137  
-    #   "word".singluarize             # => "word"
  137
+    #   "word".singularize             # => "word"
138 138
     #   "the blue mailmen".singularize # => "the blue mailman"
139 139
     #   "CamelOctopi".singularize      # => "CamelOctopus"
140 140
     def singularize(word)
@@ -307,4 +307,4 @@ def ordinalize(number)
307 307
   end
308 308
 end
309 309
 
310  
-require File.dirname(__FILE__) + '/inflections'
  310
+require 'active_support/inflections'
19  activesupport/test/core_ext/module/model_naming_test.rb
... ...
@@ -0,0 +1,19 @@
  1
+require 'abstract_unit'
  2
+
  3
+class ModelNamingTest < Test::Unit::TestCase
  4
+  def setup
  5
+    @name = ActiveSupport::ModelName.new('Post::TrackBack')
  6
+  end
  7
+
  8
+  def test_singular
  9
+    assert_equal 'post_track_back', @name.singular
  10
+  end
  11
+
  12
+  def test_plural
  13
+    assert_equal 'post_track_backs', @name.plural
  14
+  end
  15
+
  16
+  def test_partial_path
  17
+    assert_equal 'post/track_backs/track_back', @name.partial_path
  18
+  end
  19
+end

0 notes on commit 49c4c7b

Please sign in to comment.
Something went wrong with that request. Please try again.