composite_primary_keys 7.0.11 → 7.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4146bd7f227eed41c3dbfeb8cd6e397789839531
4
- data.tar.gz: 523601026d76d779cc5b96e216fb04adb562c0a1
3
+ metadata.gz: 65cb7db711192f8ae6b531e8c5cc5fa316554806
4
+ data.tar.gz: ce079278064acfc94b691592c804047c6b0a4dd5
5
5
  SHA512:
6
- metadata.gz: feb2446749e8f8872a7073a1ba9d6409071e963cf46ea84d7b3bf8abd922ec580320b99cb33459694d56d4d57d74568a9f13ddb9b2f6e2eda7eb7e9c12a6ef6c
7
- data.tar.gz: b842f5f95e23cae95d0af45bb315355542f6601d9407f745db47ac7a15d8315ebcc5a52506e380395a69e45183183f5f8b6191014e5c980f5643b58ed99f2719
6
+ metadata.gz: f927ff29273325bc1034c2981382403fc568709f6f400db40e9d33c0933dc2016b222a9ae67cc85ad414b1e8b0e5186441ca534039f9c61c0bf35fe27c87c284
7
+ data.tar.gz: c0f88b9953c629c61b2bb001b0ff9375878c196b9b13f6d60e92705514ff04971b5a5ed4add442c55eebd169aded609f159d5a30ee0bd326a4b370f1d595a787
data/History.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 7.0.12 (2014-11-09)
2
+
3
+ * ActiveRecord 4.1.7 support (Tom Hughes)
4
+ * Extract CPK methods to its own module and extending instead of opening the singleton class
5
+ which fixes Marshal#dump (Nicolás Hock Isaza)
6
+
1
7
  == 7.0.11 (2014-10-10)
2
8
 
3
9
  * ActiveRecord 4.1.6 support (Tom Hughes, Charlie Savage)
data/README.rdoc CHANGED
@@ -84,6 +84,7 @@ CPK supports the following databases:
84
84
 
85
85
  * PostgreSQL
86
86
  * MySQL
87
+ * MariaDB
87
88
  * Oracle
88
89
  * DB2
89
90
  * SQLite
@@ -26,7 +26,7 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~>4.1.6'
29
+ gem 'activerecord', '~>4.1.7'
30
30
  require 'active_record'
31
31
  end
32
32
 
@@ -104,3 +104,5 @@ require 'composite_primary_keys/relation/predicate_builder'
104
104
  require 'composite_primary_keys/relation/query_methods'
105
105
 
106
106
  require 'composite_primary_keys/validations/uniqueness'
107
+
108
+ require 'composite_primary_keys/composite_relation'
@@ -0,0 +1,48 @@
1
+ module CompositePrimaryKeys
2
+ module CompositeRelation
3
+ include CompositePrimaryKeys::ActiveRecord::Batches
4
+ include CompositePrimaryKeys::ActiveRecord::Calculations
5
+ include CompositePrimaryKeys::ActiveRecord::FinderMethods
6
+ include CompositePrimaryKeys::ActiveRecord::QueryMethods
7
+
8
+ def delete(id_or_array)
9
+ # CPK
10
+ if self.composite?
11
+ id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
12
+ [id_or_array]
13
+ else
14
+ Array(id_or_array)
15
+ end
16
+
17
+ id_or_array.each do |id|
18
+ # Is the passed in id actually a record?
19
+ id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
20
+ where(cpk_id_predicate(table, self.primary_key, id)).delete_all
21
+ end
22
+ else
23
+ where(primary_key => id_or_array).delete_all
24
+ end
25
+ end
26
+
27
+ def destroy(id_or_array)
28
+ # Without CPK:
29
+ #if id.is_a?(Array)
30
+ # id.map { |one_id| destroy(one_id) }
31
+ #else
32
+ # find(id).destroy
33
+ #end
34
+
35
+ id_or_array = if id_or_array.kind_of?(CompositePrimaryKeys::CompositeKeys)
36
+ [id_or_array]
37
+ else
38
+ Array(id_or_array)
39
+ end
40
+
41
+ id_or_array.each do |id|
42
+ where(cpk_id_predicate(table, self.primary_key, id)).each do |record|
43
+ record.destroy
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,13 @@
1
1
  module ActiveRecord
