aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/module.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/module/model_naming.rb22
-rw-r--r--activesupport/test/core_ext/module/model_naming_test.rb19
3 files changed, 46 insertions, 0 deletions
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