diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-05-12 00:18:42 -0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-05-12 09:31:30 -0700 |
commit | f7862b2c34b5b298bf7b937c55f0637ebfe43a25 (patch) | |
tree | c05c9b3cafaa86dfb69c9d2405d5e8f98fc4a3dd /activemodel/lib/active_model/naming.rb | |
parent | 42fa2714c5005bb50679bf9a081d8bf0774f7eec (diff) | |
download | rails-f7862b2c34b5b298bf7b937c55f0637ebfe43a25.tar.gz rails-f7862b2c34b5b298bf7b937c55f0637ebfe43a25.tar.bz2 rails-f7862b2c34b5b298bf7b937c55f0637ebfe43a25.zip |
Refactor of active_model/naming.rb and allow collection and element to be writable
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activemodel/lib/active_model/naming.rb')
-rw-r--r-- | activemodel/lib/active_model/naming.rb | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 8cdd3d2fe8..a860388bae 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -3,18 +3,36 @@ require 'active_support/inflector' module ActiveModel class Name < String - attr_reader :singular, :plural, :element, :collection, :partial_path - alias_method :cache_key, :collection + attr_reader :singular, :plural, :element def initialize(klass) super(klass.name) @klass = klass @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze @plural = ActiveSupport::Inflector.pluralize(@singular).freeze - @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + @collection = nil + self.element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + end + + def element=(element) + @element = element @human = ActiveSupport::Inflector.humanize(@element).freeze - @collection = ActiveSupport::Inflector.tableize(self).freeze - @partial_path = "#{@collection}/#{@element}".freeze + @default_collection = nil + @partial_path = nil + end + + def collection + @collection || default_collection + end + alias_method :cache_key, :collection + + def collection=(collection) + @collection = collection + @partial_path = nil + end + + def partial_path + @partial_path ||= "#{collection}/#{@element}" end # Transform the model name into a more humane format, using I18n. By default, @@ -34,6 +52,12 @@ module ActiveModel options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults I18n.translate(defaults.shift, options) end + + private + + def default_collection + @default_collection ||= ActiveSupport::Inflector.tableize(self.sub(/[^:]*$/, @element)).freeze + end end # ActiveModel::Naming is a module that creates a +model_name+ method on your |