composite_primary_keys 8.1.5 → 8.1.6

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: abff4d9e394b49dc7b5202df306d247ac92cbcd4
4
- data.tar.gz: f0faa4bff366aba7dd02ce375eb1ba69b0b33cff
3
+ metadata.gz: 1f837ed1d8f5f9a5af46c14b91dfed1101e55de3
4
+ data.tar.gz: e115fe2d97338b6d2aa19132be1f936f107dea74
5
5
  SHA512:
6
- metadata.gz: 25822213d2b20007dfa81137f7504e6df9b86b75e41592bf9adfb75ef903c234d3d5dcda2a4df8a82a0579701b1db8bea0a4320b9db62ce3aae2c96999236d48
7
- data.tar.gz: 9734252590c4f158064e6c8f8ef1e695892f97084731639bd5875c9b0f67c18bc51070b3a7cc458b1477a53b85ebcdc915f561735b3f780d147fd9d77affd0e5
6
+ metadata.gz: f81ee887e2a3145def527fbdce440bd2cc986fa2a03d4fef914e269e642ba355d09c307cb8be25723d6d79fed632fe238cd28376fb6734a549de54d7b9f16a2a
7
+ data.tar.gz: 2e0166164b07de368e07758d72029c6461fab1596d311c11634b8c4e97dfd4159a3593d5acde2af2b60eec7701b7214f8487824b3b6185d687df064b5ebe91ef
data/History.rdoc CHANGED
@@ -1,3 +1,12 @@
1
+ == 8.1.6 (2017-05-20)
2
+
3
+ Query cache with bind params (ttw)
4
+ Fix TestIds#test_set_ids_string. Sqlite's serial and integer are a bit different (Boris Peterbarg)
5
+ Associations with inverse fix (Boris Peterbarg)
6
+ Fix save_has_one_association and add a test for it (Boris Peterbarg)
7
+ Fix save_belongs_to_association override (Boris Peterbarg)
8
+ Extend approach from #344 to fix autosave for has_one associations as well (Cameron Finucane)
9
+
1
10
  == 8.1.5 (2017-01-01)
2
11
 
3
12
  * Don't nest PK twice when looking up id, fixes #319 (Kerey Roper)
data/README_DB2.rdoc CHANGED
@@ -1,33 +1,33 @@
1
- Composite Primary key support for db2
2
-
3
- == Driver Support
4
-
5
- DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
6
- project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
7
- This rubyforge project appears to be permenant location for the IBM adapter.
8
- Older versions of the driver available from IBM Alphaworks will not work.
9
-
10
- == Driver Bug and workaround provided as part of this plugin
11
-
12
- Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
13
- method doesn't return " column_name = 1 " when string values (integers in string type variable)
14
- are passed for quoting numeric column. Rather it returns "column_name = '1'.
15
- DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
16
- this plugin a fix is provided for the DB2 adapter since this plugin does
17
- pass string values like this. Perhaps a patch should be sent to the DB2 adapter
18
- project for a permanant fix.
19
-
20
- == Database Setup
21
-
22
- Database must be manually created using a separate command. Read the rake task
23
- for creating tables and change the db name, user and passwords accordingly.
24
-
25
- == Tested Database Server version
26
-
27
- This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
28
-
29
- == Tested Database Client version
30
-
31
- This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
32
-
33
-
1
+ Composite Primary key support for db2
2
+
3
+ == Driver Support
4
+
5
+ DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
6
+ project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
7
+ This rubyforge project appears to be permenant location for the IBM adapter.
8
+ Older versions of the driver available from IBM Alphaworks will not work.
9
+
10
+ == Driver Bug and workaround provided as part of this plugin
11
+
12
+ Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
13
+ method doesn't return " column_name = 1 " when string values (integers in string type variable)
14
+ are passed for quoting numeric column. Rather it returns "column_name = '1'.
15
+ DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
16
+ this plugin a fix is provided for the DB2 adapter since this plugin does
17
+ pass string values like this. Perhaps a patch should be sent to the DB2 adapter
18
+ project for a permanant fix.
19
+
20
+ == Database Setup
21
+
22
+ Database must be manually created using a separate command. Read the rake task
23
+ for creating tables and change the db name, user and passwords accordingly.
24
+
25
+ == Tested Database Server version
26
+
27
+ This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
28
+
29
+ == Tested Database Client version
30
+
31
+ This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
32
+
33
+
@@ -2,6 +2,27 @@ module ActiveRecord
2
2
  module Associations
3
3
  class AssociationScope
4
4
 
