composite_primary_keys 5.0.8 → 5.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.rdoc CHANGED
@@ -1,3 +1,13 @@
1
+ == 5.0.9 2012-09-29
2
+ * Enable tests for MS SQL Servder (Enderson Maia)
3
+ * Update ActiveRecord::Base#initialize_dup override in line with ActiveRecord::Base 3.2.5+ (Lucas Maxwell)
4
+ * Require postgresql_adapter.rb also when using activerecord-postgis-adapter (Torsti Schulz)
5
+ * Make cpk_in_predicate build a simple IN when possible (Tom Hughes)
6
+ * Fixed problem loading jdbcpostgresql adapter (Troy Caldwell)
7
+ * Add support for counter cache (Charlie Savage)
8
+ * Fix recreating postgresql database (Charlie Savage)
9
+ * Update dependency to ActiveRecord 3.2.8 or higher (Charlie Savage)
10
+
1
11
  == 5.0.8 2012-07-01
2
12
  * Enabled tests for MS SQL Server (Enderson Maia)
3
13
  * Update establish_connection to work with Rails 3.2.6 (Ivan Schneider)
@@ -26,11 +26,12 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~> 3.2.0'
29
+ gem 'activerecord', '>= 3.2.8', '~> 3.2.0'
30
30
  require 'active_record'
31
31
  end
32
32
 
33
33
  # AR files we override
34
+ require 'active_record/counter_cache'
34
35
  require 'active_record/fixtures'
35
36
  require 'active_record/persistence'
36
37
  require 'active_record/relation'
@@ -66,6 +67,7 @@ require 'active_record/validations/uniqueness'
66
67
  require 'composite_primary_keys/base'
67
68
  require 'composite_primary_keys/composite_arrays'
68
69
  require 'composite_primary_keys/composite_predicates'
70
+ require 'composite_primary_keys/counter_cache'
69
71
  require 'composite_primary_keys/fixtures'
70
72
  require 'composite_primary_keys/persistence'
71
73
  require 'composite_primary_keys/relation'
@@ -59,6 +59,7 @@ module ActiveRecord
59
59
 
60
60
  def initialize_dup(other)
61
61
  cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
62
+ self.class.initialize_attributes(cloned_attributes, :serialized => false)
62
63
  # CPK
63
64
  # cloned_attributes.delete(self.class.primary_key)
64
65
  Array(self.class.primary_key).each {|key| cloned_attributes.delete(key.to_s)}
@@ -148,7 +149,7 @@ module ActiveRecord
148
149
  def to_key
149
150
  ids.to_a if !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids
150
151
  end
151
-
152
+
152
153
  def to_param
153
154
  persisted? ? to_key.join(CompositePrimaryKeys::ID_SEP) : nil
154
155
  end
@@ -49,14 +49,19 @@ module CompositePrimaryKeys
49
49
  end
50
50
 
51
51
  def cpk_in_predicate(table, primary_keys, ids)
52
- and_predicates = ids.map do |id_set|
53
- eq_predicates = Array(primary_keys).zip(Array(id_set)).map do |primary_key, value|
54
- table[primary_key].eq(value)
52
+ primary_keys = Array(primary_keys)
53
+ if primary_keys.length > 1
54
+ and_predicates = ids.map do |id_set|
55
+ eq_predicates = Array(primary_keys).zip(Array(id_set)).map do |primary_key, value|
56
+ table[primary_key].eq(value)
57
+ end
58
+ cpk_and_predicate(eq_predicates)
55
59
  end
56
- cpk_and_predicate(eq_predicates)
57
- end
58
60
 
59
- cpk_or_predicate(and_predicates, table)
61
+ cpk_or_predicate(and_predicates, table)
62
+ else
63
+ table[primary_keys.first].in(ids.flatten)
64
+ end
60
65
  end
61
66
  end
62
67
  end
@@ -65,4 +70,4 @@ ActiveRecord::Associations::AssociationScope.send(:include, CompositePrimaryKeys
65
70
  ActiveRecord::Associations::HasAndBelongsToManyAssociation.send(:include, CompositePrimaryKeys::Predicates)
66
71
  ActiveRecord::Associations::JoinDependency::JoinAssociation.send(:include, CompositePrimaryKeys::Predicates)
67
72
  ActiveRecord::Associations::Preloader::Association.send(:include, CompositePrimaryKeys::Predicates)
68
- ActiveRecord::Relation.send(:include, CompositePrimaryKeys::Predicates)
73
+ ActiveRecord::Relation.send(:include, CompositePrimaryKeys::Predicates)
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  class Base
3
3
  def self.load_cpk_adapter(adapter)
4
- if adapter.to_s == 'postgresql'
5
- require "composite_primary_keys/connection_adapters/#{adapter}_adapter.rb"
4
+ if (adapter.to_s =~ /postgresql/) or (adapter.to_s =~ /postgis/)
5
+ require "composite_primary_keys/connection_adapters/postgresql_adapter.rb"
6
6
  end
7
7
  end
8
8
 
@@ -0,0 +1,22 @@
1
+ module ActiveRecord
2
+ module CounterCache
3
+ def update_counters(id, counters)
4
+ updates = counters.map do |counter_name, value|
5
+ operator = value < 0 ? '-' : '+'
6
+ quoted_column = connection.quote_column_name(counter_name)
7
+ "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
8
+ end
9
+
10
+ IdentityMap.remove_by_id(symbolized_base_class, id) if IdentityMap.enabled?
11
+
12
+ # CPK
13
+ # update_all(updates.join(', '), primary_key => id )
14
+ primary_key_predicate = relation.cpk_id_predicate(self.arel_table, Array(self.primary_key), Array(id))
15
+ update_all(updates.join(', '), primary_key_predicate)
16
+ end
17
+
18
+ def decrement_counter(counter_name, id)
19
+ update_counters(id, counter_name => -1)
20
+ end
21
+ end
22
+ end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 5
4
4
  MINOR = 0
5
- TINY = 8
5
+ TINY = 9
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -15,6 +15,10 @@ namespace :postgresql do
15
15
 
16
16
  desc 'Build the PostgreSQL test database'
17
17
  task :build_database => [:create_database] do
18
+ ActiveRecord::Base.clear_all_connections!
19
+ spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
20
+ connection = ActiveRecord::Base.establish_connection(spec)
21
+
18
22
  path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
19
23
  sql = File.open(path, 'rb') do |file|
20
24
  file.read
@@ -3,6 +3,7 @@ santiago-cpk:
3
3
  user_id: 1
4
4
  group_id: 1
5
5
  status: Active
6
+
6
7
  drnic-cpk:
7
8
  id: 2
8
9
  user_id: 2
@@ -1,6 +1,7 @@
1
1
  santiago-cpk:
2
2
  user_id: 1
3
3
  group_id: 1
4
+
4
5
  drnic-cpk:
5
6
  user_id: 2
6
7
  group_id: 1
@@ -3,11 +3,13 @@ first:
3
3
  city_id: 1
4
4
  suburb_id: 1
5
5
  name: First Street
6
+
6
7
  second1:
7
8
  id: 2
8
9
  city_id: 2
9
10
  suburb_id: 1
10
11
  name: First Street
12
+
11
13
  second2:
12
14
  id: 3
13
15
  city_id: 2
@@ -2,10 +2,12 @@ first:
2
2
  city_id: 1
3
3
  suburb_id: 1
4
4
  name: First Suburb
5
+
5
6
  second:
6
7
  city_id: 2
7
8
  suburb_id: 1
8
9
  name: Second Suburb
10
+
9
11
  no_mcdonalds:
10
12
  city_id: 1
11
13
  suburb_id: 2
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestCalculations < ActiveSupport::TestCase
4
+ fixtures :tariffs
5
+
6
+ def test_update_counter
7
+ tariff = tariffs(:flat)
8
+ assert_equal(50, tariff.amount)
9
+ Tariff.update_counters(tariff.id, :amount => 1)
10
+ tariff.reload
11
+ assert_equal(51, tariff.amount)
12
+ end
13
+
14
+ def test_increment_counter
15
+ tariff = tariffs(:flat)
16
+ assert_equal(50, tariff.amount)
17
+ Tariff.increment_counter(:amount, tariff.id)
18
+
19
+ tariff.reload
20
+ assert_equal(51, tariff.amount)
21
+ end
22
+
23
+ def test_decrement_counter
24
+ tariff = tariffs(:flat)
25
+ assert_equal(50, tariff.amount)
26
+ Tariff.decrement_counter(:amount, tariff.id)
27
+
28
+ tariff.reload
29
+ assert_equal(49, tariff.amount)
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.8
4
+ version: 5.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,13 +10,16 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-01 00:00:00.000000000 Z
13
+ date: 2012-09-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.8
20
23
  - - ~>
21
24
  - !ruby/object:Gem::Version
22
25
  version: 3.2.0
@@ -25,6 +28,9 @@ dependencies:
25
28
  version_requirements: !ruby/object:Gem::Requirement
26
29
  none: false
27
30
  requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.8
28
34
  - - ~>
29
35
  - !ruby/object:Gem::Version
30
36
  version: 3.2.0
@@ -60,6 +66,7 @@ files:
60
66
  - lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb
61
67
  - lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
62
68
  - lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
69
+ - lib/composite_primary_keys/counter_cache.rb
63
70
  - lib/composite_primary_keys/dirty.rb
64
71
  - lib/composite_primary_keys/fixtures.rb
65
72
  - lib/composite_primary_keys/persistence.rb
@@ -165,6 +172,7 @@ files:
165
172
  - test/test_attribute_methods.rb
166
173
  - test/test_calculations.rb
167
174
  - test/test_composite_arrays.rb
175
+ - test/test_counter_cache.rb
168
176
  - test/test_create.rb
169
177
  - test/test_delete.rb
170
178
  - test/test_dup.rb
@@ -202,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
210
  version: '0'
203
211
  requirements: []
204
212
  rubyforge_project: compositekeys
205
- rubygems_version: 1.8.24
213
+ rubygems_version: 1.8.23
206
214
  signing_key:
207
215
  specification_version: 3
208
216
  summary: Composite key support for ActiveRecord
@@ -217,6 +225,7 @@ test_files:
217
225
  - test/test_attribute_methods.rb
218
226
  - test/test_calculations.rb
219
227
  - test/test_composite_arrays.rb
228
+ - test/test_counter_cache.rb
220
229
  - test/test_create.rb
221
230
  - test/test_delete.rb
222
231
  - test/test_dup.rb