aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/attribute/user_provided_default.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/attribute/user_provided_default.rb')
-rw-r--r--activemodel/lib/active_model/attribute/user_provided_default.rb30
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