diff options
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/conversion.rb | 28 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/lint.rb | 15 | ||||
-rw-r--r-- | activemodel/lib/active_model/naming.rb | 3 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/with.rb | 4 |
5 files changed, 44 insertions, 8 deletions
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb index 1405b1bfe3..80a3ba51c3 100644 --- a/activemodel/lib/active_model/conversion.rb +++ b/activemodel/lib/active_model/conversion.rb @@ -1,9 +1,12 @@ +require 'active_support/concern' +require 'active_support/inflector' + module ActiveModel # == Active Model Conversions # - # Handles default conversions: to_model, to_key and to_param. + # Handles default conversions: to_model, to_key, to_param, and to_partial_path. # - # Let's take for example this non persisted object. + # Let's take for example this non-persisted object. # # class ContactMessage # include ActiveModel::Conversion @@ -18,8 +21,11 @@ module ActiveModel # cm.to_model == self # => true # cm.to_key # => nil # cm.to_param # => nil + # cm.to_path # => "contact_messages/contact_message" # module Conversion + extend ActiveSupport::Concern + # If your object is already designed to implement all of the Active Model # you can use the default <tt>:to_model</tt> implementation, which simply # returns self. @@ -45,5 +51,23 @@ module ActiveModel def to_param persisted? ? to_key.join('-') : nil end + + # Returns a string identifying the path associated with the object. + # ActionPack uses this to find a suitable partial to represent the object. + def to_partial_path + self.class._to_partial_path + end + + module ClassMethods #:nodoc: + # Provide a class level cache for the to_path. This is an + # internal method and should not be accessed directly. + def _to_partial_path #:nodoc: + @_to_partial_path ||= begin + element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)) + collection = ActiveSupport::Inflector.tableize(self) + "#{collection}/#{element}".freeze + end + end + end end end diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 71ece15b71..1cf8144e98 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -86,7 +86,7 @@ module ActiveModel # Do the error messages include an error with key +error+? def include?(error) - messages.include? error + (v = messages[error]) && v.any? end # Get messages for +key+ diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index b71ef4b22e..bfe7ea1869 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -43,6 +43,16 @@ module ActiveModel assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false" end + # == Responds to <tt>to_partial_path</tt> + # + # Returns a string giving a relative path. This is used for looking up + # partials. For example, a BlogPost model might return "blog_posts/blog_post" + # + def test_to_partial_path + assert model.respond_to?(:to_partial_path), "The model should respond to to_partial_path" + assert_kind_of String, model.to_partial_path + end + # == Responds to <tt>valid?</tt> # # Returns a boolean that specifies whether the object is in a valid or invalid @@ -66,15 +76,14 @@ module ActiveModel # == Naming # - # Model.model_name must return a string with some convenience methods as - # :human and :partial_path. Check ActiveModel::Naming for more information. + # Model.model_name must return a string with some convenience methods: + # :human, :singular, and :plural. Check ActiveModel::Naming for more information. # def test_model_naming assert model.class.respond_to?(:model_name), "The model should respond to model_name" model_name = model.class.model_name assert_kind_of String, model_name assert_kind_of String, model_name.human - assert_kind_of String, model_name.partial_path assert_kind_of String, model_name.singular assert_kind_of String, model_name.plural end diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 4c1a82f413..f16459ede2 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -1,12 +1,15 @@ require 'active_support/inflector' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/module/introspection' +require 'active_support/core_ext/module/deprecation' module ActiveModel class Name < String attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key alias_method :cache_key, :collection + deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead." + def initialize(klass, namespace = nil, name = nil) name ||= klass.name super(name) diff --git a/activemodel/lib/active_model/validations/with.rb b/activemodel/lib/active_model/validations/with.rb index 65ae18a769..a87b213fe4 100644 --- a/activemodel/lib/active_model/validations/with.rb +++ b/activemodel/lib/active_model/validations/with.rb @@ -101,7 +101,7 @@ module ActiveModel # class Person # include ActiveModel::Validations # - # validates :instance_validations + # validate :instance_validations # # def instance_validations # validates_with MyValidator @@ -116,7 +116,7 @@ module ActiveModel # class Person # include ActiveModel::Validations # - # validates :instance_validations, :on => :create + # validate :instance_validations, :on => :create # # def instance_validations # validates_with MyValidator, MyOtherValidator |