diff options
author | Lisa Ugray <lisa.ugray@shopify.com> | 2017-10-19 12:45:07 -0400 |
---|---|---|
committer | Lisa Ugray <lisa.ugray@shopify.com> | 2017-11-09 14:29:39 -0500 |
commit | c3675f50d2e59b7fc173d7b332860c4b1a24a726 (patch) | |
tree | 736119c8ea9b683ac465c07e6a640d7e14bbc1b0 /activemodel/lib/active_model/attribute | |
parent | dac7c8844b4d9944eaa0fca98b45ee478cdb7201 (diff) | |
download | rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.tar.gz rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.tar.bz2 rails-c3675f50d2e59b7fc173d7b332860c4b1a24a726.zip |
Move Attribute and AttributeSet to ActiveModel
Use these to back the attributes API. Stop automatically including
ActiveModel::Dirty in ActiveModel::Attributes, and make it optional.
Diffstat (limited to 'activemodel/lib/active_model/attribute')
-rw-r--r-- | activemodel/lib/active_model/attribute/user_provided_default.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/attribute/user_provided_default.rb b/activemodel/lib/active_model/attribute/user_provided_default.rb new file mode 100644 index 0000000000..f274b687d4 --- /dev/null +++ b/activemodel/lib/active_model/attribute/user_provided_default.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "active_model/attribute" + +module ActiveModel + class Attribute # :nodoc: + class UserProvidedDefault < FromUser # :nodoc: + def initialize(name, value, type, database_default) + @user_provided_value = value + super(name, value, type, database_default) + end + + def value_before_type_cast + if user_provided_value.is_a?(Proc) + @memoized_value_before_type_cast ||= user_provided_value.call + else + @user_provided_value + end + end + + def with_type(type) + self.class.new(name, user_provided_value, type, original_attribute) + end + + protected + + attr_reader :user_provided_value + end + end +end |