ar_pg_array 0.9.12 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -33,7 +33,18 @@ This library adds ability to use PostgreSQL array types with ActiveRecord.
33
33
  end
34
34
  end
35
35
 
36
- Plugin worked with rails 2.3.x. integer_array works in ActiveRecord 3 at the moment.
36
+ Installation
37
+ ============
37
38
 
39
+ gem install ar_pg_array
40
+
41
+ Changelog
42
+ =========
43
+
44
+ 0.9.13
45
+
46
+ Since version 0.9.13 ar_pg_array will try to detect arrays changed inplace.
47
+ And parsed arrays are now cached in @attributes_cache.
48
+ Thanks to Romain Beauxis (https://github.com/toots ) for being insistent about this.
38
49
 
39
50
  Copyright (c) 2010 Sokolov Yura aka funny_falcon, released under the MIT license
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.12
1
+ 0.9.13
@@ -0,0 +1,38 @@
1
+ module ActiveRecord
2
+ module CheckArrayBeforeUpdate
3
+ def mark_arrays_for_update
4
+ @attributes_cache.each do |name, value|
5
+ attribute_will_change!(name) if Array === value && _read_attribute(name) != value
6
+ end
7
+ end
8
+ end
9
+ if VERSION::MAJOR < 3
10
+ module CheckArrayBeforeUpdate
11
+ def self.included(base)
12
+ base.alias_method_chain :update, :check_array
13
+ base.send(:alias_method, :_read_attribute, :read_attribute)
14
+ end
15
+
16
+ def update_with_check_array
17
+ mark_arrays_for_update
18
+ update_without_check_array
19
+ end
20
+ end
21
+ else
22
+ module CheckArrayBeforeUpdate
23
+ include ActiveSupport::Concern
24
+
25
+ if VERSION::MAJOR == 3 && VERSION::MINOR >= 2
26
+ def _read_attribute(attr_name)
27
+ column_for_attribute(attr_name).type_cast(@attributes[attr_name])
28
+ end
29
+ end
30
+
31
+ def update(*)
32
+ mark_arrays_for_update
33
+ super
34
+ end
35
+ end
36
+ end
37
+ Base.__send__ :include, CheckArrayBeforeUpdate
38
+ end
@@ -150,7 +150,6 @@ module ActiveRecord
150
150
  when :string, :text, :other
151
151
  pa = prepare_array_for_arel_by_base_type(value, base_type)
152
152
  "'#{ quote_string( pa ) }'"
153
- #"'#{ pa.gsub("'","''") }'"
154
153
  else
155
154
  raise "Unsupported array base type #{base_type} for arel"
156
155
  end
@@ -164,7 +163,6 @@ module ActiveRecord
164
163
  prepare_pg_float_array(value)
165
164
  when :string, :text, :other
166
165
  prepare_pg_text_array(value)
167
- #prepare_pg_string_array(value, base_type).gsub("''","'")
168
166
  when :datetime, :timestamp, :time
169
167
  prepare_pg_string_array(value, base_type)
170
168
  when :decimal, :boolean, :date, :safe
@@ -0,0 +1,11 @@
1
+ adjust_cached_types = lambda do |atcbd|
2
+ atcbd << /_array$/
3
+ def atcbd.include?(val)
4
+ any?{|type| type === val}
5
+ end
6
+ end
7
+ if ActiveRecord::VERSION::MAJOR < 3
8
+ adjust_cached_types.call(ActiveRecord::AttributeMethods::ATTRIBUTE_TYPES_CACHED_BY_DEFAULT)
9
+ else
10
+ adjust_cached_types.call(ActiveRecord::AttributeMethods::Read::ATTRIBUTE_TYPES_CACHED_BY_DEFAULT)
11
+ end
@@ -0,0 +1,11 @@
1
+ adjust_cached_types = lambda do |atcbd|
2
+ atcbd << /_array$/
3
+ def atcbd.include?(val)
4
+ any?{|type| type === val}
5
+ end
6
+ end
7
+ if ActiveRecord::VERSION::MAJOR < 3
8
+ adjust_cached_types.call(ActiveRecord::AttributeMethods::ATTRIBUTE_TYPES_CACHED_BY_DEFAULT)
9
+ else
10
+ adjust_cached_types.call(ActiveRecord::AttributeMethods::Read::ATTRIBUTE_TYPES_CACHED_BY_DEFAULT)
11
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module Dirty
3
+ private
4
+ def attribute_will_change!(attr)
5
+ val = changed_attributes[attr] = clone_attribute_value(:read_attribute, attr)
6
+ if Array === val && !(Array === @attributes[attr])
7
+ send(attr) unless @attributes_cache.has_key?(attr)
8
+ @attributes[attr] = @attributes_cache[attr]
9
+ end
10
+ val
11
+ end
12
+ end
13
+ end
data/lib/ar_pg_array.rb CHANGED
@@ -3,9 +3,13 @@ require 'active_record/base'
3
3
  require 'active_record/connection_adapters/postgresql_adapter'
4
4
 
5
5
  require 'ar_pg_array/schema'
6
+ require 'ar_pg_array/schema_cacheable'
6
7
  require 'ar_pg_array/querying'
8
+ require 'ar_pg_array/allways_save'
7
9
  require 'ar_pg_array/references_by'
8
- if defined? ::Arel
10
+ if ActiveRecord::VERSION::MAJOR >= 3
9
11
  require 'ar_pg_array/schema_arel'
10
- require 'ar_pg_array/querying_arel'
11
- end
12
+ require 'ar_pg_array/querying_arel'
13
+ else
14
+ require 'ar_pg_array/schema_fix_will_change'
15
+ end
@@ -131,6 +131,27 @@ describe "PgArray" do
131
131
  bulks_where(:ints => [1,2].search_subarray).should == bulks_where(:id=>[1,2,4])
132
132
  end
133
133
 
134
+ it "should be cached in @attributes_cache" do
135
+ bulk = Bulk.find(1)
136
+ ar = bulk.ints
137
+ bulk.ints.should.equal?(ar)
138
+ ard = ar.dup
139
+ arn = (bulk.ints << 1)
140
+ arn.should.equal?(ar)
141
+ bulk.ints.should.equal?(ar)
142
+ bulk.ints.should == (ard + [1])
143
+ end
144
+
145
+ it 'should allways save changed value' do
146
+ bulk = Bulk.find(1)
147
+ old = bulk.ints.dup
148
+ bulk.ints << 2
149
+ new = bulk.ints.dup
150
+ bulk.save
151
+ bulk.reload
152
+ bulk.ints.should == new
153
+ end
154
+
134
155
  def map_times(times)
135
156
  times.map{|t| t.strftime("%F %T")}
136
157
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_pg_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.9.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-05 00:00:00.000000000 Z
12
+ date: 2012-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &84412580 !ruby/object:Gem::Requirement
16
+ requirement: &84224510 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 2.3.5
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *84412580
24
+ version_requirements: *84224510
25
25
  description: ar_pg_array includes support of PostgreSQL's int[], float[], text[],
26
26
  timestamptz[] etc. into ActiveRecord. You could define migrations for array columns,
27
27
  query on array columns.
@@ -37,11 +37,15 @@ files:
37
37
  - VERSION
38
38
  - init.rb
39
39
  - lib/ar_pg_array.rb
40
+ - lib/ar_pg_array/allways_save.rb
40
41
  - lib/ar_pg_array/querying.rb
41
42
  - lib/ar_pg_array/querying_arel.rb
42
43
  - lib/ar_pg_array/references_by.rb
43
44
  - lib/ar_pg_array/schema.rb
44
45
  - lib/ar_pg_array/schema_arel.rb
46
+ - lib/ar_pg_array/schema_cachable.rb
47
+ - lib/ar_pg_array/schema_cacheable.rb
48
+ - lib/ar_pg_array/schema_fix_will_change.rb
45
49
  - spec/fixtures/bulk.rb
46
50
  - spec/fixtures/bulks.yml
47
51
  - spec/fixtures/item.rb
@@ -72,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
76
  version: '0'
73
77
  requirements: []
74
78
  rubyforge_project: ar-pg-array
75
- rubygems_version: 1.8.10
79
+ rubygems_version: 1.8.12
76
80
  signing_key:
77
81
  specification_version: 3
78
82
  summary: Use power of PostgreSQL Arrays in ActiveRecord