From 0e655873d4f41922fa2614919342482b1dbd2343 Mon Sep 17 00:00:00 2001 From: William Myers Date: Mon, 7 Jan 2013 00:04:35 -0500 Subject: DirtyModel uses a hash to keep track of any changes made to attributes of an instance. When using the attribute_will_change! method, you must supply a string and not a symbol or the *_changed? method will break (because it is looking for the attribute name as a string in the keys of the underlying hash). To remedy this, I simply made the underlying hash a HashWithIndifferentAccess so it won't matter if you supply the attribute name as a symbol or string to attribute_will_change!. --- activemodel/lib/active_model/dirty.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 6e67cd2285..b89b98feed 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -139,7 +139,7 @@ module ActiveModel # person.name = 'robert' # person.changed_attributes # => {"name" => "bob"} def changed_attributes - @changed_attributes ||= {} + @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end private -- cgit v1.2.3 From 9aa1a3d85327fa0a3055b5b757a0be092ce582f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 10:59:05 -0300 Subject: Merge pull request #10816 from bogdan/less-dirty-dirty Make AM::Dirty less dirty to plugin into AR or other library --- activemodel/lib/active_model/dirty.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index ea5ddf71de..be2759b141 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -14,13 +14,7 @@ module ActiveModel # track. # * Call attr_name_will_change! before each change to the tracked # attribute. - # - # If you wish to also track previous changes on save or update, you need to - # add: - # - # @previously_changed = changes - # - # inside of your save or update method. + # * Call changes_applied after the changes are persisted. # # A minimal implementation could be: # @@ -39,8 +33,8 @@ module ActiveModel # end # # def save - # @previously_changed = changes - # @changed_attributes.clear + # # do persistence work + # changes_applied # end # end # @@ -129,7 +123,7 @@ module ActiveModel # person.save # person.previous_changes # => {"name" => ["bob", "robert"]} def previous_changes - @previously_changed + @previously_changed ||= {} end # Returns a hash of the attributes with unsaved changes indicating their original @@ -154,6 +148,18 @@ module ActiveModel private + # Removes current changes and makes them accessible through +previous_changes+. + def changes_applied + @previously_changed = changes + @changed_attributes = {} + end + + # Removes all dirty data: current changes and previous changes + def reset_changes + @previously_changed = {} + @changed_attributes = {} + end + # Handle *_change for +method_missing+. def attribute_change(attr) [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) -- cgit v1.2.3 From 089e1b6426719ad5132153323d23798094d271fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 11:00:41 -0300 Subject: Document reset_changes since it is part of public API [ci skip] --- activemodel/lib/active_model/dirty.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index be2759b141..0b17443219 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -15,6 +15,8 @@ module ActiveModel # * Call attr_name_will_change! before each change to the tracked # attribute. # * Call changes_applied after the changes are persisted. + # * Call reset_changes when you want to reset the changes + # information. # # A minimal implementation could be: # @@ -36,6 +38,10 @@ module ActiveModel # # do persistence work # changes_applied # end + # + # def reload! + # reset_changes + # end # end # # A newly instantiated object is unchanged: @@ -59,6 +65,12 @@ module ActiveModel # person.changed? # => false # person.name_changed? # => false # + # Reset the changes: + # + # person.previous_changes # => {"name" => ["Uncle Bob", "Bill"]} + # person.reload + # person.previous_changes # => {} + # # Assigning the same value leaves the attribute unchanged: # # person.name = 'Bill' -- cgit v1.2.3 From ed0b080cb37a420194b56a7029017ce4a1e8b408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 23 Sep 2013 11:25:43 -0300 Subject: Fix the documentation method. It is reload! in the class definition. [ci skip] --- activemodel/lib/active_model/dirty.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 0b17443219..98e7d84608 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -68,7 +68,7 @@ module ActiveModel # Reset the changes: # # person.previous_changes # => {"name" => ["Uncle Bob", "Bill"]} - # person.reload + # person.reload! # person.previous_changes # => {} # # Assigning the same value leaves the attribute unchanged: -- cgit v1.2.3 From 4a99e1019931481011c621fe89f715fe6dac9dd3 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Mon, 23 Sep 2013 14:28:26 -0400 Subject: bcrypt-ruby v3.1.2 supports Ruby 2.0 on Windows --- activemodel/lib/active_model/secure_password.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 8b9ac97bbb..17fafe4be9 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -20,9 +20,9 @@ module ActiveModel # value to the password_confirmation attribute and the validation # will not be triggered. # - # You need to add bcrypt-ruby (~> 3.1.0) to Gemfile to use #has_secure_password: + # You need to add bcrypt-ruby (~> 3.1.2) to Gemfile to use #has_secure_password: # - # gem 'bcrypt-ruby', '~> 3.1.0' + # gem 'bcrypt-ruby', '~> 3.1.2' # # Example using Active Record (which automatically includes ActiveModel::SecurePassword): # @@ -46,7 +46,7 @@ module ActiveModel # This is to avoid ActiveModel (and by extension the entire framework) # being dependent on a binary library. begin - gem 'bcrypt-ruby', '~> 3.1.0' + gem 'bcrypt-ruby', '~> 3.1.2' require 'bcrypt' rescue LoadError $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install" -- cgit v1.2.3 From 5d7b413d84e5858dba50e716c31981b42c40aba6 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Mon, 23 Sep 2013 14:46:41 -0400 Subject: Use bcrypt's public cost attr, not internal constant See: - https://github.com/codahale/bcrypt-ruby/pull/63 - https://github.com/codahale/bcrypt-ruby/pull/64 - https://github.com/codahale/bcrypt-ruby/pull/65 --- activemodel/lib/active_model/secure_password.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 17fafe4be9..f87c36e39e 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -103,7 +103,7 @@ module ActiveModel def password=(unencrypted_password) unless unencrypted_password.blank? @password = unencrypted_password - cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine::DEFAULT_COST + cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost) end end -- cgit v1.2.3 From 68db6bc431fbff0b2291f1f60ccf974b4eece596 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 23 Oct 2013 22:01:14 +0900 Subject: Let validates_inclusion_of accept Time and DateTime ranges fixes 4.0.0 regression introduced in 0317b93c17a46d7663a8c36edc26ad0ba3d75f85 --- activemodel/lib/active_model/validations/clusivity.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb index 1c35cb7c35..fd6cc1edb4 100644 --- a/activemodel/lib/active_model/validations/clusivity.rb +++ b/activemodel/lib/active_model/validations/clusivity.rb @@ -30,12 +30,18 @@ module ActiveModel @delimiter ||= options[:in] || options[:within] end - # In Ruby 1.9 Range#include? on non-numeric ranges checks all possible values in the - # range for equality, which is slower but more accurate. Range#cover? uses - # the previous logic of comparing a value with the range endpoints, which is fast - # but is only accurate on numeric ranges. + # In Ruby 1.9 Range#include? on non-number-or-time-ish ranges checks all + # possible values in the range for equality, which is slower but more accurate. + # Range#cover? uses the previous logic of comparing a value with the range + # endpoints, which is fast but is only accurate on Numeric, Time, or DateTime ranges. def inclusion_method(enumerable) - (enumerable.is_a?(Range) && enumerable.first.is_a?(Numeric)) ? :cover? : :include? + return :include? unless enumerable.is_a?(Range) + case enumerable.first + when Numeric, Time, DateTime + :cover? + else + :include? + end end end end -- cgit v1.2.3 From c31b900221cb15386235145b4a809a02abee8df9 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Thu, 24 Oct 2013 13:32:59 -0700 Subject: Merge pull request #12635 from mperham/4-0-stable Allow any version of BCrypt --- activemodel/lib/active_model/secure_password.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index f87c36e39e..7e694b5c50 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -46,7 +46,6 @@ module ActiveModel # This is to avoid ActiveModel (and by extension the entire framework) # being dependent on a binary library. begin - gem 'bcrypt-ruby', '~> 3.1.2' require 'bcrypt' rescue LoadError $stderr.puts "You don't have bcrypt-ruby installed in your application. Please add it to your Gemfile and run bundle install" -- cgit v1.2.3 From fb6f02c521b87c749757eb3260e4bfa120fc7c32 Mon Sep 17 00:00:00 2001 From: Andrey Samsonov Date: Tue, 29 Oct 2013 15:23:03 +0400 Subject: Minor doc fix of ActiveModel::Naming. - qoute example line's result when it kind of String - right ("singular_route_key") method in example --- activemodel/lib/active_model/naming.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index c0d93e5d53..4322592a7e 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -263,10 +263,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> post + # ActiveModel::Naming.singular_route_key(Blog::Post) #=> "post" # # # For shared engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> blog_post + # ActiveModel::Naming.singular_route_key(Blog::Post) #=> "blog_post" def self.singular_route_key(record_or_class) model_name_from_record_or_class(record_or_class).singular_route_key end @@ -275,10 +275,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> posts + # ActiveModel::Naming.route_key(Blog::Post) #=> "posts" # # # For shared engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts + # ActiveModel::Naming.route_key(Blog::Post) #=> "blog_posts" # # The route key also considers if the noun is uncountable and, in # such cases, automatically appends _index. @@ -290,10 +290,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.param_key(Blog::Post) #=> post + # ActiveModel::Naming.param_key(Blog::Post) #=> "post" # # # For shared engine: - # ActiveModel::Naming.param_key(Blog::Post) #=> blog_post + # ActiveModel::Naming.param_key(Blog::Post) #=> "blog_post" def self.param_key(record_or_class) model_name_from_record_or_class(record_or_class).param_key end -- cgit v1.2.3 From efff6c1fd4b9e2e4c9f705a45879373cb34a5b0e Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Nov 2013 13:53:54 -0500 Subject: Change syntax format for example returned values According to our guideline, we leave 1 space between `#` and `=>`, so we want `# =>` instead of `#=>`. Thanks to @fxn for the suggestion. [ci skip] --- activemodel/lib/active_model/naming.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 198efc5088..11ebfe6cc0 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -262,10 +262,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.singular_route_key(Blog::Post) #=> "post" + # ActiveModel::Naming.singular_route_key(Blog::Post) # => "post" # # # For shared engine: - # ActiveModel::Naming.singular_route_key(Blog::Post) #=> "blog_post" + # ActiveModel::Naming.singular_route_key(Blog::Post) # => "blog_post" def self.singular_route_key(record_or_class) model_name_from_record_or_class(record_or_class).singular_route_key end @@ -274,10 +274,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> "posts" + # ActiveModel::Naming.route_key(Blog::Post) # => "posts" # # # For shared engine: - # ActiveModel::Naming.route_key(Blog::Post) #=> "blog_posts" + # ActiveModel::Naming.route_key(Blog::Post) # => "blog_posts" # # The route key also considers if the noun is uncountable and, in # such cases, automatically appends _index. @@ -289,10 +289,10 @@ module ActiveModel # namespaced models regarding whether it's inside isolated engine. # # # For isolated engine: - # ActiveModel::Naming.param_key(Blog::Post) #=> "post" + # ActiveModel::Naming.param_key(Blog::Post) # => "post" # # # For shared engine: - # ActiveModel::Naming.param_key(Blog::Post) #=> "blog_post" + # ActiveModel::Naming.param_key(Blog::Post) # => "blog_post" def self.param_key(record_or_class) model_name_from_record_or_class(record_or_class).param_key end -- cgit v1.2.3 From f70e30ec6d3655d4a29de8014dfc501dab4008ae Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 00:05:10 -0200 Subject: Avoid creation of extra hash with merge just to set a value --- activemodel/lib/active_model/validations/numericality.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index c6abe45f4a..a02dc8b9e3 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -73,7 +73,9 @@ module ActiveModel end def filtered_options(value) - options.except(*RESERVED_OPTIONS).merge!(value: value) + filtered = options.except(*RESERVED_OPTIONS) + filtered[:value] = value + filtered end end -- cgit v1.2.3 From d2992818e02bd380f6a68385b4e97a301ec2f8aa Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 14 Nov 2013 23:59:40 -0200 Subject: Simplify number parsing logic in numericality validation --- activemodel/lib/active_model/validations/numericality.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index a02dc8b9e3..8f4ae693a1 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -56,16 +56,9 @@ module ActiveModel protected def parse_raw_value_as_a_number(raw_value) - case raw_value - when /\A0[xX]/ - nil - else - begin - Kernel.Float(raw_value) - rescue ArgumentError, TypeError - nil - end - end + Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/ + rescue ArgumentError, TypeError + nil end def parse_raw_value_as_an_integer(raw_value) -- cgit v1.2.3 From 4c7e3a3087d48ffbdf1c185ab12ef259b5f8e074 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 00:19:03 -0200 Subject: Use a simple conditional rather than short circuit with next --- activemodel/lib/active_model/validations/numericality.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 8f4ae693a1..0f131de923 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -11,8 +11,9 @@ module ActiveModel def check_validity! keys = CHECKS.keys - [:odd, :even] options.slice(*keys).each do |option, value| - next if value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol) - raise ArgumentError, ":#{option} must be a number, a symbol or a proc" + unless value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol) + raise ArgumentError, ":#{option} must be a number, a symbol or a proc" + end end end -- cgit v1.2.3 From 12815e0d44faf1154bebe5703577ff56c13688bc Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 00:24:40 -0200 Subject: Avoid a new hash allocation --- activemodel/lib/active_model/validations/numericality.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 0f131de923..5acd4477ff 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -48,7 +48,7 @@ module ActiveModel option_value = record.send(option_value) if option_value.is_a?(Symbol) unless value.send(CHECKS[option], option_value) - record.errors.add(attr_name, option, filtered_options(value).merge(count: option_value)) + record.errors.add(attr_name, option, filtered_options(value).merge!(count: option_value)) end end end -- cgit v1.2.3 From 70161ae3b890bb6b0603470709432a1bec911da7 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 00:19:28 -0200 Subject: Make code simpler to read by using a case statement --- activemodel/lib/active_model/validations/numericality.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 5acd4477ff..c8d3236463 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -44,8 +44,12 @@ module ActiveModel record.errors.add(attr_name, option, filtered_options(value)) end else - option_value = option_value.call(record) if option_value.is_a?(Proc) - option_value = record.send(option_value) if option_value.is_a?(Symbol) + case option_value + when Proc + option_value = option_value.call(record) + when Symbol + option_value = record.send(option_value) + end unless value.send(CHECKS[option], option_value) record.errors.add(attr_name, option, filtered_options(value).merge!(count: option_value)) -- cgit v1.2.3 From b8c6c0845218fe976a3f530688c54f62813cb31a Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 00:38:22 -0200 Subject: Cache regexp source on format validation to avoid allocating new objects Example: >> r = /some-regexp/ => /some-regexp/ >> r.source.object_id == r.source.object_id => false --- activemodel/lib/active_model/validations/format.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index be7cae588f..b6cc162582 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -33,8 +33,8 @@ module ActiveModel end def regexp_using_multiline_anchors?(regexp) - regexp.source.start_with?("^") || - (regexp.source.end_with?("$") && !regexp.source.end_with?("\\$")) + source = regexp.source + source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$")) end def check_options_validity(options, name) -- cgit v1.2.3 From 61fef761065e310a8c398bd8f52da01cb82723e6 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 01:00:53 -0200 Subject: Remove argument that is accessible as attribute --- activemodel/lib/active_model/validations/format.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index b6cc162582..f17105e574 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -17,8 +17,8 @@ module ActiveModel raise ArgumentError, "Either :with or :without must be supplied (but not both)" end - check_options_validity(options, :with) - check_options_validity(options, :without) + check_options_validity :with + check_options_validity :without end private @@ -37,7 +37,7 @@ module ActiveModel source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$")) end - def check_options_validity(options, name) + def check_options_validity(name) option = options[name] if option && !option.is_a?(Regexp) && !option.respond_to?(:call) raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}" -- cgit v1.2.3 From 9014a79436c8585ac17c8c27d14f7467cddd3562 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 01:01:59 -0200 Subject: Only check that the option exists once instead of doing on each conditional --- activemodel/lib/active_model/validations/format.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index f17105e574..488a498a41 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -38,14 +38,15 @@ module ActiveModel end def check_options_validity(name) - option = options[name] - if option && !option.is_a?(Regexp) && !option.respond_to?(:call) - raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}" - elsif option && option.is_a?(Regexp) && - regexp_using_multiline_anchors?(option) && options[:multiline] != true - raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \ - "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \ - ":multiline => true option?" + if option = options[name] + if !option.is_a?(Regexp) && !option.respond_to?(:call) + raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}" + elsif option.is_a?(Regexp) && + regexp_using_multiline_anchors?(option) && options[:multiline] != true + raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \ + "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \ + ":multiline => true option?" + end end end end -- cgit v1.2.3 From 374d465f2894dc5b120cc6a5f28619a6e60407be Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 01:03:07 -0200 Subject: Invert conditional to avoid double checking for Regexp --- activemodel/lib/active_model/validations/format.rb | 23 +++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 488a498a41..f0fe22438f 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -32,23 +32,24 @@ module ActiveModel record.errors.add(attribute, :invalid, options.except(name).merge!(value: value)) end - def regexp_using_multiline_anchors?(regexp) - source = regexp.source - source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$")) - end - def check_options_validity(name) if option = options[name] - if !option.is_a?(Regexp) && !option.respond_to?(:call) + if option.is_a?(Regexp) + if options[:multiline] != true && regexp_using_multiline_anchors?(option) + raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \ + "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \ + ":multiline => true option?" + end + elsif !option.respond_to?(:call) raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}" - elsif option.is_a?(Regexp) && - regexp_using_multiline_anchors?(option) && options[:multiline] != true - raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \ - "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \ - ":multiline => true option?" end end end + + def regexp_using_multiline_anchors?(regexp) + source = regexp.source + source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$")) + end end module HelperMethods -- cgit v1.2.3 From aa7fdfb859d8a73f58460a7aba7174a47b5101d5 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 15 Nov 2013 01:11:57 -0200 Subject: Remove short circuit return in favor of simple conditional --- activemodel/lib/active_model/validations/clusivity.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb index fd6cc1edb4..bad9e4f9a9 100644 --- a/activemodel/lib/active_model/validations/clusivity.rb +++ b/activemodel/lib/active_model/validations/clusivity.rb @@ -35,10 +35,13 @@ module ActiveModel # Range#cover? uses the previous logic of comparing a value with the range # endpoints, which is fast but is only accurate on Numeric, Time, or DateTime ranges. def inclusion_method(enumerable) - return :include? unless enumerable.is_a?(Range) - case enumerable.first - when Numeric, Time, DateTime - :cover? + if enumerable.is_a? Range + case enumerable.first + when Numeric, Time, DateTime + :cover? + else + :include? + end else :include? end -- cgit v1.2.3 From b1b9a0aeca879b1c1bc2c8a74f2c9cabd143b9bb Mon Sep 17 00:00:00 2001 From: Lauro Caetano Date: Tue, 3 Dec 2013 12:04:25 -0200 Subject: Typos. return -> returns. [ci skip] --- activemodel/lib/active_model/validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 690856aee1..e19b8e0a9e 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -109,7 +109,7 @@ module ActiveModel deprecated_setup(options) end - # Return the kind for this validator. + # Returns the kind for this validator. # # PresenceValidator.new.kind # => :presence # UniquenessValidator.new.kind # => :uniqueness -- cgit v1.2.3