diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG | 107 | ||||
-rw-r--r-- | activemodel/CHANGELOG.md | 109 | ||||
-rw-r--r-- | activemodel/activemodel.gemspec | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 35 | ||||
-rw-r--r-- | activemodel/lib/active_model/observing.rb | 3 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 43 |
8 files changed, 181 insertions, 122 deletions
diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG deleted file mode 100644 index 3d26d646b0..0000000000 --- a/activemodel/CHANGELOG +++ /dev/null @@ -1,107 +0,0 @@ -* Add ability to define strict validation(with :strict => true option) that always raises exception when fails [Bogdan Gusiev] - -* Deprecate "Model.model_name.partial_path" in favor of "model.to_partial_path" [Grant Hutchins, Peter Jaros] - -* Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior [Bogdan Gusiev] - -*Rails 3.1.0 (August 30, 2011)* - -* Alternate I18n namespace lookup is no longer supported. - Instead of "activerecord.models.admins.post", do "activerecord.models.admins/post" instead [José Valim] - -* attr_accessible and friends now accepts :as as option to specify a role [Josh Kalderimis] - -* Add support for proc or lambda as an option for InclusionValidator, - ExclusionValidator, and FormatValidator [Prem Sichanugrist] - - You can now supply Proc, lambda, or anything that respond to #call in those - validations, and it will be called with current record as an argument. - That given proc or lambda must returns an object which respond to #include? for - InclusionValidator and ExclusionValidator, and returns a regular expression - object for FormatValidator. - -* Added ActiveModel::SecurePassword to encapsulate dead-simple password usage with BCrypt encryption and salting [DHH] - -* ActiveModel::AttributeMethods allows attributes to be defined on demand [Alexander Uvarov] - -* Add support for selectively enabling/disabling observers [Myron Marston] - - -*Rails 3.0.7 (April 18, 2011)* - -*No changes. - - -*Rails 3.0.6 (April 5, 2011) - -* Fix when database column name has some symbolic characters (e.g. Oracle CASE# VARCHAR2(20)) #5818 #6850 [Robert Pankowecki, Santiago Pastorino] - -* Fix length validation for fixnums #6556 [Andriy Tyurnikov] - -* Fix i18n key collision with namespaced models #6448 [yves.senn] - - -*Rails 3.0.5 (February 26, 2011)* - -* No changes. - - -*Rails 3.0.4 (February 8, 2011)* - -* No changes. - - -*Rails 3.0.3 (November 16, 2010)* - -* No changes. - - -*Rails 3.0.2 (November 15, 2010)* - -* No changes - - -*Rails 3.0.1 (October 15, 2010)* - -* No Changes, just a version bump. - - -*Rails 3.0.0 (August 29, 2010)* - -* Added ActiveModel::MassAssignmentSecurity [Eric Chapweske, Josh Kalderimis] - -* JSON supports a custom root option: to_json(:root => 'custom') #4515 [Jatinder Singh] - -* #new_record? and #destroyed? were removed from ActiveModel::Lint. Use - persisted? instead. A model is persisted if it's not a new_record? and it was - not destroyed? [MG] - -* Added validations reflection in ActiveModel::Validations [JV] - - Model.validators - Model.validators_on(:field) - -* #to_key was added to ActiveModel::Lint so we can generate DOM IDs for - AMo objects with composite keys [MG] - -* ActiveModel::Observer#add_observer! - - It has a custom hook to define after_find that should really be in a - ActiveRecord::Observer subclass: - - def add_observer!(klass) - klass.add_observer(self) - klass.class_eval 'def after_find() end' unless klass.respond_to?(:after_find) - end - -* Change the ActiveModel::Base.include_root_in_json default to true for Rails 3 [DHH] - -* Add validates_format_of :without => /regexp/ option. #430 [Elliot Winkler, Peer Allan] - - Example : - - validates_format_of :subdomain, :without => /www|admin|mail/ - -* Introduce validates_with to encapsulate attribute validations in a class. #2630 [Jeff Dean] - -* Extracted from Active Record and Active Resource. diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md new file mode 100644 index 0000000000..71a737caff --- /dev/null +++ b/activemodel/CHANGELOG.md @@ -0,0 +1,109 @@ +* Added ActiveModel::Errors#added? to check if a specific error has been added *Martin Svalin* + +* Add ability to define strict validation(with :strict => true option) that always raises exception when fails *Bogdan Gusiev* + +* Deprecate "Model.model_name.partial_path" in favor of "model.to_partial_path" *Grant Hutchins, Peter Jaros* + +* Provide mass_assignment_sanitizer as an easy API to replace the sanitizer behavior. Also support both :logger (default) and :strict sanitizer behavior *Bogdan Gusiev* + +## Rails 3.1.0 (August 30, 2011) ## + +* Alternate I18n namespace lookup is no longer supported. + Instead of "activerecord.models.admins.post", do "activerecord.models.admins/post" instead *José Valim* + +* attr_accessible and friends now accepts :as as option to specify a role *Josh Kalderimis* + +* Add support for proc or lambda as an option for InclusionValidator, + ExclusionValidator, and FormatValidator *Prem Sichanugrist* + + You can now supply Proc, lambda, or anything that respond to #call in those + validations, and it will be called with current record as an argument. + That given proc or lambda must returns an object which respond to #include? for + InclusionValidator and ExclusionValidator, and returns a regular expression + object for FormatValidator. + +* Added ActiveModel::SecurePassword to encapsulate dead-simple password usage with BCrypt encryption and salting *DHH* + +* ActiveModel::AttributeMethods allows attributes to be defined on demand *Alexander Uvarov* + +* Add support for selectively enabling/disabling observers *Myron Marston* + + +## Rails 3.0.7 (April 18, 2011) ## + +* No changes. + + +* Rails 3.0.6 (April 5, 2011) + +* Fix when database column name has some symbolic characters (e.g. Oracle CASE# VARCHAR2(20)) #5818 #6850 *Robert Pankowecki, Santiago Pastorino* + +* Fix length validation for fixnums #6556 *Andriy Tyurnikov* + +* Fix i18n key collision with namespaced models #6448 *yves.senn* + + +## Rails 3.0.5 (February 26, 2011) ## + +* No changes. + + +## Rails 3.0.4 (February 8, 2011) ## + +* No changes. + + +## Rails 3.0.3 (November 16, 2010) ## + +* No changes. + + +## Rails 3.0.2 (November 15, 2010) ## + +* No changes + + +## Rails 3.0.1 (October 15, 2010) ## + +* No Changes, just a version bump. + + +## Rails 3.0.0 (August 29, 2010) ## + +* Added ActiveModel::MassAssignmentSecurity *Eric Chapweske, Josh Kalderimis* + +* JSON supports a custom root option: to_json(:root => 'custom') #4515 *Jatinder Singh* + +* #new_record? and #destroyed? were removed from ActiveModel::Lint. Use + persisted? instead. A model is persisted if it's not a new_record? and it was + not destroyed? *MG* + +* Added validations reflection in ActiveModel::Validations *JV* + + Model.validators + Model.validators_on(:field) + +* #to_key was added to ActiveModel::Lint so we can generate DOM IDs for + AMo objects with composite keys *MG* + +* ActiveModel::Observer#add_observer! + + It has a custom hook to define after_find that should really be in a + ActiveRecord::Observer subclass: + + def add_observer!(klass) + klass.add_observer(self) + klass.class_eval 'def after_find() end' unless klass.respond_to?(:after_find) + end + +* Change the ActiveModel::Base.include_root_in_json default to true for Rails 3 *DHH* + +* Add validates_format_of :without => /regexp/ option. #430 *Elliot Winkler, Peer Allan* + + Example : + + validates_format_of :subdomain, :without => /www|admin|mail/ + +* Introduce validates_with to encapsulate attribute validations in a class. #2630 *Jeff Dean* + +* Extracted from Active Record and Active Resource. diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 260ad01b65..49f664bf89 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |s| s.email = 'david@loudthinking.com' s.homepage = 'http://www.rubyonrails.org' - s.files = Dir['CHANGELOG', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] + s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.add_dependency('activesupport', version) diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index f90030641d..8337b04c0d 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -205,21 +205,14 @@ module ActiveModel messages.dup end - # Adds +message+ to the error messages on +attribute+, which will be returned on a call to - # <tt>on(attribute)</tt> for the same attribute. More than one error can be added to the same - # +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>. + # Adds +message+ to the error messages on +attribute+. More than one error can be added to the same + # +attribute+. # If no +message+ is supplied, <tt>:invalid</tt> is assumed. # # If +message+ is a symbol, it will be translated using the appropriate scope (see +translate_error+). # If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error. def add(attribute, message = nil, options = {}) - message ||= :invalid - - if message.is_a?(Symbol) - message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS)) - elsif message.is_a?(Proc) - message = message.call - end + message = normalize_message(attribute, message, options) if options[:strict] raise ActiveModel::StrictValidationFailed, message end @@ -244,6 +237,15 @@ module ActiveModel end end + # Returns true if an error on the attribute with the given message is present, false otherwise. + # +message+ is treated the same as for +add+. + # p.errors.add :name, :blank + # p.errors.added? :name, :blank # => true + def added?(attribute, message = nil, options = {}) + message = normalize_message(attribute, message, options) + self[attribute].include? message + end + # Returns all the full error messages in an array. # # class Company @@ -329,6 +331,19 @@ module ActiveModel I18n.translate(key, options) end + + private + def normalize_message(attribute, message, options) + message ||= :invalid + + if message.is_a?(Symbol) + generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS)) + elsif message.is_a?(Proc) + message.call + else + message + end + end end class StrictValidationFailed < StandardError diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index 7a910d18e7..cd8eb357de 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -187,8 +187,7 @@ module ActiveModel def observe(*models) models.flatten! models.collect! { |model| model.respond_to?(:to_sym) ? model.to_s.camelize.constantize : model } - remove_possible_method(:observed_classes) - define_method(:observed_classes) { models } + redefine_method(:observed_classes) { models } end # Returns an array of Classes to observe. diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index fbceb81e8f..8e09f6ac35 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -57,7 +57,7 @@ module ActiveModel # # Additionally validator classes may be in another namespace and still used within any class. # - # validates :name, :'file/title' => true + # validates :name, :'film/title' => true # # The validators hash can also handle regular expressions, ranges, # arrays and strings in shortcut form, e.g. diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 0e444738ba..35ec98c822 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -57,7 +57,7 @@ module ActiveModel #:nodoc: # To add behavior to the initialize method, use the following signature: # # class MyValidator < ActiveModel::Validator - # def initialize(record, options) + # def initialize(options) # super # @my_custom_field = options[:field_name] || :first_name # end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 784a2b2709..8ddedb160a 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -80,6 +80,49 @@ class ErrorsTest < ActiveModel::TestCase assert_equal ["can not be blank"], person.errors[:name] end + test "added? should be true if that error was added" do + person = Person.new + person.errors.add(:name, "can not be blank") + assert person.errors.added?(:name, "can not be blank") + end + + test "added? should handle when message is a symbol" do + person = Person.new + person.errors.add(:name, :blank) + assert person.errors.added?(:name, :blank) + end + + test "added? should handle when message is a proc" do + person = Person.new + message = Proc.new { "can not be blank" } + person.errors.add(:name, message) + assert person.errors.added?(:name, message) + end + + test "added? should default message to :invalid" do + person = Person.new + person.errors.add(:name, :invalid) + assert person.errors.added?(:name) + end + + test "added? should be true when several errors are present, and we ask for one of them" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "is invalid") + assert person.errors.added?(:name, "can not be blank") + end + + test "added? should be false if no errors are present" do + person = Person.new + assert !person.errors.added?(:name) + end + + test "added? should be false when an error is present, but we check for another error" do + person = Person.new + person.errors.add(:name, "is invalid") + assert !person.errors.added?(:name, "can not be blank") + end + test 'should respond to size' do person = Person.new person.errors.add(:name, "can not be blank") |