mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add #element and #collection to ModelName
This commit is contained in:
parent
d9f16fafec
commit
99cf77be27
2 changed files with 13 additions and 3 deletions
|
@ -2,14 +2,16 @@ require 'active_support/inflector'
|
||||||
|
|
||||||
module ActiveSupport
|
module ActiveSupport
|
||||||
class ModelName < String
|
class ModelName < String
|
||||||
attr_reader :singular, :plural, :cache_key, :partial_path
|
attr_reader :singular, :plural, :element, :collection, :partial_path
|
||||||
|
alias_method :cache_key, :collection
|
||||||
|
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
super
|
super
|
||||||
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
|
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
|
||||||
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
|
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
|
||||||
@cache_key = tableize.freeze
|
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
|
||||||
@partial_path = "#{@cache_key}/#{ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self))}".freeze
|
@collection = ActiveSupport::Inflector.tableize(self).freeze
|
||||||
|
@partial_path = "#{@collection}/#{@element}".freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,14 @@ class ModelNamingTest < Test::Unit::TestCase
|
||||||
assert_equal 'post_track_backs', @model_name.plural
|
assert_equal 'post_track_backs', @model_name.plural
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_element
|
||||||
|
assert_equal 'track_back', @model_name.element
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_collection
|
||||||
|
assert_equal 'post/track_backs', @model_name.collection
|
||||||
|
end
|
||||||
|
|
||||||
def test_partial_path
|
def test_partial_path
|
||||||
assert_equal 'post/track_backs/track_back', @model_name.partial_path
|
assert_equal 'post/track_backs/track_back', @model_name.partial_path
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue