Skip to content

Commit

Permalink
Merge pull request #11 from derek-watson/master
Browse files Browse the repository at this point in the history
Added proper typecasting for integer arrays
  • Loading branch information
tlconnor committed Jun 15, 2012
2 parents fbb9cfc + 36387c9 commit bbcfa9e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.textile
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/activerecord-postgres-array/string.rb
Expand Up @@ -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
4 changes: 3 additions & 1 deletion spec/integration_spec.rb
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions 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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler'
require 'active_support/dependencies'

Bundler.require :default, :development

Expand Down

0 comments on commit bbcfa9e

Please sign in to comment.