From 4f3955657736796664aa8d6f6d149ef9b213b058 Mon Sep 17 00:00:00 2001 From: dixpac Date: Sat, 13 May 2017 13:56:37 +0200 Subject: Improving docs for callbacks execution order [ci skip] When define callbacks latest definition on the same callback/method overwrites previous ones. --- activemodel/lib/active_model/callbacks.rb | 3 +++ activemodel/lib/active_model/validations.rb | 3 +++ 2 files changed, 6 insertions(+) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index eac2761433..835e6f7716 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -56,6 +56,9 @@ module ActiveModel # # Would only create the +after_create+ and +before_create+ callback methods in # your class. + # + # NOTE: Calling the same callback multiple times will overwrite previous callback definitions. + # module Callbacks def self.extended(base) #:nodoc: base.class_eval do diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index d460068830..9fcde45167 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -147,6 +147,9 @@ module ActiveModel # or unless: Proc.new { |user| user.signup_step <= 2 }). The # method, proc or string should return or evaluate to a +true+ or +false+ # value. + # + # NOTE: Calling +validate+ multiple times on the same method will overwrite previous definitions. + # def validate(*args, &block) options = args.extract_options! -- cgit v1.2.3 From 40bdbce191ad90dfea43dad51fac5c4726b89392 Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Mon, 15 May 2017 14:17:28 +0000 Subject: Define path with __dir__ ".. with __dir__ we can restore order in the Universe." - by @fxn Related to 5b8738c2df003a96f0e490c43559747618d10f5f --- activemodel/Rakefile | 8 +++----- activemodel/activemodel.gemspec | 2 +- activemodel/lib/active_model.rb | 2 +- activemodel/lib/active_model/validations.rb | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) (limited to 'activemodel') diff --git a/activemodel/Rakefile b/activemodel/Rakefile index c7f97a4258..d60f6d9997 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -1,14 +1,12 @@ require "rake/testtask" -dir = File.dirname(__FILE__) - task default: :test task :package Rake::TestTask.new do |t| t.libs << "test" - t.test_files = Dir.glob("#{dir}/test/cases/**/*_test.rb") + t.test_files = Dir.glob("#{__dir__}/test/cases/**/*_test.rb") t.warning = true t.verbose = true t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION) @@ -16,8 +14,8 @@ end namespace :test do task :isolated do - Dir.glob("#{dir}/test/**/*_test.rb").all? do |file| - sh(Gem.ruby, "-w", "-I#{dir}/lib", "-I#{dir}/test", file) + Dir.glob("#{__dir__}/test/**/*_test.rb").all? do |file| + sh(Gem.ruby, "-w", "-I#{__dir__}/lib", "-I#{__dir__}/test", file) end || raise("Failures") end end diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index fd715f6ba9..43f1e09c77 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -1,4 +1,4 @@ -version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip +version = File.read(File.expand_path("../RAILS_VERSION", __dir__)).strip Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 2389c858d5..a2892e9ea9 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -68,5 +68,5 @@ module ActiveModel end ActiveSupport.on_load(:i18n) do - I18n.load_path << File.dirname(__FILE__) + "/active_model/locale/en.yml" + I18n.load_path << File.expand_path("active_model/locale/en.yml", __dir__) end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 9fcde45167..1f14a068d1 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -435,4 +435,4 @@ module ActiveModel end end -Dir[File.dirname(__FILE__) + "/validations/*.rb"].each { |file| require file } +Dir[File.expand_path("validations/*.rb", __dir__)].each { |file| require file } -- cgit v1.2.3 From f4f6c512b9bee6ebf3fc27c2d793e715cabb630b Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Fri, 26 May 2017 18:33:38 -0400 Subject: Fix broken RDoc formatting [ci skip] --- activemodel/lib/active_model/validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 1a4d13f2d0..6c11981e1d 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -82,7 +82,7 @@ module ActiveModel # end # # It can be useful to access the class that is using that validator when there are prerequisites such - # as an +attr_accessor+ being present. This class is accessible via +options[:class]+ in the constructor. + # as an +attr_accessor+ being present. This class is accessible via options[:class] in the constructor. # To setup your validator override the constructor. # # class MyValidator < ActiveModel::Validator -- cgit v1.2.3 From d83b8e65102d625c9024cd9a2727a10b0ef83b79 Mon Sep 17 00:00:00 2001 From: Bradley Priest Date: Sat, 27 May 2017 21:47:07 +0800 Subject: Fix regression in Numericality validator where extra decimal places on a user input for a decimal column were ignored by numerically validations --- activemodel/CHANGELOG.md | 5 +++++ activemodel/lib/active_model/validations/numericality.rb | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index e707a65147..7483704212 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix regression in numericality validator when comparing Decimal and Float input + values with more scale than the schema. + + *Bradley Priest* + * Fix methods `#keys`, `#values` in `ActiveModel::Errors`. Change `#keys` to only return the keys that don't have empty messages. diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index b82c85ddf4..fb053a4c4e 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -36,7 +36,9 @@ module ActiveModel return end - unless raw_value.is_a?(Numeric) + if raw_value.is_a?(Numeric) + value = raw_value + else value = parse_raw_value_as_a_number(raw_value) end -- cgit v1.2.3 From 1c275d812f35f53f93cd96184a4f319983766cc5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 29 May 2017 18:01:50 +0200 Subject: Add option for class_attribute default (#29270) * Allow a default value to be declared for class_attribute * Convert to using class_attribute default rather than explicit setter * Removed instance_accessor option by mistake * False is a valid default value * Documentation --- activemodel/lib/active_model/attribute_methods.rb | 5 ++--- activemodel/lib/active_model/serializers/json.rb | 3 +-- activemodel/lib/active_model/validations.rb | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index b5c0b43b61..b3b39bf7ae 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -68,9 +68,8 @@ module ActiveModel CALL_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?]?\z/ included do - class_attribute :attribute_aliases, :attribute_method_matchers, instance_writer: false - self.attribute_aliases = {} - self.attribute_method_matchers = [ClassMethods::AttributeMethodMatcher.new] + class_attribute :attribute_aliases, instance_writer: false, default: {} + class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ] end module ClassMethods diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb index a9d92eb92a..205b84ddb4 100644 --- a/activemodel/lib/active_model/serializers/json.rb +++ b/activemodel/lib/active_model/serializers/json.rb @@ -10,8 +10,7 @@ module ActiveModel included do extend ActiveModel::Naming - class_attribute :include_root_in_json, instance_writer: false - self.include_root_in_json = false + class_attribute :include_root_in_json, instance_writer: false, default: false end # Returns a hash representing the model. Some configuration can be diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 1f14a068d1..ae1d69f685 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -49,8 +49,7 @@ module ActiveModel private :validation_context= define_callbacks :validate, scope: :name - class_attribute :_validators, instance_writer: false - self._validators = Hash.new { |h, k| h[k] = [] } + class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] } end module ClassMethods -- cgit v1.2.3 From ea2850b96a97d1c454d1f6ca0c8c7958cc63ea70 Mon Sep 17 00:00:00 2001 From: Viktor Fonic Date: Wed, 31 May 2017 10:21:51 +0800 Subject: Docs: Fix output representation [ci skip] The output of two string attributes is displayed differently in the docs. Standardize the output by always showing it as a comment. --- activemodel/lib/active_model/attribute_assignment.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_assignment.rb b/activemodel/lib/active_model/attribute_assignment.rb index ee130df989..930e89d611 100644 --- a/activemodel/lib/active_model/attribute_assignment.rb +++ b/activemodel/lib/active_model/attribute_assignment.rb @@ -19,10 +19,10 @@ module ActiveModel # cat = Cat.new # cat.assign_attributes(name: "Gorby", status: "yawning") # cat.name # => 'Gorby' - # cat.status => 'yawning' + # cat.status # => 'yawning' # cat.assign_attributes(status: "sleeping") # cat.name # => 'Gorby' - # cat.status => 'sleeping' + # cat.status # => 'sleeping' def assign_attributes(new_attributes) if !new_attributes.respond_to?(:stringify_keys) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument." -- cgit v1.2.3 From 285cba022ce37977f977376a20ca866197ef33bd Mon Sep 17 00:00:00 2001 From: shotat Date: Wed, 14 Jun 2017 09:13:26 +0900 Subject: enhance active model assignment --- activemodel/lib/active_model/attribute_assignment.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_assignment.rb b/activemodel/lib/active_model/attribute_assignment.rb index 930e89d611..a0a6de8dd7 100644 --- a/activemodel/lib/active_model/attribute_assignment.rb +++ b/activemodel/lib/active_model/attribute_assignment.rb @@ -42,8 +42,9 @@ module ActiveModel end def _assign_attribute(k, v) - if respond_to?("#{k}=") - public_send("#{k}=", v) + setter = "#{k}=" + if respond_to?(setter) + public_send(setter, v) else raise UnknownAttributeError.new(self, k) end -- cgit v1.2.3 From 2396f79fec607657b61eef329a98c4debcaa88c7 Mon Sep 17 00:00:00 2001 From: shotat Date: Wed, 14 Jun 2017 17:49:54 +0900 Subject: freeze string --- activemodel/lib/active_model/attribute_assignment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_assignment.rb b/activemodel/lib/active_model/attribute_assignment.rb index a0a6de8dd7..f521e74f32 100644 --- a/activemodel/lib/active_model/attribute_assignment.rb +++ b/activemodel/lib/active_model/attribute_assignment.rb @@ -42,7 +42,7 @@ module ActiveModel end def _assign_attribute(k, v) - setter = "#{k}=" + setter = "#{k}=".freeze if respond_to?(setter) public_send(setter, v) else -- cgit v1.2.3 From 6673cf7071094e87d473459452a2d0e4c2ccfebe Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Sun, 11 Jun 2017 15:59:23 +0300 Subject: Use `require_relative` instead of `require` with full path --- activemodel/bin/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/bin/test b/activemodel/bin/test index a7beb14b27..470ce93f10 100755 --- a/activemodel/bin/test +++ b/activemodel/bin/test @@ -1,4 +1,4 @@ #!/usr/bin/env ruby COMPONENT_ROOT = File.expand_path("..", __dir__) -require File.expand_path("../tools/test", COMPONENT_ROOT) +require_relative "../../tools/test" -- cgit v1.2.3 From b2999d631fb6b61c62b975e4cfe86727b42c1a70 Mon Sep 17 00:00:00 2001 From: shotat Date: Thu, 15 Jun 2017 11:45:15 +0900 Subject: add frozen string literal comment --- activemodel/lib/active_model/attribute_assignment.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/attribute_assignment.rb b/activemodel/lib/active_model/attribute_assignment.rb index f521e74f32..aa931119ff 100644 --- a/activemodel/lib/active_model/attribute_assignment.rb +++ b/activemodel/lib/active_model/attribute_assignment.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "active_support/core_ext/hash/keys" module ActiveModel @@ -42,7 +44,7 @@ module ActiveModel end def _assign_attribute(k, v) - setter = "#{k}=".freeze + setter = :"#{k}=" if respond_to?(setter) public_send(setter, v) else -- cgit v1.2.3