composite_primary_keys 13.0.7 → 14.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +50 -1
  3. data/README.rdoc +182 -181
  4. data/Rakefile +1 -1
  5. data/lib/composite_primary_keys/associations/association.rb +2 -2
  6. data/lib/composite_primary_keys/associations/association_scope.rb +1 -1
  7. data/lib/composite_primary_keys/associations/collection_association.rb +38 -31
  8. data/lib/composite_primary_keys/associations/has_many_through_association.rb +19 -0
  9. data/lib/composite_primary_keys/associations/preloader/association.rb +52 -61
  10. data/lib/composite_primary_keys/autosave_association.rb +60 -60
  11. data/lib/composite_primary_keys/composite_arrays.rb +88 -86
  12. data/lib/composite_primary_keys/composite_predicates.rb +121 -120
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +1 -2
  14. data/lib/composite_primary_keys/nested_attributes.rb +2 -2
  15. data/lib/composite_primary_keys/persistence.rb +96 -83
  16. data/lib/composite_primary_keys/relation/calculations.rb +110 -104
  17. data/lib/composite_primary_keys/relation/query_methods.rb +14 -16
  18. data/lib/composite_primary_keys/relation.rb +2 -0
  19. data/lib/composite_primary_keys/validations/uniqueness.rb +40 -32
  20. data/lib/composite_primary_keys/version.rb +2 -2
  21. data/lib/composite_primary_keys.rb +117 -119
  22. data/scripts/console.rb +2 -2
  23. data/tasks/databases/trilogy.rake +23 -0
  24. data/test/abstract_unit.rb +124 -114
  25. data/test/connections/databases.ci.yml +10 -0
  26. data/test/fixtures/admin.rb +4 -0
  27. data/test/fixtures/comments.yml +6 -0
  28. data/test/fixtures/db_definitions/db2-create-tables.sql +34 -0
  29. data/test/fixtures/db_definitions/db2-drop-tables.sql +7 -1
  30. data/test/fixtures/db_definitions/mysql.sql +23 -0
  31. data/test/fixtures/db_definitions/oracle.drop.sql +4 -0
  32. data/test/fixtures/db_definitions/oracle.sql +21 -0
  33. data/test/fixtures/db_definitions/postgresql.sql +23 -0
  34. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  35. data/test/fixtures/db_definitions/sqlserver.sql +23 -0
  36. data/test/fixtures/department.rb +20 -16
  37. data/test/fixtures/moderator.rb +4 -0
  38. data/test/fixtures/room.rb +4 -1
  39. data/test/fixtures/room_assignment.rb +18 -14
  40. data/test/fixtures/staff_room.rb +6 -0
  41. data/test/fixtures/staff_room_key.rb +6 -0
  42. data/test/fixtures/user.rb +3 -0
  43. data/test/fixtures/user_with_polymorphic_name.rb +9 -0
  44. data/test/test_associations.rb +403 -372
  45. data/test/test_composite_arrays.rb +44 -38
  46. data/test/test_has_one_through.rb +30 -0
  47. data/test/test_nested_attributes.rb +23 -0
  48. data/test/test_polymorphic.rb +6 -0
  49. metadata +14 -7
  50. data/lib/composite_primary_keys/associations/through_association.rb +0 -24
@@ -11,6 +11,8 @@ drop table streets;
11
11
  drop sequence streets_seq;
12
12
  drop table users;
13
13
  drop sequence users_seq;
14
+ drop table moderators;
15
+ drop table admins;
14
16
  drop table articles;
15
17
  drop sequence articles_seq;
16
18
  drop table readings;
@@ -35,6 +37,8 @@ drop table room_attributes;
35
37
  drop sequence room_attributes_seq;
36
38
  drop table room_attribute_assignments;
37
39
  drop table room_assignments;
40
+ drop table staff_rooms;
41
+ drop table staff_room_keys;
38
42
  drop table students;
39
43
  drop sequence students_seq;
40
44
  drop table capitols;
@@ -63,6 +63,14 @@ create table users (
63
63
  name varchar(50) not null
64
64
  );
65
65
 
66
+ create table moderators (
67
+ id number(11) primary key
68
+ );
69
+
70
+ create table admins (
71
+ id number(11) primary key
72
+ );
73
+
66
74
  create sequence articles_seq start with 1000;
67
75
 
