mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move Class::ModelName to Active Support module core_ext
This commit is contained in:
parent
c1a98209da
commit
566d717d78
4 changed files with 48 additions and 18 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
19
activesupport/test/core_ext/module/model_naming_test.rb
Normal file
19
activesupport/test/core_ext/module/model_naming_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue