diff options
Diffstat (limited to 'activemodel')
7 files changed, 75 insertions, 216 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index a8f470397b..9d0ff5ba99 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,11 @@ ## Rails 4.0.0 (unreleased) ## +* Changed inclusion and exclusion validators to accept a symbol for `:in` option. + + This allows to use dynamic inclusion/exclusion values using methods, besides the current lambda/proc support. + + *Gabriel Sobrinho* + * `AM::Validation#validates` ability to pass custom exception to `:strict` option. *Bogdan Gusiev* @@ -50,208 +56,4 @@ * When `^` or `$` are used in the regular expression provided to `validates_format_of` and the :multiline option is not set to true, an exception will be raised. This is to prevent security vulnerabilities when using `validates_format_of`. The problem is described in detail in the Rails security guide. - -## Rails 3.2.8 (Aug 9, 2012) ## - -* No changes. - - -## Rails 3.2.7 (Jul 26, 2012) ## - -* `validates_inclusion_of` and `validates_exclusion_of` now accept `:within` option as alias of `:in` as documented. - -* Fix the the backport of the object dup with the ruby 1.9.3p194. - - -## Rails 3.2.6 (Jun 12, 2012) ## - -* No changes. - - -## Rails 3.2.5 (Jun 1, 2012) ## - -* No changes. - - -## Rails 3.2.4 (May 31, 2012) ## - -* No changes. - - -## Rails 3.2.3 (March 30, 2012) ## - -* No changes. - - -## Rails 3.2.2 (March 1, 2012) ## - -* No changes. - - -## Rails 3.2.1 (January 26, 2012) ## - -* No changes. - - -## Rails 3.2.0 (January 20, 2012) ## - -* Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`, because this only existed to - support methods like `set_table_name` in Active Record, which are themselves being deprecated. - - *Jon Leighton* - -* Add 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.3 (November 20, 2011) ## - -* No changes - - -## Rails 3.1.2 (November 18, 2011) ## - -* No changes - - -## Rails 3.1.1 (October 7, 2011) ## - -* Remove hard dependency on bcrypt-ruby to avoid make ActiveModel dependent on a binary library. - You must add the gem explicitly to your Gemfile if you want use ActiveModel::SecurePassword: - - gem 'bcrypt-ruby', '~> 3.0.0' - - See GH #2687. *Guillermo Iguaran* - - -## 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.12 (March 1, 2012) ## - -* No changes. - - -## Rails 3.0.11 (November 18, 2011) ## - -* No changes. - - -## Rails 3.0.10 (August 16, 2011) ## - -* No changes. - - -## Rails 3.0.9 (June 16, 2011) ## - -* No changes. - - -## Rails 3.0.8 (June 7, 2011) ## - -* No changes. - - -## 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. +Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activemodel/CHANGELOG.md) for previous changes. diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb index 643a6f2b7c..cf1415b6c2 100644 --- a/activemodel/lib/active_model/validations/clusivity.rb +++ b/activemodel/lib/active_model/validations/clusivity.rb @@ -3,11 +3,11 @@ require 'active_support/core_ext/range.rb' module ActiveModel module Validations module Clusivity #:nodoc: - ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " << + ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " << "and must be supplied as the :in (or :within) option of the configuration hash" def check_validity! - unless [:include?, :call].any?{ |method| delimiter.respond_to?(method) } + unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym) raise ArgumentError, ERROR_MESSAGE end end @@ -15,7 +15,14 @@ module ActiveModel private def include?(record, value) - exclusions = delimiter.respond_to?(:call) ? delimiter.call(record) : delimiter + exclusions = if delimiter.respond_to?(:call) + delimiter.call(record) + elsif delimiter.respond_to?(:to_sym) + record.send(delimiter) + else + delimiter + end + exclusions.send(inclusion_method(exclusions), value) end diff --git a/activemodel/lib/active_model/validations/exclusion.rb b/activemodel/lib/active_model/validations/exclusion.rb index dc3368c569..3ec552c372 100644 --- a/activemodel/lib/active_model/validations/exclusion.rb +++ b/activemodel/lib/active_model/validations/exclusion.rb @@ -24,11 +24,12 @@ module ActiveModel # validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed" # validates_exclusion_of :password, in: ->(person) { [person.username, person.first_name] }, # message: 'should not be the same as your username or first name' + # validates_exclusion_of :karma, in: :reserved_karmas # end # # Configuration options: # * <tt>:in</tt> - An enumerable object of items that the value shouldn't - # be part of. This can be supplied as a proc or lambda which returns an + # be part of. This can be supplied as a proc, lambda or symbol which returns an # enumerable. If the enumerable is a range the test is performed with # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt> # <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb index c2835c550b..babc8982da 100644 --- a/activemodel/lib/active_model/validations/inclusion.rb +++ b/activemodel/lib/active_model/validations/inclusion.rb @@ -23,11 +23,12 @@ module ActiveModel # validates_inclusion_of :age, in: 0..99 # validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list" # validates_inclusion_of :states, in: ->(person) { STATES[person.country] } + # validates_inclusion_of :karma, in: :available_karmas # end # # Configuration options: # * <tt>:in</tt> - An enumerable object of available items. This can be - # supplied as a proc or lambda which returns an enumerable. If the + # supplied as a proc, lambda or symbol which returns an enumerable. If the # enumerable is a range the test is performed with <tt>Range#cover?</tt>, # otherwise with <tt>include?</tt>. # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt> diff --git a/activemodel/test/cases/mass_assignment_security_test.rb b/activemodel/test/cases/mass_assignment_security_test.rb index 0c6352cd71..45757615f5 100644 --- a/activemodel/test/cases/mass_assignment_security_test.rb +++ b/activemodel/test/cases/mass_assignment_security_test.rb @@ -11,7 +11,6 @@ class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer end class MassAssignmentSecurityTest < ActiveModel::TestCase - def test_attribute_protection user = User.new expected = { "name" => "John Smith", "email" => "john@smith.com" } @@ -96,7 +95,6 @@ class MassAssignmentSecurityTest < ActiveModel::TestCase assert_blank TightDescendant.protected_attributes(:admin) - TightDescendant.attributes_protected_by_default assert_equal Set.new(['name', 'address', 'admin', 'super_powers']), TightDescendant.accessible_attributes(:admin) - end def test_mass_assignment_multiparameter_protector @@ -107,14 +105,14 @@ class MassAssignmentSecurityTest < ActiveModel::TestCase end def test_custom_sanitizer + old_sanitizer = User._mass_assignment_sanitizer + user = User.new User.mass_assignment_sanitizer = CustomSanitizer.new assert_raise StandardError do user.sanitize_for_mass_assignment("admin" => true) end ensure - User.mass_assignment_sanitizer = nil - + User.mass_assignment_sanitizer = old_sanitizer end - -end +end
\ No newline at end of file diff --git a/activemodel/test/cases/validations/exclusion_validation_test.rb b/activemodel/test/cases/validations/exclusion_validation_test.rb index baccf72ecb..7d5af27f3d 100644 --- a/activemodel/test/cases/validations/exclusion_validation_test.rb +++ b/activemodel/test/cases/validations/exclusion_validation_test.rb @@ -64,4 +64,29 @@ class ExclusionValidationTest < ActiveModel::TestCase t.title = "wasabi" assert t.valid? end + + def test_validates_inclusion_of_with_symbol + Person.validates_exclusion_of :karma, :in => :reserved_karmas + + p = Person.new + p.karma = "abe" + + def p.reserved_karmas + %w(abe) + end + + assert p.invalid? + assert_equal ["is reserved"], p.errors[:karma] + + p = Person.new + p.karma = "abe" + + def p.reserved_karmas + %w() + end + + assert p.valid? + ensure + Person.reset_callbacks(:validate) + end end diff --git a/activemodel/test/cases/validations/inclusion_validation_test.rb b/activemodel/test/cases/validations/inclusion_validation_test.rb index c57fa75faf..117e9109fc 100644 --- a/activemodel/test/cases/validations/inclusion_validation_test.rb +++ b/activemodel/test/cases/validations/inclusion_validation_test.rb @@ -96,4 +96,29 @@ class InclusionValidationTest < ActiveModel::TestCase t.title = "elephant" assert t.valid? end + + def test_validates_inclusion_of_with_symbol + Person.validates_inclusion_of :karma, :in => :available_karmas + + p = Person.new + p.karma = "Lifo" + + def p.available_karmas + %w() + end + + assert p.invalid? + assert_equal ["is not included in the list"], p.errors[:karma] + + p = Person.new + p.karma = "Lifo" + + def p.available_karmas + %w(Lifo) + end + + assert p.valid? + ensure + Person.reset_callbacks(:validate) + end end |