5
+ def self.get_bind_values(owner, chain)
6
+ binds = []
7
+ last_reflection = chain.last
8
+
9
+ # CPK
10
+ # binds << last_reflection.join_id_for(owner)
11
+ values = last_reflection.join_id_for(owner)
12
+ binds += Array(values)
13
+
14
+ if last_reflection.type
15
+ binds << owner.class.base_class.name
16
+ end
17
+
18
+ chain.each_cons(2).each do |reflection, next_reflection|
19
+ if reflection.type
20
+ binds << next_reflection.klass.base_class.name
21
+ end
22
+ end
23
+ binds
24
+ end
25
+
5
26
  def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_table, next_reflection)
6
27
  join_keys = reflection.join_keys(assoc_klass)
7
28
  key = join_keys.key
@@ -26,8 +47,12 @@ module ActiveRecord
26
47
  foreign_key = join_keys.foreign_key
27
48
 
28
49
  if key.kind_of?(Array) || foreign_key.kind_of?(Array)
29
- predicate = cpk_join_predicate(table, key, owner, foreign_key)
30
- scope = scope.where(predicate)
50
+ key = Array(key) unless key.kind_of?(Array)
51
+ foreign_key = Array(foreign_key) unless foreign_key.kind_of?(Array)
52
+ key.zip(foreign_key).map do |k, fk|
53
+ bind_val = bind scope, table.table_name, k.to_s, owner[fk], tracker
54
+ scope = scope.where(table[k].eq(bind_val))
55
+ end
31
56
  else
32
57
  bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
33
58
  scope = scope.where(table[key].eq(bind_val))
@@ -17,8 +17,9 @@ module ActiveRecord
17
17
  saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
18
18
 
19
19
  if association.updated?
20
- # it will fail to use "#record.send(reflection.options[:primary_key] || :id)" for CPK
21
- association_id = record.read_attribute(reflection.options[:primary_key] || :id)
20
+ # CPK
21
+ # association_id = record.send(reflection.options[:primary_key] || :id)
22
+ association_id = reflection.options[:primary_key] ? record.read_attribute(reflection.options[:primary_key]) : record.id
22
23
  self[reflection.foreign_key] = association_id
23
24
  association.loaded!
24
25
  end
@@ -27,5 +28,40 @@ module ActiveRecord
27
28
  end
28
29
  end
29
30
  end
31
+
32
+ # Saves the associated record if it's new or <tt>:autosave</tt> is enabled
33
+ # on the association.
34
+ #
35
+ # In addition, it will destroy the association if it was marked for
36
+ # destruction with mark_for_destruction.
37
+ #
38
+ # This all happens inside a transaction, _if_ the Transactions module is included into
39
+ # ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
40
+ def save_has_one_association(reflection)
41
+ association = association_instance_get(reflection.name)
42
+ record = association && association.load_target
43
+
44
+ if record && !record.destroyed?
45
+ autosave = reflection.options[:autosave]
46
+
47
+ if autosave && record.marked_for_destruction?
48
+ record.destroy
49
+ elsif autosave != false
50
+ # CPK
51
+ #key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
52
+ key = reflection.options[:primary_key] ? read_attribute(reflection.options[:primary_key]) : id
53
+
54
+ if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
55
+ unless reflection.through_reflection
56
+ record[reflection.foreign_key] = key
57
+ end
58
+
59
+ saved = record.save(:validate => !autosave)
60
+ raise ActiveRecord::Rollback if !saved && autosave
61
+ saved
62
+ end
63
+ end
64
+ end
65
+ end
30
66
  end
31
67
  end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 8
4
4
  MINOR = 1
5
- TINY = 5
5
+ TINY = 6
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -116,6 +116,7 @@ create table employees (
116
116
  id int not null auto_increment,
117
117
  department_id int default null,
118
118
  location_id int default null,
119
+ name varchar(100) not null default 'anonymous',
119
120
  primary key (id)
120
121
  );
121
122
 
@@ -216,3 +217,12 @@ create table employees_groups (
216
217
  employee_id int not null,
217
218
  group_id int not null
218
219
  );
220
+
221
+ create table pk_called_ids (
222
+ id serial not null,
223
+ reference_code int not null,
224
+ code_label varchar(50) default null,
225
+ abbreviation varchar(50) default null,
226
+ description varchar(50) default null,
227
+ primary key (id, reference_code)
228
+ );
@@ -124,7 +124,8 @@ create sequence employees_seq start with 1000;
124
124
  create table employees (
125
125
  id number(11) not null primary key,
126
126
  department_id number(11) default null,
127
- location_id number(11) default null
127
+ location_id number(11) default null,
128
+ name varchar2(100) default 'anonymous'
128
129
  );
129
130
 
130
131
  create sequence salaries_seq start with 1000;
