Permalink
Browse files

Proper type casting for integer arrays

  • Loading branch information...
1 parent fbb9cfc commit 36387c9c754cbd67e0f68005cf60812038139778 @derek-watson derek-watson committed Apr 23, 2012
Showing with 11 additions and 5 deletions.
  1. +1 −1 README.textile
  2. +3 −1 lib/activerecord-postgres-array/string.rb
  3. +3 −1 spec/integration_spec.rb
  4. +3 −2 spec/internal/db/schema.rb
  5. +1 −0 spec/spec_helper.rb
View
2 README.textile
@@ -30,7 +30,7 @@ activerecord-postgres-hstore and activerecord-postgres-array both monkeypatch @A
h2. Current limitations
* Parsing of multi-dimensional postgres array strings is currently not implemented.
-* String and Decimal arrays have been tested, but other array types have not been. Type casting will need to be implemented for booleans, dates, etc
+* String, Decimal and Integer arrays have been tested, but other array types have not been. Type casting will need to be implemented for booleans, dates, etc
h3. Future enhancements
View
4 lib/activerecord-postgres-array/string.rb
@@ -28,9 +28,11 @@ def from_postgres_array(base_type = :string)
if base_type == :decimal
elements.collect(&:to_d)
+ elsif base_type == :integer
+ elements.collect(&:to_i)
else
elements
end
end
end
-end
+end
View
4 spec/integration_spec.rb
@@ -3,10 +3,12 @@
describe Article do
describe ".create" do
it "builds valid arrays" do
- article = Article.create(:languages => ["English", "German"])
+ article = Article.create(:languages => ["English", "German"], :author_ids => [1,2,3])
article.reload
article.languages_before_type_cast.should == "{English,German}"
article.languages.should == ["English", "German"]
+ article.author_ids_before_type_cast.should == "{1,2,3}"
+ article.author_ids.should == [1,2,3]
end
it "escapes single quotes correctly" do
View
5 spec/internal/db/schema.rb
@@ -1,6 +1,7 @@
ActiveRecord::Schema.define do
create_table(:articles, :force => true) do |t|
- t.string :name
- t.string_array :languages
+ t.string :name
+ t.string_array :languages
+ t.integer_array :author_ids
end
end
View
1 spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler'
+require 'active_support/dependencies'
Bundler.require :default, :development

0 comments on commit 36387c9

Please sign in to comment.