Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document option forwarding in ActiveRecord::Base.attribute #35685

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions activerecord/lib/active_record/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module ClassMethods
# +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.
Expand Down Expand Up @@ -112,6 +115,16 @@ module ClassMethods
# 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
XrXr marked this conversation as resolved.
Show resolved Hide resolved
#
# ==== Creating Custom Types
#
# Users may also define their own custom types, as long as they respond
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ class CustomPropertiesTest < ActiveRecord::TestCase
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)

Expand Down