From 99ec3251c2681b7a7273582e15e5823aedb4b838 Mon Sep 17 00:00:00 2001 From: Sokolov Yura Date: Mon, 5 Jul 2010 16:55:51 +0400 Subject: [PATCH] Make ActiveRecord Arel datatypes hackable [#5047 state:resolved] --- activerecord/lib/active_record/base.rb | 5 +++- .../connection_adapters/abstract/quoting.rb | 8 +++++++ .../active_record/relation/predicate_builder.rb | 21 +++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c868ff3..9ca069f 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1761,14 +1761,17 @@ MSG # an Arel insert/update method. def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys) attrs = {} + connection = self.class.connection attribute_names.each do |name| if (column = column_for_attribute(name)) && (include_primary_key || !column.primary) if include_readonly_attributes || (!include_readonly_attributes && !self.class.readonly_attributes.include?(name)) value = read_attribute(name) - if value && ((self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time))) || value.is_a?(Hash) || value.is_a?(Array)) + if value && self.class.serialized_attributes.has_key?(name) value = value.to_yaml + else + value = connection.arel_attribute_value(value, column) end attrs[self.class.arel_table[name]] = value end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index d7b5bf8..ba501c8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -67,6 +67,14 @@ module ActiveRecord value end.to_s(:db) end + + def arel_attribute_value(value, column) + if value && ((value.acts_like?(:date) || value.acts_like?(:time)) || value.is_a?(Hash) || value.is_a?(Array)) + value.to_yaml + else + value + end + end end end end diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 5cea232..b6fb4e9 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -22,20 +22,23 @@ module ActiveRecord attribute = table[column] || Arel::Attribute.new(table, column) - case value - when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation - values = value.to_a - attribute.in(values) - when Range, Arel::Relation - attribute.in(value) - else - attribute.eq(value) - end + value_arel_predicate(attribute, value) end end predicates.flatten end + def value_arel_predicate(attribute, value) + case value + when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Relation + values = value.to_a + attribute.in(values) + when Range, Arel::Relation + attribute.in(value) + else + attribute.eq(value) + end + end end end -- 1.7.0.4