@@ -118,6 +118,7 @@ create table employees (
118
118
  id serial not null,
119
119
  department_id int default null,
120
120
  location_id int default null,
121
+ name text not null default 'anonymous',
121
122
  primary key (id)
122
123
  );
123
124
 
@@ -109,6 +109,7 @@ create table departments (
109
109
  create table employees (
110
110
  id integer not null primary key autoincrement,
111
111
  department_id integer null,
112
+ name text not null default 'anonymous',
112
113
  location_id integer null
113
114
  );
114
115
 
@@ -204,3 +205,11 @@ create table employees_groups (
204
205
  group_id integer not null
205
206
  );
206
207
 
208
+ create table pk_called_ids (
209
+ id integer not null,
210
+ reference_code int not null,
211
+ code_label varchar(50) default null,
212
+ abbreviation varchar(50) default null,
213
+ description varchar(50) default null,
214
+ primary key (id, reference_code)
215
+ );
@@ -126,7 +126,8 @@ go
126
126
  CREATE TABLE employees (
127
127
  id [int] IDENTITY(1000,1) NOT NULL,
128
128
  department_id [int] NULL,
129
- location_id [int] NULL
129
+ location_id [int] NULL,
130
+ name varchar(100) NOT NULL
130
131
  );
131
132
  go
132
133
 
@@ -7,5 +7,6 @@ class Employee < ActiveRecord::Base
7
7
  :foreign_key => [:employee_id, :location_id]
8
8
  has_one :one_salary, :class_name => "Salary",
9
9
  :primary_key => [:id, :location_id],
10
- :foreign_key => [:employee_id, :location_id]
10
+ :foreign_key => [:employee_id, :location_id],
11
+ :inverse_of => :employee
11
12
  end
@@ -1,19 +1,23 @@
1
- steve:
2
- id: 1
3
- department_id: 1
4
- location_id: 1
5
-
6
- jill:
7
- id: 2
8
- department_id: 1
9
- location_id: 1
10
-
11
- sarah:
12
- id: 3
13
- department_id: 2
14
- location_id: 1
15
-
16
- robert:
17
- id: 4
18
- department_id: 2
19
- location_id: 1
1
+ steve:
2
+ id: 1
3
+ department_id: 1
4
+ location_id: 1
5
+ name: steve
6
+
7
+ jill:
8
+ id: 2
9
+ department_id: 1
10
+ location_id: 1
11
+ name: jill
12
+
13
+ sarah:
14
+ id: 3
15
+ department_id: 2
16
+ location_id: 1
17
+ name: sarah
18
+
19
+ robert:
20
+ id: 4
21
+ department_id: 2
22
+ location_id: 1
23
+ name: robert
@@ -91,6 +91,13 @@ class TestAssociations < ActiveSupport::TestCase
91
91
  assert_equal(room_assignment.room_id, 1001)
92
92
  end
93
93
 
94
+ def test_association_with_composite_primary_key_can_be_autosaved_on_update
95
+ robert = employees(:robert)
96
+ robert_salary = robert.create_one_salary(year: "2015", month: "1")
97
+ robert.update(name: robert.name.reverse)
98
+ assert_equal(robert.id, robert_salary.employee_id)
99
+ end
100
+
94
101
  def test_has_one_association_primary_key_and_foreign_key_are_present
95
102
  steve = employees(:steve)
96
103
  steve_salary = steve.create_one_salary(year: "2015", month: "1")
data/test/test_create.rb CHANGED
@@ -123,7 +123,7 @@ class TestCreate < ActiveSupport::TestCase
123
123
  assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
124
124
  assignment1.save!
125
125
 
126
- room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id]]
126
+ room.room_assignments = [assignment1]
127
127
  room.save!
128
128
 
129
129
  assert_equal(1, room.room_assignments.length)
@@ -146,8 +146,7 @@ class TestCreate < ActiveSupport::TestCase
146
146
  assignment2 = RoomAssignment.new(:student_id => student2.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
147
147
  assignment2.save!
148
148
 
149
- room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id],
150
- [assignment2.student_id, assignment2.dorm_id, assignment2.room_id]]
149
+ room.room_assignments = [assignment1,assignment2]
151
150
  room.save!
152
151
 
153
152
  assert_equal(2, room.room_assignments.length)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.1.5
4
+ version: 8.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-02 00:00:00.000000000 Z
11
+ date: 2017-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.4.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.0
69
83
  description: Composite key support for ActiveRecord
70
84
  email:
71
85
  executables: []
@@ -271,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
285
  version: '0'
272
286
  requirements: []
273
287
  rubyforge_project:
274
- rubygems_version: 2.6.8
288
+ rubygems_version: 2.6.11
275
289
  signing_key:
276
290
  specification_version: 4
277
291
  summary: Composite key support for ActiveRecord