diff options
7 files changed, 37 insertions, 85 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 51f6a009db..75154a7975 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -202,12 +202,17 @@ module ActiveRecord if column.nil? ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc) `column_for_attribute` will return a null object for non-existent columns - in Rails 5.0. If you would like to continue to receive `nil`, you should - instead call `model.class.columns_hash[name]` + in Rails 5.0. Use `attribute_exists?` if you need to check for an + attribute's existence. MESSAGE end column end + + # Returns whether or not an attribute exists with the given name. + def attribute_exists?(name) + @attributes.include?(name) + end end # A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>, diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 5be11e6ab9..8a964fb03c 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -13,11 +13,11 @@ module ActiveRecord end def values_before_type_cast - attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast } + attributes.transform_values(&:value_before_type_cast) end def to_hash - initialized_attributes.each_with_object({}) { |(k, v), h| h[k] = v.value } + initialized_attributes.transform_values(&:value) end alias_method :to_h, :to_hash @@ -43,11 +43,7 @@ module ActiveRecord end def initialize_dup(_) - @attributes = attributes.dup - attributes.each do |key, attr| - attributes[key] = attr.dup - end - + @attributes = attributes.transform_values(&:dup) super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index a865c5c310..c916c0795d 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -2,18 +2,9 @@ module ActiveRecord module ConnectionAdapters module PostgreSQL module Cast # :nodoc: - def point_to_string(point) # :nodoc: - "(#{number_for_point(point[0])},#{number_for_point(point[1])})" - end - - def number_for_point(number) - number.to_s.gsub(/\.0$/, '') - end - - def hstore_to_string(object, array_member = false) # :nodoc: + def hstore_to_string(object) # :nodoc: if Hash === object string = object.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ') - string = escape_hstore(string) if array_member string else object @@ -34,28 +25,12 @@ module ActiveRecord end end - def json_to_string(object) # :nodoc: - if Hash === object || Array === object - ActiveSupport::JSON.encode(object) - else - object - end - end - def range_to_string(object) # :nodoc: from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}" end - def string_to_json(string) # :nodoc: - if String === string - ActiveSupport::JSON.decode(string) - else - string - end - end - private HstorePair = begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb index ab1165f301..e12ddd9901 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -10,11 +10,19 @@ module ActiveRecord end def type_cast_from_database(value) - ConnectionAdapters::PostgreSQLColumn.string_to_json(value) + if value.is_a?(::String) + ::ActiveSupport::JSON.decode(value) + else + super + end end def type_cast_for_database(value) - ConnectionAdapters::PostgreSQLColumn.json_to_string(value) + if value.is_a?(::Array) || value.is_a?(::Hash) + ::ActiveSupport::JSON.encode(value) + else + super + end end def accessor diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb index 9b6494867f..bac8b01d6b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb @@ -25,11 +25,17 @@ module ActiveRecord def type_cast_for_database(value) if value.is_a?(::Array) - PostgreSQLColumn.point_to_string(value) + "(#{number_for_point(value[0])},#{number_for_point(value[1])})" else super end end + + private + + def number_for_point(number) + number.to_s.gsub(/\.0$/, '') + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 4caed77952..f9541b437a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -27,17 +27,9 @@ module ActiveRecord else super end - when Array - case sql_type - when 'point' then super(PostgreSQLColumn.point_to_string(value)) - when 'json' then super(PostgreSQLColumn.json_to_string(value)) - else - super(value, array_column(column)) - end when Hash case sql_type when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column) - when 'json' then super(PostgreSQLColumn.json_to_string(value), column) else super end when Float @@ -71,39 +63,29 @@ module ActiveRecord end end - def type_cast(value, column, array_member = false) - return super(value, column) unless column + def type_cast(value, column) + return super unless column case value when Range if /range$/ =~ column.sql_type PostgreSQLColumn.range_to_string(value) else - super(value, column) + super end when NilClass - if column.array && array_member - 'NULL' - elsif column.array + if column.array value else - super(value, column) - end - when Array - case column.sql_type - when 'point' then PostgreSQLColumn.point_to_string(value) - when 'json' then PostgreSQLColumn.json_to_string(value) - else - super(value, array_column(column)) + super end when Hash case column.sql_type - when 'hstore' then PostgreSQLColumn.hstore_to_string(value, array_member) - when 'json' then PostgreSQLColumn.json_to_string(value) - else super(value, column) + when 'hstore' then PostgreSQLColumn.hstore_to_string(value) + else super end else - super(value, column) + super end end @@ -177,26 +159,6 @@ module ActiveRecord super end end - - def array_column(column) - if column.array && !column.respond_to?(:cast_type) - Column.new('', nil, OID::Array.new(AdapterProxyType.new(column, self))) - else - column - end - end - - class AdapterProxyType < SimpleDelegator # :nodoc: - def initialize(column, adapter) - @column = column - @adapter = adapter - super(column) - end - - def type_cast_for_database(value) - @adapter.type_cast(value, @column) - end - end end end end diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index e44afea33a..50135151c2 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -77,7 +77,7 @@ class PostgresqlJSONTest < ActiveRecord::TestCase column = JsonDataType.columns_hash["payload"] data = "{\"a_key\":\"a_value\"}" - hash = column.class.string_to_json data + hash = column.type_cast_from_database(data) assert_equal({'a_key' => 'a_value'}, hash) assert_equal({'a_key' => 'a_value'}, column.type_cast_from_database(data)) |