diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2016-12-29 20:09:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-29 20:09:43 +0100 |
commit | ca3eb2c1569a86e9c7eee2e9877dd773797a76b4 (patch) | |
tree | 016b5f2247d6ad4d95f1b1f05c897b5472abb7bf /activemodel | |
parent | 654704247eba742e139cfaa8d1385f13605d9e12 (diff) | |
parent | c491bf012948383632e53f874c552041a6e23b36 (diff) | |
download | rails-ca3eb2c1569a86e9c7eee2e9877dd773797a76b4.tar.gz rails-ca3eb2c1569a86e9c7eee2e9877dd773797a76b4.tar.bz2 rails-ca3eb2c1569a86e9c7eee2e9877dd773797a76b4.zip |
Merge branch 'master' into fix_26964
Diffstat (limited to 'activemodel')
29 files changed, 97 insertions, 118 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 9c53e2ecec..853a1e7d9d 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,7 @@ +* Moved DecimalWithoutScale, Text, and UnsignedInteger from Active Model to Active Record + + *Iain Beeston* + * Allow indifferent access in `ActiveModel::Errors`. `#include?`, `#has_key?`, `#key?`, `#delete` and `#full_messages_for`. diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 1441b146f8..09825cf861 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -334,12 +334,11 @@ module ActiveModel }.tap { |mod| include mod } end - protected - def instance_method_already_implemented?(method_name) #:nodoc: + private + def instance_method_already_implemented?(method_name) generated_attribute_methods.method_defined?(method_name) end - private # The methods +method_missing+ and +respond_to?+ of this module are # invoked often in a typical rails, both of which invoke the method # +matched_attribute_method+. The latter method iterates through an @@ -349,11 +348,11 @@ module ActiveModel # used to alleviate the GC, which ultimately also speeds up the app # significantly (in our case our test suite finishes 10% faster with # this cache). - def attribute_method_matchers_cache #:nodoc: + def attribute_method_matchers_cache @attribute_method_matchers_cache ||= Concurrent::Map.new(initial_capacity: 4) end - def attribute_method_matchers_matching(method_name) #:nodoc: + def attribute_method_matchers_matching(method_name) attribute_method_matchers_cache.compute_if_absent(method_name) do # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix # will match every time. @@ -365,7 +364,7 @@ module ActiveModel # Define a method `name` in `mod` that dispatches to `send` # using the given `extra` args. This falls back on `define_method` # and `send` if the given names cannot be compiled. - def define_proxy_call(include_private, mod, name, send, *extra) #:nodoc: + def define_proxy_call(include_private, mod, name, send, *extra) defn = if NAME_COMPILABLE_REGEXP.match?(name) "def #{name}(*args)" else @@ -458,12 +457,11 @@ module ActiveModel end end - protected - def attribute_method?(attr_name) #:nodoc: + private + def attribute_method?(attr_name) respond_to_without_attributes?(:attributes) && attributes.include?(attr_name) end - private # Returns a struct representing the matching attribute method. # The struct's attributes are prefix, base and suffix. def matched_attribute_method(method_name) diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index 283090e380..e99bfab6fd 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -122,19 +122,19 @@ module ActiveModel private - def _define_before_model_callback(klass, callback) #:nodoc: + def _define_before_model_callback(klass, callback) klass.define_singleton_method("before_#{callback}") do |*args, &block| set_callback(:"#{callback}", :before, *args, &block) end end - def _define_around_model_callback(klass, callback) #:nodoc: + def _define_around_model_callback(klass, callback) klass.define_singleton_method("around_#{callback}") do |*args, &block| set_callback(:"#{callback}", :around, *args, &block) end end - def _define_after_model_callback(klass, callback) #:nodoc: + def _define_after_model_callback(klass, callback) klass.define_singleton_method("after_#{callback}") do |*args, &block| options = args.extract_options! options[:prepend] = true diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 5ee9413cff..9df4ca51fe 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -419,16 +419,23 @@ module ActiveModel I18n.translate(key, options) end - def marshal_dump + def marshal_dump # :nodoc: [@base, without_default_proc(@messages), without_default_proc(@details)] end - def marshal_load(array) + def marshal_load(array) # :nodoc: @base, @messages, @details = array apply_default_array(@messages) apply_default_array(@details) end + def init_with(coder) # :nodoc: + coder.map.each { |k, v| instance_variable_set(:"@#{k}", v) } + @details ||= {} + apply_default_array(@messages) + apply_default_array(@details) + end + private def normalize_message(attribute, message, options) case message diff --git a/activemodel/lib/active_model/forbidden_attributes_protection.rb b/activemodel/lib/active_model/forbidden_attributes_protection.rb index d2c6a89cc2..45ab8a2ca1 100644 --- a/activemodel/lib/active_model/forbidden_attributes_protection.rb +++ b/activemodel/lib/active_model/forbidden_attributes_protection.rb @@ -15,7 +15,7 @@ module ActiveModel end module ForbiddenAttributesProtection # :nodoc: - protected + private def sanitize_for_mass_assignment(attributes) if attributes.respond_to?(:permitted?) raise ActiveModel::ForbiddenAttributesError if !attributes.permitted? diff --git a/activemodel/lib/active_model/model.rb b/activemodel/lib/active_model/model.rb index e683050787..945a5402a3 100644 --- a/activemodel/lib/active_model/model.rb +++ b/activemodel/lib/active_model/model.rb @@ -2,10 +2,10 @@ module ActiveModel # == Active \Model \Basic \Model # # Includes the required interface for an object to interact with - # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules. + # Action Pack and Action View, using different Active Model modules. # It includes model name introspections, conversions, translations and # validations. Besides that, it allows you to initialize the object with a - # hash of attributes, pretty much like <tt>ActiveRecord</tt> does. + # hash of attributes, pretty much like Active Record does. # # A minimal implementation could be: # diff --git a/activemodel/lib/active_model/type.rb b/activemodel/lib/active_model/type.rb index 0d3349e236..b8e6d2376b 100644 --- a/activemodel/lib/active_model/type.rb +++ b/activemodel/lib/active_model/type.rb @@ -7,14 +7,11 @@ require "active_model/type/boolean" require "active_model/type/date" require "active_model/type/date_time" require "active_model/type/decimal" -require "active_model/type/decimal_without_scale" require "active_model/type/float" require "active_model/type/immutable_string" require "active_model/type/integer" require "active_model/type/string" -require "active_model/type/text" require "active_model/type/time" -require "active_model/type/unsigned_integer" require "active_model/type/registry" @@ -53,7 +50,6 @@ module ActiveModel register(:immutable_string, Type::ImmutableString) register(:integer, Type::Integer) register(:string, Type::String) - register(:text, Type::Text) register(:time, Type::Time) end end diff --git a/activemodel/lib/active_model/type/decimal_without_scale.rb b/activemodel/lib/active_model/type/decimal_without_scale.rb deleted file mode 100644 index 985e1038ed..0000000000 --- a/activemodel/lib/active_model/type/decimal_without_scale.rb +++ /dev/null @@ -1,11 +0,0 @@ -require "active_model/type/big_integer" - -module ActiveModel - module Type - class DecimalWithoutScale < BigInteger # :nodoc: - def type - :decimal - end - end - end -end diff --git a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb index facea12704..f783d286c5 100644 --- a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb +++ b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb @@ -1,7 +1,7 @@ module ActiveModel module Type - module Helpers - class AcceptsMultiparameterTime < Module # :nodoc: + module Helpers # :nodoc: all + class AcceptsMultiparameterTime < Module def initialize(defaults: {}) define_method(:cast) do |value| if value.is_a?(Hash) diff --git a/activemodel/lib/active_model/type/helpers/mutable.rb b/activemodel/lib/active_model/type/helpers/mutable.rb index 4dddbe4e5e..f3a17a1698 100644 --- a/activemodel/lib/active_model/type/helpers/mutable.rb +++ b/activemodel/lib/active_model/type/helpers/mutable.rb @@ -1,7 +1,7 @@ module ActiveModel module Type - module Helpers - module Mutable # :nodoc: + module Helpers # :nodoc: all + module Mutable def cast(value) deserialize(serialize(value)) end diff --git a/activemodel/lib/active_model/type/helpers/numeric.rb b/activemodel/lib/active_model/type/helpers/numeric.rb index 98533f8771..275822b738 100644 --- a/activemodel/lib/active_model/type/helpers/numeric.rb +++ b/activemodel/lib/active_model/type/helpers/numeric.rb @@ -1,7 +1,7 @@ module ActiveModel module Type - module Helpers - module Numeric # :nodoc: + module Helpers # :nodoc: all + module Numeric def cast(value) value = \ case value diff --git a/activemodel/lib/active_model/type/helpers/time_value.rb b/activemodel/lib/active_model/type/helpers/time_value.rb index ad78fd49ec..e57a52104b 100644 --- a/activemodel/lib/active_model/type/helpers/time_value.rb +++ b/activemodel/lib/active_model/type/helpers/time_value.rb @@ -2,8 +2,8 @@ require "active_support/core_ext/time/zones" module ActiveModel module Type - module Helpers - module TimeValue # :nodoc: + module Helpers # :nodoc: all + module TimeValue def serialize(value) value = apply_seconds_precision(value) @@ -34,7 +34,7 @@ module ActiveModel return value unless precision && value.respond_to?(:usec) number_of_insignificant_digits = 6 - precision round_power = 10**number_of_insignificant_digits - value.change(usec: value.usec / round_power * round_power) + value.change(usec: value.usec - value.usec % round_power) end def type_cast_for_schema(value) diff --git a/activemodel/lib/active_model/type/integer.rb b/activemodel/lib/active_model/type/integer.rb index 41dd655a5c..230e45ba60 100644 --- a/activemodel/lib/active_model/type/integer.rb +++ b/activemodel/lib/active_model/type/integer.rb @@ -29,6 +29,8 @@ module ActiveModel result end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected attr_reader :range diff --git a/activemodel/lib/active_model/type/registry.rb b/activemodel/lib/active_model/type/registry.rb index d25f1a129e..2d5dd366eb 100644 --- a/activemodel/lib/active_model/type/registry.rb +++ b/activemodel/lib/active_model/type/registry.rb @@ -21,6 +21,8 @@ module ActiveModel end end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected attr_reader :registrations @@ -55,6 +57,8 @@ module ActiveModel type_name == name end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected attr_reader :name, :block diff --git a/activemodel/lib/active_model/type/text.rb b/activemodel/lib/active_model/type/text.rb deleted file mode 100644 index 7c0d647706..0000000000 --- a/activemodel/lib/active_model/type/text.rb +++ /dev/null @@ -1,11 +0,0 @@ -require "active_model/type/string" - -module ActiveModel - module Type - class Text < String # :nodoc: - def type - :text - end - end - end -end diff --git a/activemodel/lib/active_model/type/unsigned_integer.rb b/activemodel/lib/active_model/type/unsigned_integer.rb deleted file mode 100644 index 288fa23efe..0000000000 --- a/activemodel/lib/active_model/type/unsigned_integer.rb +++ /dev/null @@ -1,15 +0,0 @@ -module ActiveModel - module Type - class UnsignedInteger < Integer # :nodoc: - private - - def max_value - super * 2 - end - - def min_value - 0 - end - end - end -end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index a1f7c971db..d460068830 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -399,14 +399,14 @@ module ActiveModel # end alias :read_attribute_for_validation :send - protected + private - def run_validations! #:nodoc: + def run_validations! _run_validate_callbacks errors.empty? end - def raise_validation_error + def raise_validation_error # :doc: raise(ValidationError.new(self)) end end diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index 9826c2fe9c..e11005b9ba 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -56,6 +56,8 @@ module ActiveModel klass.send(:attr_writer, *attr_writers) end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected attr_reader :attributes diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb index a201f72ed0..70bc1a0624 100644 --- a/activemodel/lib/active_model/validations/callbacks.rb +++ b/activemodel/lib/active_model/validations/callbacks.rb @@ -104,10 +104,10 @@ module ActiveModel end end - protected + private # Overwrite run validations to include callbacks. - def run_validations! #:nodoc: + def run_validations! _run_validation_callbacks { super } end end diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 48e0e5c9f6..2297faaee5 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -61,29 +61,29 @@ module ActiveModel end end - protected + private - def is_number?(raw_value) + def is_number?(raw_value) # :doc: !parse_raw_value_as_a_number(raw_value).nil? rescue ArgumentError, TypeError false end - def parse_raw_value_as_a_number(raw_value) + def parse_raw_value_as_a_number(raw_value) # :doc: Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/ end - def is_integer?(raw_value) + def is_integer?(raw_value) # :doc: /\A[+-]?\d+\z/ === raw_value.to_s end - def filtered_options(value) + def filtered_options(value) # :doc: filtered = options.except(*RESERVED_OPTIONS) filtered[:value] = value filtered end - def allow_only_integer?(record) + def allow_only_integer?(record) # :doc: case options[:only_integer] when Symbol record.send(options[:only_integer]) @@ -94,12 +94,10 @@ module ActiveModel end end - private - - def record_attribute_changed_in_place?(record, attr_name) - record.respond_to?(:attribute_changed_in_place?) && - record.attribute_changed_in_place?(attr_name.to_s) - end + def record_attribute_changed_in_place?(record, attr_name) + record.respond_to?(:attribute_changed_in_place?) && + record.attribute_changed_in_place?(attr_name.to_s) + end end module HelperMethods diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index f95f44de61..0ce5935f3a 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -148,15 +148,15 @@ module ActiveModel validates(*(attributes << options)) end - protected + private # When creating custom validators, it might be useful to be able to specify # additional default keys. This can be done by overwriting this method. - def _validates_default_keys # :nodoc: + def _validates_default_keys [:if, :unless, :on, :allow_blank, :allow_nil , :strict] end - def _parse_validates_options(options) # :nodoc: + def _parse_validates_options(options) case options when TrueClass {} diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb index 8eb389d331..fa41d1b95f 100644 --- a/activemodel/test/cases/attribute_assignment_test.rb +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -16,6 +16,8 @@ class AttributeAssignmentTest < ActiveModel::TestCase raise ErrorFromAttributeWriter end + # TODO Change this to private once we've dropped Ruby 2.2 support. + # Workaround for Ruby 2.2 "private attribute?" warning. protected attr_writer :metadata diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index fc840bf192..e6ba06301d 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require "yaml" class ErrorsTest < ActiveModel::TestCase class Person @@ -365,4 +366,24 @@ class ErrorsTest < ActiveModel::TestCase assert_equal errors.messages, serialized.messages assert_equal errors.details, serialized.details end + + test "errors are backward compatible with the Rails 4.2 format" do + yaml = <<-CODE.strip_heredoc + --- !ruby/object:ActiveModel::Errors + base: &1 !ruby/object:ErrorsTest::Person + errors: !ruby/object:ActiveModel::Errors + base: *1 + messages: {} + messages: {} + CODE + + errors = YAML.load(yaml) + errors.add(:name, :invalid) + assert_equal({ name: ["is invalid"] }, errors.messages) + assert_equal({ name: [{ error: :invalid }] }, errors.details) + + errors.clear + assert_equal({}, errors.messages) + assert_equal({}, errors.details) + end end diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 9423df2c09..77ce86f392 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -179,7 +179,7 @@ class SecurePasswordTest < ActiveModel::TestCase test "setting a nil password should clear an existing password" do @existing_user.password = nil - assert_equal nil, @existing_user.password_digest + assert_nil @existing_user.password_digest end test "authenticate" do diff --git a/activemodel/test/cases/type/binary_test.rb b/activemodel/test/cases/type/binary_test.rb index e6a32dbeec..e9c2ccfca4 100644 --- a/activemodel/test/cases/type/binary_test.rb +++ b/activemodel/test/cases/type/binary_test.rb @@ -6,7 +6,7 @@ module ActiveModel class BinaryTest < ActiveModel::TestCase def test_type_cast_binary type = Type::Binary.new - assert_equal nil, type.cast(nil) + assert_nil type.cast(nil) assert_equal "1", type.cast("1") assert_equal 1, type.cast(1) end diff --git a/activemodel/test/cases/type/date_test.rb b/activemodel/test/cases/type/date_test.rb index 44e20a327b..0cc90e99d3 100644 --- a/activemodel/test/cases/type/date_test.rb +++ b/activemodel/test/cases/type/date_test.rb @@ -6,10 +6,10 @@ module ActiveModel class DateTest < ActiveModel::TestCase def test_type_cast_date type = Type::Date.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast("") - assert_equal nil, type.cast(" ") - assert_equal nil, type.cast("ABC") + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast(" ") + assert_nil type.cast("ABC") date_string = ::Time.now.utc.strftime("%F") assert_equal date_string, type.cast(date_string).strftime("%F") diff --git a/activemodel/test/cases/type/date_time_test.rb b/activemodel/test/cases/type/date_time_test.rb index fb82260d2b..75a7fc686e 100644 --- a/activemodel/test/cases/type/date_time_test.rb +++ b/activemodel/test/cases/type/date_time_test.rb @@ -6,10 +6,10 @@ module ActiveModel class DateTimeTest < ActiveModel::TestCase def test_type_cast_datetime_and_timestamp type = Type::DateTime.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast("") - assert_equal nil, type.cast(" ") - assert_equal nil, type.cast("ABC") + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast(" ") + assert_nil type.cast("ABC") datetime_string = ::Time.now.utc.strftime("%FT%T") assert_equal datetime_string, type.cast(datetime_string).strftime("%FT%T") diff --git a/activemodel/test/cases/type/time_test.rb b/activemodel/test/cases/type/time_test.rb index a6a79833e6..0cc4d33caa 100644 --- a/activemodel/test/cases/type/time_test.rb +++ b/activemodel/test/cases/type/time_test.rb @@ -6,9 +6,9 @@ module ActiveModel class TimeTest < ActiveModel::TestCase def test_type_cast_time type = Type::Time.new - assert_equal nil, type.cast(nil) - assert_equal nil, type.cast("") - assert_equal nil, type.cast("ABC") + assert_nil type.cast(nil) + assert_nil type.cast("") + assert_nil type.cast("ABC") time_string = ::Time.now.utc.strftime("%T") assert_equal time_string, type.cast(time_string).strftime("%T") diff --git a/activemodel/test/cases/type/unsigned_integer_test.rb b/activemodel/test/cases/type/unsigned_integer_test.rb deleted file mode 100644 index 026cb08a06..0000000000 --- a/activemodel/test/cases/type/unsigned_integer_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "cases/helper" -require "active_model/type" - -module ActiveModel - module Type - class UnsignedIntegerTest < ActiveModel::TestCase - test "unsigned int max value is in range" do - assert_equal(4294967295, UnsignedInteger.new.serialize(4294967295)) - end - - test "minus value is out of range" do - assert_raises(ActiveModel::RangeError) do - UnsignedInteger.new.serialize(-1) - end - end - end - end -end |