activerecord-postgres-array 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,12 @@ end
25
25
 
26
26
  h3. Compatibility with activerecord-postgres-hstore
27
27
 
28
+ **Note**
29
+
30
+ As of activerecord-postgres-hstore '0.7.0', @ActiveRecord::Base#arel_attributes_value@ is not monkey patched and therefore does not interfere with loading of this gem.
31
+
32
+ If you are using '0.6.0', or earlier, of activerecord-postgres-hstore you must use this gem's version '0.0.8' and the following still applies:
33
+
28
34
  activerecord-postgres-hstore and activerecord-postgres-array both monkeypatch @ActiveRecord::Base#arel_attributes_values@, which leads to problems if these gems are used together. This gem is aware of activerecord-postgres-hstore and incorporates it in the monkeypatch. However, it is important that activerecord-postgres-array is loaded _after_ activerecord-postgres-hstore for this to work.
29
35
 
30
36
  h2. Current limitations
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "activerecord-postgres-array"
3
- s.version = "0.0.8"
3
+ s.version = "0.0.9"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Tim Connor"]
@@ -16,8 +16,6 @@ module ActiveRecord
16
16
  value = read_attribute(name)
17
17
  if column.type.to_s =~ /_array$/ && value && value.is_a?(Array)
18
18
  value = value.to_postgres_array(new_record?)
19
- elsif defined?(::Hstore) && column.type == :hstore && value && value.is_a?(Hash)
20
- value = value.to_hstore
21
19
  elsif klass.serialized_attributes.include?(name)
22
20
  value = @attributes[name].serialized_value
23
21
  end
@@ -50,6 +48,24 @@ module ActiveRecord
50
48
  alias_method_chain :quote, :array
51
49
  end
52
50
 
51
+ class Table
52
+ # Adds array type for migrations. So you can add columns to a table like:
53
+ # create_table :people do |t|
54
+ # ...
55
+ # t.string_array :real_energy
56
+ # t.decimal_array :real_energy, :precision => 18, :scale => 6
57
+ # ...
58
+ # end
59
+ PostgreSQLAdapter::POSTGRES_ARRAY_TYPES.each do |column_type|
60
+ define_method("#{column_type}_array") do |*args|
61
+ options = args.extract_options!
62
+ base_type = @base.type_to_sql(column_type.to_sym, options[:limit], options[:precision], options[:scale])
63
+ column_names = args
64
+ column_names.each { |name| column(name, "#{base_type}[]", options) }
65
+ end
66
+ end
67
+ end
68
+
53
69
  class TableDefinition
54
70
  # Adds array type for migrations. So you can add columns to a table like:
55
71
  # create_table :people do |t|
@@ -73,7 +89,7 @@ module ActiveRecord
73
89
  def type_cast_code_with_array(var_name)
74
90
  if type.to_s =~ /_array$/
75
91
  base_type = type.to_s.gsub(/_array/, '')
76
- "#{var_name}.from_postgres_array(:#{base_type})"
92
+ "#{var_name}.from_postgres_array(:#{base_type.parameterize('_')})"
77
93
  else
78
94
  type_cast_code_without_array(var_name)
79
95
  end
@@ -86,6 +102,10 @@ module ActiveRecord
86
102
  :decimal_array
87
103
  elsif field_type =~ /character varying.*\[\]/
88
104
  :string_array
105
+ elsif field_type =~ /^(?:real|double precision)\[\]$/
106
+ :float_array
107
+ elsif field_type =~ /timestamp.*\[\]/
108
+ :timestamp_array
89
109
  elsif field_type =~ /\[\]$/
90
110
  field_type.gsub(/\[\]/, '_array').to_sym
91
111
  else
@@ -28,8 +28,12 @@ class String
28
28
 
29
29
  if base_type == :decimal
30
30
  elements.collect(&:to_d)
31
- elsif base_type == :integer
31
+ elsif base_type == :float
32
+ elements.collect(&:to_f)
33
+ elsif base_type == :integer || base_type == :bigint
32
34
  elements.collect(&:to_i)
35
+ elsif base_type == :timestamp
36
+ elements.collect(&:to_time)
33
37
  else
34
38
  elements
35
39
  end
@@ -11,6 +11,10 @@ describe "Array" do
11
11
  [1,2,3].to_postgres_array.should == "'{1,2,3}'"
12
12
  end
13
13
 
14
+ it "returns a correct array if used on a float array" do
15
+ [1.0,2.1,3.2].to_postgres_array.should == "'{1.0,2.1,3.2}'"
16
+ end
17
+
14
18
  it "returns a correct array if used on a string array" do
15
19
  ["Ruby","on","Rails"].to_postgres_array.should == "'{\"Ruby\",\"on\",\"Rails\"}'"
16
20
  end
@@ -47,9 +47,13 @@ describe Article do
47
47
 
48
48
  it "builds valid arrays" do
49
49
  @article.languages = ["English", "German"]
50
+ @article.prices = [1.2, 1.3]
50
51
  @article.save
51
52
  @article.reload
52
53
  @article.languages_before_type_cast.should == "{English,German}"
54
+ @article.prices_before_type_cast.should == "{1.2,1.3}"
55
+ @article.languages.should == ["English", "German"]
56
+ @article.prices.should == [1.2, 1.3]
53
57
  end
54
58
 
55
59
  it "escapes single quotes correctly" do
@@ -3,5 +3,6 @@ ActiveRecord::Schema.define do
3
3
  t.string :name
4
4
  t.string_array :languages
5
5
  t.integer_array :author_ids
6
+ t.float_array :prices
6
7
  end
7
8
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres-array
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Connor
@@ -143,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  requirements: []
144
144
 
145
145
  rubyforge_project:
146
- rubygems_version: 1.8.24
146
+ rubygems_version: 1.8.15
147
147
  signing_key:
148
148
  specification_version: 3
149
149
  summary: Adds support for postgres arrays to ActiveRecord