Skip to content

Commit

Permalink
Fix multidimensional PG arrays containing non-string items
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Aug 7, 2013
1 parent 4343e2d commit 806bc2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix multidimensional PG arrays containing non-string items.

*Yves Senn*

* Load fixtures from linked folders.

*Kassio Borges*
Expand Down
Expand Up @@ -115,7 +115,7 @@ def cidr_to_string(object)
end

def string_to_array(string, oid)
parse_pg_array(string).map{|val| oid.type_cast val}
parse_pg_array(string).map {|val| type_cast_array(oid, val)}
end

private
Expand Down Expand Up @@ -146,6 +146,14 @@ def quote_and_escape(value)
"\"#{value.gsub(/"/,"\\\"")}\""
end
end

def type_cast_array(oid, value)
if ::Array === value
value.map {|item| type_cast_array(oid, item)}
else
oid.type_cast value
end
end
end
end
end
Expand Down
33 changes: 19 additions & 14 deletions activerecord/test/cases/adapters/postgresql/array_test.rb
Expand Up @@ -12,7 +12,8 @@ def setup
@connection = ActiveRecord::Base.connection
@connection.transaction do
@connection.create_table('pg_arrays') do |t|
t.string 'tags', :array => true
t.string 'tags', array: true
t.integer 'ratings', array: true
end
end
@column = PgArray.columns.find { |c| c.name == 'tags' }
Expand Down Expand Up @@ -78,28 +79,32 @@ def test_select
assert_equal(['1','2','3'], x.tags)
end

def test_multi_dimensional
assert_cycle([['1','2'],['2','3']])
def test_multi_dimensional_with_strings
assert_cycle(:tags, [[['1'], ['2']], [['2'], ['3']]])
end

def test_multi_dimensional_with_integers
assert_cycle(:ratings, [[[1], [7]], [[8], [10]]])
end

def test_strings_with_quotes
assert_cycle(['this has','some "s that need to be escaped"'])
assert_cycle(:tags, ['this has','some "s that need to be escaped"'])
end

def test_strings_with_commas
assert_cycle(['this,has','many,values'])
assert_cycle(:tags, ['this,has','many,values'])
end

def test_strings_with_array_delimiters
assert_cycle(['{','}'])
assert_cycle(:tags, ['{','}'])
end

def test_strings_with_null_strings
assert_cycle(['NULL','NULL'])
assert_cycle(:tags, ['NULL','NULL'])
end

def test_contains_nils
assert_cycle(['1',nil,nil])
assert_cycle(:tags, ['1',nil,nil])
end

def test_insert_fixture
Expand All @@ -109,17 +114,17 @@ def test_insert_fixture
end

private
def assert_cycle array
def assert_cycle field, array
# test creation
x = PgArray.create!(:tags => array)
x = PgArray.create!(field => array)
x.reload
assert_equal(array, x.tags)
assert_equal(array, x.public_send(field))

# test updating
x = PgArray.create!(:tags => [])
x.tags = array
x = PgArray.create!(field => [])
x.public_send("#{field}=", array)
x.save!
x.reload
assert_equal(array, x.tags)
assert_equal(array, x.public_send(field))
end
end

0 comments on commit 806bc2a

Please sign in to comment.