68
76
  create table articles (
@@ -169,6 +177,19 @@ create table room_attribute_assignments (
169
177
  room_attribute_id number(11) not null
170
178
  );
171
179
 
180
+ create table staff_rooms (
181
+ dorm_id number(11) not null,
182
+ room_id number(11) not null,
183
+ constraint staff_rooms_pk primary key (dorm_id, room_id)
184
+ );
185
+
186
+ create table staff_room_keys (
187
+ dorm_id number(11) not null,
188
+ room_id number(11) not null,
189
+ key_no varchar(50) not null,
190
+ constraint staff_room_keys_pk primary key (dorm_id, room_id)
191
+ );
192
+
172
193
  create sequence students_seq start with 1000;
173
194
 
174
195
  create table students (
@@ -60,6 +60,16 @@ create table users (
60
60
  primary key (id)
61
61
  );
62
62
 
63
+ create table moderators (
64
+ id serial not null,
65
+ primary key (id)
66
+ );
67
+
68
+ create table admins (
69
+ id serial not null,
70
+ primary key (id)
71
+ );
72
+
63
73
  create table articles (
64
74
  id serial not null,
65
75
  name varchar(50) not null,
@@ -154,6 +164,19 @@ create table room_attribute_assignments (
154
164
  room_attribute_id int not null
155
165
  );
156
166
 
167
+ create table staff_rooms (
168
+ dorm_id int not null,
169
+ room_id int not null,
170
+ primary key (dorm_id, room_id)
171
+ );
172
+
173
+ create table staff_room_keys (
174
+ dorm_id int not null,
175
+ room_id int not null,
176
+ key_no varchar(50) not null,
177
+ primary key (dorm_id, room_id)
178
+ );
179
+
157
180
  create table students (
158
181
  id serial not null,
159
182
  primary key (id)
@@ -56,6 +56,14 @@ create table users (
56
56
  name varchar(50) not null
57
57
  );
58
58
 
59
+ create table moderators (
60
+ id integer not null primary key
61
+ );
62
+
63
+ create table admins (
64
+ id integer not null primary key
65
+ );
66
+
59
67
  create table articles (
60
68
  id integer not null primary key autoincrement,
61
69
  name varchar(50) not null
@@ -142,6 +150,19 @@ create table room_attribute_assignments (
142
150
  room_attribute_id integer not null
143
151
  );
144
152
 
153
+ create table staff_rooms (
154
+ dorm_id integer not null,
155
+ room_id integer not null,
156
+ primary key (dorm_id, room_id)
157
+ );
158
+
159
+ create table staff_room_keys (
160
+ dorm_id integer not null,
161
+ room_id integer not null,
162
+ key_no varchar(50) not null,
163
+ primary key (dorm_id, room_id)
164
+ );
165
+
145
166
  create table students (
146
167
  id integer not null primary key autoincrement
147
168
  );
@@ -58,6 +58,14 @@ CREATE TABLE users (
58
58
  name varchar(50) NOT NULL
59
59
  );
60
60
 
61
+ CREATE TABLE moderators (
62
+ id [int] PRIMARY KEY
63
+ );
64
+
65
+ CREATE TABLE admins (
66
+ id [int] PRIMARY KEY
67
+ );
68
+
61
69
  CREATE TABLE articles (
62
70
  id [int] IDENTITY(1000,1) NOT NULL,
63
71
  name varchar(50) NOT NULL
@@ -148,6 +156,21 @@ CREATE TABLE room_attribute_assignments (
148
156
  room_attribute_id [int] NOT NULL
149
157
  );
150
158
 
159
+ CREATE TABLE staff_rooms (
160
+ dorm_id [int] NOT NULL,
161
+ room_id [int] NOT NULL,
162
+ CONSTRAINT [staff_rooms_pk] PRIMARY KEY CLUSTERED
163
+ ( [dorm_id], [room_id] )
164
+ );
165
+
166
+ CREATE TABLE staff_room_keys (
167
+ dorm_id [int] NOT NULL,
168
+ room_id [int] NOT NULL,
169
+ key_no [varchar](50) NOT NULL,
170
+ CONSTRAINT [staff_room_keys_pk] PRIMARY KEY CLUSTERED
171
+ ( [dorm_id], [room_id] )
172
+ );
173
+
151
174
  CREATE TABLE students (
152
175
  id [int] IDENTITY(1000,1) PRIMARY KEY NOT NULL
153
176
  );
@@ -1,16 +1,20 @@
1
- class Department < ActiveRecord::Base
2
- self.primary_keys = :id, :location_id
3
-
4
- has_many :employees,
5
- # We intentionally redefine primary key for test purposes. #455
6
- :primary_key => [:id, :location_id],
7
- :foreign_key => [:department_id, :location_id]
8
-
9
- has_many :comments, :through => :employees
10
-
11
- has_one :head, :class_name => 'Employee', :autosave => true, :dependent => :delete,
12
- # We intentionally redefine primary key for test purposes. #455
13
- :primary_key => [:id, :location_id],
14
- :foreign_key => [:department_id, :location_id]
15
-
16
- end
1
+ class Department < ActiveRecord::Base
2
+ self.primary_keys = :id, :location_id
3
+
4
+ has_many :employees,
5
+ # We intentionally redefine primary key for test purposes. #455
6
+ :primary_key => [:id, :location_id],
7
+ :foreign_key => [:department_id, :location_id]
8
+
9
+ has_many :comments, :through => :employees
10
+
11
+ has_one :head, :class_name => 'Employee', :autosave => true, :dependent => :delete,
12
+ # We intentionally redefine primary key for test purposes. #455
13
+ :primary_key => [:id, :location_id],
14
+ :foreign_key => [:department_id, :location_id]
15
+
16
+ has_one :head_without_autosave, :class_name => 'Employee',
17
+ # We intentionally redefine primary key for test purposes. #455
18
+ :primary_key => [:id, :location_id],
19
+ :foreign_key => [:department_id, :location_id]
20
+ end
@@ -0,0 +1,4 @@
1
+ class Moderator < ActiveRecord::Base
2
+ belongs_to :user, :foreign_key => :id, :inverse_of => :moderator
3
+ has_one :admin, :foreign_key => :id, :inverse_of => :moderator
4
+ end
@@ -4,7 +4,10 @@ class Room < ActiveRecord::Base
4
4
  has_many :room_assignments, :foreign_key => [:dorm_id, :room_id]
5
5
  has_many :room_attribute_assignments, :foreign_key => [:dorm_id, :room_id]
6
6
  has_many :room_attributes, :through => :room_attribute_assignments
7
-
7
+
8
+ has_one :staff_room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :room
9
+ delegate :staff_room_key, :to => :staff_room, :allow_nil => true
10
+
8
11
  def find_custom_room_attributes
9
12
  room_attributes.where("room_attributes.name != ?", "type")
10
13
  end
@@ -1,14 +1,18 @@
1
- class RoomAssignment < ActiveRecord::Base
2
- self.primary_keys = :student_id, :dorm_id, :room_id
3
- belongs_to :student
4
- belongs_to :room, :foreign_key => [:dorm_id, :room_id], :primary_key => [:dorm_id, :room_id]
5
- validates_uniqueness_of :student_id
6
-
7
- before_destroy do |record|
8
- puts record
9
- end
10
-
11
- after_destroy do |record|
12
- puts record
13
- end
14
- end
1
+ class RoomAssignment < ActiveRecord::Base
2
+ self.primary_keys = :student_id, :dorm_id, :room_id
3
+ belongs_to :student
4
+ belongs_to :room, :foreign_key => [:dorm_id, :room_id], :primary_key => [:dorm_id, :room_id]
5
+ validates :student_id, uniqueness: {
6
+ conditions: ->(record) {
7
+ where(student_id: record.student_id) # enough just to exercise this code path
8
+ }
9
+ }
10
+
11
+ before_destroy do |record|
12
+ puts record
13
+ end
14
+
15
+ after_destroy do |record|
16
+ puts record
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ class StaffRoom < ActiveRecord::Base
2
+ self.primary_keys = :dorm_id, :room_id
3
+
4
+ belongs_to :room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room
5
+ has_one :staff_room_key, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room
6
+ end
@@ -0,0 +1,6 @@
1
+ class StaffRoomKey < ActiveRecord::Base
2
+ self.primary_keys = :dorm_id, :room_id
3
+
4
+ belongs_to :staff_room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room_key
5
+ has_one :room, :through => :staff_room
6
+ end
@@ -5,6 +5,9 @@ class User < ActiveRecord::Base
5
5
  has_many :comments, :as => :person
6
6
  has_one :first_comment, :as => :person, :class_name => "Comment"
7
7
 
8
+ has_one :moderator, :foreign_key => :id, :inverse_of => :user
9
+ delegate :admin, :to => :moderator, :allow_nil => true
10
+
8
11
  def find_custom_articles
9
12
  articles.where("name = ?", "Article One")
10
13
  end
@@ -0,0 +1,9 @@
1
+ class UserWithPolymorphicName < ActiveRecord::Base
2
+ self.table_name = "users"
3
+
4
+ has_many :comments, :as => :person
5
+
6
+ def self.polymorphic_name
7
+ "User1"
8
+ end
9
+ end