2
2
  module NestedAttributes
3
+ def cpk_detect_record(id, records)
4
+ id_string = id.is_a?(Array) ? CompositePrimaryKeys::CompositeKeys.new(id).to_s : id.to_s
5
+
6
+ records.detect do |record|
7
+ record.id.to_s == id_string
8
+ end
9
+ end
10
+
3
11
  def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
4
12
  options = self.nested_attributes_options[association_name]
5
13
 
@@ -48,12 +56,12 @@ module ActiveRecord
48
56
  unless reject_new_record?(association_name, attributes)
49
57
  association.build(attributes.except(*UNASSIGNABLE_KEYS))
50
58
  end
51
- elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
59
+ elsif existing_record = cpk_detect_record(attributes['id'], existing_records)
52
60
  unless call_reject_if(association_name, attributes)
53
61
  # Make sure we are operating on the actual object which is in the association's
54
62
  # proxy_target array (either by finding it, or adding it if not found)
55
63
  # Take into account that the proxy_target may have changed due to callbacks
56
- target_record = association.target.detect { |record| record.id.to_s == attributes['id'].to_s }
64
+ target_record = cpk_detect_record(attributes['id'], association.target)
57
65
  if target_record
58
66
  existing_record = target_record
59
67
  else
@@ -4,7 +4,6 @@ module ActiveRecord
4
4
  def initialize(klass, table, values = {})
5
5
  initialize_without_cpk(klass, table, values)
6
6
  add_cpk_support if klass && klass.composite?
7
- add_cpk_where_values_hash
8
7
  end
9
8
 
10
9
  alias :initialize_copy_without_cpk :initialize_copy
@@ -14,75 +13,27 @@ module ActiveRecord
14
13
  end
15
14
 
16
15
  def add_cpk_support
17
- class << self
18
- include CompositePrimaryKeys::ActiveRecord::Batches
19
- include CompositePrimaryKeys::ActiveRecord::Calculations
20
- include CompositePrimaryKeys::ActiveRecord::FinderMethods
21
- include CompositePrimaryKeys::ActiveRecord::QueryMethods
22
-
23
- def delete(id_or_array)
24
- # CPK
25
- if self.composite?
26
- id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
27
- [id_or_array]
28
- else
29
- Array(id_or_array)
30
- end
31
-
32
- id_or_array.each do |id|
33
- # Is the passed in id actually a record?
34
- id = id.kind_of?(ActiveRecord::Base) ? id.id : id
35
- where(cpk_id_predicate(table, self.primary_key, id)).delete_all
36
- end
37
- else
38
- where(primary_key => id_or_array).delete_all
39
- end
40
- end
41
-
42
- def destroy(id_or_array)
43
- # Without CPK:
44
- #if id.is_a?(Array)
45
- # id.map { |one_id| destroy(one_id) }
46
- #else
47
- # find(id).destroy
48
- #end
49
-
50
- id_or_array = if id_or_array.kind_of?(CompositePrimaryKeys::CompositeKeys)
51
- [id_or_array]
52
- else
53
- Array(id_or_array)
54
- end
55
-
56
- id_or_array.each do |id|
57
- where(cpk_id_predicate(table, self.primary_key, id)).each do |record|
58
- record.destroy
59
- end
60
- end
61
- end
62
- end
16
+ extend CompositePrimaryKeys::CompositeRelation
63
17
  end
64
18
 
65
- def add_cpk_where_values_hash
66
- class << self
67
- def where_values_hash(relation_table_name = table_name)
68
- # CPK adds this so that it finds the Equality nodes beneath the And node:
69
- #equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node|
70
- # node.left.relation.name == table_name
71
- # }
72
- nodes_from_and = where_values.grep(Arel::Nodes::And).map {|and_node| and_node.children.grep(Arel::Nodes::Equality) }.flatten
19
+ # CPK adds this so that it finds the Equality nodes beneath the And node:
20
+ # equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node|
21
+ # node.left.relation.name == table_name
22
+ # }
23
+ alias :where_values_hash_without_cpk :where_values_hash
24
+ def where_values_hash(relation_table_name = table_name)
25
+ nodes_from_and = where_values.grep(Arel::Nodes::And).map {|and_node| and_node.children.grep(Arel::Nodes::Equality) }.flatten
73
26
 
