aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/helpers
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-02-07 13:52:23 -0700
committerSean Griffin <sean@thoughtbot.com>2015-02-07 13:52:23 -0700
commit631707a572fe14f3bbea2779cc97fcc581048d62 (patch)
tree25646486f0275d24099afb557fc7f4d2180f0caf /activerecord/lib/active_record/type/helpers
parentbdeeca84e34d3e0863a6fd03defb9c95fd0241d9 (diff)
downloadrails-631707a572fe14f3bbea2779cc97fcc581048d62.tar.gz
rails-631707a572fe14f3bbea2779cc97fcc581048d62.tar.bz2
rails-631707a572fe14f3bbea2779cc97fcc581048d62.zip
Push multi-parameter assignement into the types
This allows us to remove `Type::Value#klass`, as it was only used for multi-parameter assignment to reach into the types internals. The relevant type objects now accept a hash in addition to their previous accepted arguments to `type_cast_from_user`. This required minor modifications to the tests, since previously they were relying on the fact that mulit-parameter assignement was reaching into the internals of time zone aware attributes. In reaility, changing those properties at runtime wouldn't change the accessor methods for all other forms of assignment.
Diffstat (limited to 'activerecord/lib/active_record/type/helpers')
-rw-r--r--activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb b/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb
new file mode 100644
index 0000000000..c528d410d3
--- /dev/null
+++ b/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb
@@ -0,0 +1,30 @@
+module ActiveRecord
+ module Type
+ module Helpers
+ class AcceptsMultiparameterTime < Module
+ def initialize(defaults: {})
+ define_method(:type_cast_from_user) do |value|
+ if value.is_a?(Hash)
+ value_from_multiparameter_assignment(value)
+ else
+ super(value)
+ end
+ end
+
+ define_method(:value_from_multiparameter_assignment) do |values_hash|
+ defaults.each do |k, v|
+ values_hash[k] ||= v
+ end
+ return unless values_hash[1] && values_hash[2] && values_hash[3]
+ values = values_hash.sort.map(&:last)
+ ::Time.send(
+ ActiveRecord::Base.default_timezone,
+ *values
+ )
+ end
+ private :value_from_multiparameter_assignment
+ end
+ end
+ end
+ end
+end