diff options
author | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2019-03-20 15:38:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-20 15:38:47 -0400 |
commit | e04af4eb18503c7bd41b73d0cb6eecdff1893dc6 (patch) | |
tree | b542e9d5965c0f5f0c4740d35e1bdca47f3af0f0 | |
parent | 6b5130720eee785b45dfc0fc5044224cdc2b6c98 (diff) | |
parent | 15b11ce505b9e94031c640d8488d6237f860d08b (diff) | |
download | rails-e04af4eb18503c7bd41b73d0cb6eecdff1893dc6.tar.gz rails-e04af4eb18503c7bd41b73d0cb6eecdff1893dc6.tar.bz2 rails-e04af4eb18503c7bd41b73d0cb6eecdff1893dc6.zip |
Merge pull request #35685 from XrXr/attribute-forwarding
Document option forwarding in ActiveRecord::Base.attribute
-rw-r--r-- | activerecord/lib/active_record/attributes.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/attributes_test.rb | 11 |
2 files changed, 24 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 35150889d9..7cf421c184 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -41,6 +41,9 @@ module ActiveRecord # +range+ (PostgreSQL only) specifies that the type should be a range (see the # examples below). # + # When using a symbol for +cast_type+, extra options are forwarded to the + # constructor of the type object. + # # ==== Examples # # The type detected by Active Record can be overridden. @@ -112,6 +115,16 @@ module ActiveRecord # my_float_range: 1.0..3.5 # } # + # Passing options to the type constructor + # + # # app/models/my_model.rb + # class MyModel < ActiveRecord::Base + # attribute :small_int, :integer, limit: 2 + # end + # + # MyModel.create(small_int: 65537) + # # => Error: 65537 is out of range for the limit of two bytes + # # ==== Creating Custom Types # # Users may also define their own custom types, as long as they respond diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 2632aec7ab..a6fb9f0af7 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -56,6 +56,17 @@ module ActiveRecord assert_equal 255, UnoverloadedType.type_for_attribute("overloaded_string_with_limit").limit end + test "extra options are forwarded to the type caster constructor" do + klass = Class.new(OverloadedType) do + attribute :starts_at, :datetime, precision: 3, limit: 2, scale: 1 + end + + starts_at_type = klass.type_for_attribute(:starts_at) + assert_equal 3, starts_at_type.precision + assert_equal 2, starts_at_type.limit + assert_equal 1, starts_at_type.scale + end + test "nonexistent attribute" do data = OverloadedType.new(non_existent_decimal: 1) |