74
- equalities = (nodes_from_and + where_values.grep(Arel::Nodes::Equality)).find_all { |node|
75
- node.left.relation.name == relation_table_name
76
- }
27
+ equalities = (nodes_from_and + where_values.grep(Arel::Nodes::Equality)).find_all { |node|
28
+ node.left.relation.name == relation_table_name
29
+ }
77
30
 
78
- binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]
31
+ binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]
79
32
 
80
- Hash[equalities.map { |where|
81
- name = where.left.name
82
- [name, binds.fetch(name.to_s) { where.right }]
83
- }]
84
- end
85
- end
33
+ Hash[equalities.map { |where|
34
+ name = where.left.name
35
+ [name, binds.fetch(name.to_s) { where.right }]
36
+ }]
86
37
  end
87
38
 
88
39
  def _update_record(values, id, id_was)
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 7
4
4
  MINOR = 0
5
- TINY = 11
5
+ TINY = 12
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -51,5 +51,13 @@ class TestAttributeMethods < ActiveSupport::TestCase
51
51
  assert_equal([1,1], ref_code.to_key)
52
52
  end
53
53
 
54
-
54
+ def test_id_was
55
+ rt = ReferenceType.find(1)
56
+ rt.id = 2
57
+ assert_equal 1, rt.id_was
58
+
59
+ ref_code = ReferenceCode.find([1, 1])
60
+ ref_code.id = [1,2]
61
+ assert_equal [1,1], ref_code.id_was
62
+ end
55
63
  end
data/test/test_delete.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require File.expand_path('../abstract_unit', __FILE__)
2
2
 
3
3
  class TestDelete < ActiveSupport::TestCase
4
- fixtures :departments, :employees, :products, :product_tariffs,
4
+ fixtures :articles, :departments, :employees, :products, :tariffs, :product_tariffs,
5
5
  :reference_types, :reference_codes
6
6
 
7
7
  CLASSES = {
@@ -13,10 +13,9 @@ class TestValidations < ActiveSupport::TestCase
13
13
  EmployeesGroup.create(employee_id: 3, group_id: 103)
14
14
 
15
15
  assert_equal(EmployeesGroup.all.size, 3)
16
- exception = assert_raises(ActiveRecord::StatementInvalid) {
16
+ assert_raises(ActiveRecord::StatementInvalid) {
17
17
  EmployeesGroup.where(employee_id: 1).first.destroy
18
18
  }
19
- assert_match(/Unknown column 'employees_groups.' in 'where clause/, exception.message)
20
19
  assert(EmployeesGroup.all.size == 3)
21
20
  end
22
21
  end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestDumpable < ActiveSupport::TestCase
4
+ fixtures :articles, :readings, :users
5
+
6
+ def test_marshal_with_simple_preload
7
+ articles = Article.preload(:readings).where(id: 1).to_a
8
+ assert_equal(Marshal.load(Marshal.dump(articles)), articles)
9
+ end
10
+
11
+ def test_marshal_with_comples_preload
12
+ articles = Article.preload({ readings: :user }).where(id: 1).to_a
13
+ assert_equal(Marshal.load(Marshal.dump(articles)), articles)
14
+ end
15
+ end
data/test/test_suite.rb CHANGED
@@ -8,7 +8,10 @@
8
8
  test_composite_arrays
9
9
  test_create
10
10
  test_delete
11
+ test_delete_all
12
+ test_dumpable
11
13
  test_dup
14
+ test_enum
12
15
  test_equal
13
16
  test_exists
14
17
  test_find
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.11
4
+ version: 7.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.6
19
+ version: 4.1.7
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.6
26
+ version: 4.1.7
27
27
  description: Composite key support for ActiveRecord
28
28
  email:
29
29
  executables: []
@@ -34,8 +34,6 @@ files:
34
34
  - README.rdoc
35
35
  - README_DB2.rdoc
36
36
  - Rakefile
37
- - init.rb
38
- - install.rb
39
37
  - lib/composite_primary_keys.rb
40
38
  - lib/composite_primary_keys/associations/association.rb
41
39
  - lib/composite_primary_keys/associations/association_scope.rb
@@ -55,6 +53,7 @@ files:
55
53
  - lib/composite_primary_keys/base.rb
56
54
  - lib/composite_primary_keys/composite_arrays.rb
57
55
  - lib/composite_primary_keys/composite_predicates.rb
56
+ - lib/composite_primary_keys/composite_relation.rb
58
57
  - lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb
59
58
  - lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
60
59
  - lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
@@ -73,7 +72,6 @@ files:
73
72
  - lib/composite_primary_keys/sanitization.rb
74
73
  - lib/composite_primary_keys/validations/uniqueness.rb
75
74
  - lib/composite_primary_keys/version.rb
76
- - loader.rb
77
75
  - scripts/console.rb
78
76
  - scripts/txt2html
79
77
  - scripts/txt2js
@@ -176,6 +174,7 @@ files:
176
174
  - test/test_create.rb
177
175
  - test/test_delete.rb
178
176
  - test/test_delete_all.rb
177
+ - test/test_dumpable.rb
179
178
  - test/test_dup.rb
180
179
  - test/test_enum.rb
181
180
  - test/test_equal.rb
@@ -235,6 +234,7 @@ test_files:
235
234
  - test/test_create.rb
236
235
  - test/test_delete.rb
237
236
  - test/test_delete_all.rb
237
+ - test/test_dumpable.rb
238
238
  - test/test_dup.rb
239
239
  - test/test_enum.rb
240
240
  - test/test_equal.rb
data/init.rb DELETED
@@ -1,2 +0,0 @@
1
- # Include hook code here
2
- require_dependency 'composite_primary_keys'
data/install.rb DELETED
@@ -1,30 +0,0 @@
1
- require 'rbconfig'
2
- require 'find'
3
- require 'ftools'
4
-
5
- include Config
6
-
7
- # this was adapted from rdoc's install.rb by ways of Log4r
8
-
9
- $sitedir = CONFIG["sitelibdir"]
10
- unless $sitedir
11
- version = CONFIG["MAJOR"] + "." + CONFIG["MINOR"]
12
- $libdir = File.join(CONFIG["libdir"], "ruby", version)
13
- $sitedir = $:.find {|x| x =~ /site_ruby/ }
14
- if !$sitedir
15
- $sitedir = File.join($libdir, "site_ruby")
16
- elsif $sitedir !~ Regexp.quote(version)
17
- $sitedir = File.join($sitedir, version)
18
- end
19
- end
20
-
21
- # the acual gruntwork
22
- Dir.chdir("lib")
23
-
24
- Find.find("composite_primary_keys", "composite_primary_keys.rb") { |f|
25
- if f[-3..-1] == ".rb"
26
- File::install(f, File.join($sitedir, *f.split(/\//)), 0644, true)
27
- else
28
- File::makedirs(File.join($sitedir, *f.split(/\//)))
29
- end
30
- }
data/loader.rb DELETED
@@ -1,24 +0,0 @@
1
- # Load local config files in /local
2
- begin
3
- local_file_supported = Dir[File.join(PROJECT_ROOT, 'local/*.sample')].map { |path| File.basename(path).sub(".sample","") }
4
- local_file_supported.each do |file|
5
- require "local/#{file}"
6
- end
7
- rescue LoadError
8
- puts <<-EOS
9
- This Gem supports local developer extensions in local/ folder.
10
- Supported files:
11
- #{local_file_supported.map { |f| "local/#{f}"}.join(', ')}
12
-
13
- Setup default sample files:
14
- rake local:setup
15
-
16
- Current warning: #{$!}
17
-
18
- EOS
19
- end
20
-
21
-
22
- # Now load Rake tasks from /tasks
23
- rakefiles = Dir[File.join(File.dirname(__FILE__), "tasks/**/*.rake")]
24
- rakefiles.each { |rakefile| load File.expand_path(rakefile) }