This project is archived and is in readonly mode.

#5047 ✓stale
Sokolov Yura

Add hooks for new data types

Reported by Sokolov Yura | July 5th, 2010 @ 01:55 PM

I work on plugin, which allows use of PostgreSQL array type.
While modifying it to work under Rails 3, i found two places, where need for "monkey patching" was too big. Both places are about deciding what to insert into SQL depending on value type.

First place is ActiveRecord::Base#arel_attributes_values .
It would be perfect, if rows

  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))
    value = value.to_yaml
  end

would be refactored into hookable method. I suggest, it could be method in the ActiveRecord::ConnectionAdapters::AbstractAdapter

  if value && self.class.serialized_attributes.has_key?(name)
    value = value.to_yaml
  else
    value = self.class.connection.arel_attribute_value(value, column)
  ends
  module ActiveRecord
    module ConnectionAdapters
      class AbstractAdapter
        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

or simply in separated ActiveRecord::Base method, depended on column

Second is ActiveRecord::PredicateBuilder#build_from_hash
Could following lines be refactored in separate method:

  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

Comments and changes to this ticket

  • Sokolov Yura
  • Sokolov Yura

    Sokolov Yura July 7th, 2010 @ 09:17 AM

    • Tag changed from activerecord datatypes to activerecord datatypes, patched
  • Sokolov Yura

    Sokolov Yura July 7th, 2010 @ 09:18 AM

    • Tag changed from activerecord datatypes, patched to activerecord datatypes, patch
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:36 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages