Permalink
Browse files

Fixes #34 - #update_column with an array value

Monkeypatches ActiveRecord::Sanitization
  • Loading branch information...
danmcclain committed Nov 30, 2012
1 parent 473812f commit 2d42ce44873d7e1009c6075f569f6f267fc5e6b1
Showing with 32 additions and 1 deletion.
  1. +1 −0 lib/postgres_ext/active_record.rb
  2. +30 −0 lib/postgres_ext/active_record/sanitization.rb
  3. +1 −1 spec/models/array_spec.rb
@@ -1,2 +1,3 @@
+require 'postgres_ext/active_record/sanitization'
require 'postgres_ext/active_record/connection_adapters'
require 'postgres_ext/active_record/schema_dumper'
@@ -0,0 +1,30 @@
+require 'active_record/sanitization'
+
+module ActiveRecord
+ module Sanitization
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def sanitize_sql_hash_for_assignment(attrs)
+ attrs.map do |attr, value|
+ "#{connection.quote_column_name(attr)} = #{quote_bound_value(value, attr)}"
+ end.join(', ')
+ end
+
+ def quote_bound_value(value, column = nil, c = connection)
+ if column.present?
+ record_column = self.columns.select {|col| col.name == column}.first
+ c.quote(value, record_column)
+ elsif value.respond_to?(:map) && !value.acts_like?(:string)
+ if value.respond_to?(:empty?) && value.empty?
+ c.quote(nil)
+ else
+ value.map { |v| c.quote(v) }.join(',')
+ end
+ else
+ c.quote(value)
+ end
+ end
+ end
+ end
+end
@@ -109,7 +109,7 @@ class User < ActiveRecord::Base
context '#update_column' do
describe 'setting a value via update_column' do
it 'returns the value set when the record is retrieved' do
- pending #This fails, not sure where to fix this, takes different code path than #update_attribute
+ #pending #This fails, not sure where to fix this, takes different code path than #update_attribute
user = User.create(:nick_names => [])
user.reload

0 comments on commit 2d42ce4

Please sign in to comment.