Skip to content

Commit

Permalink
Merge pull request #597 from kyori19/has-one-through
Browse files Browse the repository at this point in the history
Fix HasOneThrough association
  • Loading branch information
cfis authored May 10, 2023
2 parents 8be6a9e + fc0d384 commit 358ef9b
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 28 deletions.
2 changes: 0 additions & 2 deletions lib/composite_primary_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
require 'active_record/associations/preloader/association'
require 'active_record/associations/singular_association'
require 'active_record/associations/collection_association'
require 'active_record/associations/through_association'

require 'active_record/attribute_methods/primary_key'
require 'active_record/attribute_methods/read'
Expand Down Expand Up @@ -91,7 +90,6 @@
require_relative 'composite_primary_keys/associations/join_association'
require_relative 'composite_primary_keys/associations/preloader/association'
require_relative 'composite_primary_keys/associations/collection_association'
require_relative 'composite_primary_keys/associations/through_association'

require_relative 'composite_primary_keys/attribute_methods/primary_key'
require_relative 'composite_primary_keys/attribute_methods/read'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def through_records_for(record)
end
end
end

alias :original_construct_join_attributes :construct_join_attributes

def construct_join_attributes(*records)
# CPK
if !self.source_reflection.polymorphic? && source_reflection.klass.composite?
ensure_mutable

ids = records.map do |record|
source_reflection.association_primary_key(reflection.klass).map do |key|
record.send(key)
end
end

cpk_in_predicate(through_association.scope.klass.arel_table, source_reflection.foreign_key, ids)
else
original_construct_join_attributes(*records)
end
end
end
end
end
24 changes: 0 additions & 24 deletions lib/composite_primary_keys/associations/through_association.rb

This file was deleted.

4 changes: 4 additions & 0 deletions test/fixtures/admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Admin < ActiveRecord::Base
belongs_to :moderator, :foreign_key => :id, :inverse_of => :admin
has_one :user, :through => :moderator
end
34 changes: 34 additions & 0 deletions test/fixtures/db_definitions/db2-create-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ CREATE TABLE users (
PRIMARY KEY (id)
);

CREATE TABLE moderators (
id integer NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE admins (
id integer NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE articles (
id integer NOT NULL ,
name varchar(50) NOT NULL,
Expand Down Expand Up @@ -110,3 +120,27 @@ create table products_restaurants (
franchise_id integer not null,
store_id integer not null
);

create table dorms (
id integer not null,
primary key (id)
)

create table rooms (
dorm_id integer not null,
room_id integer not null,
primary key (dorm_id, room_id)
);

create table staff_rooms (
dorm_id integer not null,
room_id integer not null,
primary key (dorm_id, room_id)
);

create table staff_room_keys (
dorm_id integer not null,
room_id integer not null,
key_no varchar(50) not null,
primary key (dorm_id, room_id)
);
8 changes: 7 additions & 1 deletion test/fixtures/db_definitions/db2-drop-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ drop table REFERENCE_TYPES;
drop table STREETS;
drop table PRODUCTS;
drop table USERS;
drop table MODERATORS;
drop table ADMINS;
drop table SUBURBS;
drop table PRODUCT_TARIFFS;
drop table KITCHEN_SINK;
drop table RESTAURANTS;
drop table RESTAURANTS_SUBURBS;
drop table PRODUCTS_RESTAURANTS;
drop table PRODUCTS_RESTAURANTS;
drop table DORMS;
drop table ROOMS;
drop table STAFF_ROOMS;
drop table STAFF_ROOM_KEYS;
23 changes: 23 additions & 0 deletions test/fixtures/db_definitions/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ create table users (
primary key (id)
);

create table moderators (
id int not null,
primary key (id)
);

create table admins (
id int not null,
primary key (id)
);

create table articles (
id int not null auto_increment,
name varchar(50) not null,
Expand Down Expand Up @@ -152,6 +162,19 @@ create table room_attribute_assignments (
room_attribute_id int not null
);

create table staff_rooms (
dorm_id int not null,
room_id int not null,
primary key (dorm_id, room_id)
);

create table staff_room_keys (
dorm_id int not null,
room_id int not null,
key_no varchar(50) not null,
primary key (dorm_id, room_id)
);

create table students (
id int not null auto_increment,
primary key(id)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/db_definitions/oracle.drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ drop table streets;
drop sequence streets_seq;
drop table users;
drop sequence users_seq;
drop table moderators;
drop table admins;
drop table articles;
drop sequence articles_seq;
drop table readings;
Expand All @@ -35,6 +37,8 @@ drop table room_attributes;
drop sequence room_attributes_seq;
drop table room_attribute_assignments;
drop table room_assignments;
drop table staff_rooms;
drop table staff_room_keys;
drop table students;
drop sequence students_seq;
drop table capitols;
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/db_definitions/oracle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ create table users (
name varchar(50) not null
);

create table moderators (
id number(11) primary key
);

create table admins (
id number(11) primary key
);

create sequence articles_seq start with 1000;

create table articles (
Expand Down Expand Up @@ -169,6 +177,19 @@ create table room_attribute_assignments (
room_attribute_id number(11) not null
);

create table staff_rooms (
dorm_id number(11) not null,
room_id number(11) not null,
constraint staff_rooms_pk primary key (dorm_id, room_id)
);

create table staff_room_keys (
dorm_id number(11) not null,
room_id number(11) not null,
key_no varchar(50) not null,
constraint staff_room_keys_pk primary key (dorm_id, room_id)
);

create sequence students_seq start with 1000;

create table students (
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/db_definitions/postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ create table users (
primary key (id)
);

create table moderators (
id serial not null,
primary key (id)
);

create table admins (
id serial not null,
primary key (id)
);

create table articles (
id serial not null,
name varchar(50) not null,
Expand Down Expand Up @@ -154,6 +164,19 @@ create table room_attribute_assignments (
room_attribute_id int not null
);

create table staff_rooms (
dorm_id int not null,
room_id int not null,
primary key (dorm_id, room_id)
);

create table staff_room_keys (
dorm_id int not null,
room_id int not null,
key_no varchar(50) not null,
primary key (dorm_id, room_id)
);

create table students (
id serial not null,
primary key (id)
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/db_definitions/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ create table users (
name varchar(50) not null
);

create table moderators (
id integer not null primary key
);

create table admins (
id integer not null primary key
);

create table articles (
id integer not null primary key autoincrement,
name varchar(50) not null
Expand Down Expand Up @@ -142,6 +150,19 @@ create table room_attribute_assignments (
room_attribute_id integer not null
);

create table staff_rooms (
dorm_id integer not null,
room_id integer not null,
primary key (dorm_id, room_id)
);

create table staff_room_keys (
dorm_id integer not null,
room_id integer not null,
key_no varchar(50) not null,
primary key (dorm_id, room_id)
);

create table students (
id integer not null primary key autoincrement
);
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/db_definitions/sqlserver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ CREATE TABLE users (
name varchar(50) NOT NULL
);

CREATE TABLE moderators (
id [int] PRIMARY KEY
);

CREATE TABLE admins (
id [int] PRIMARY KEY
);

CREATE TABLE articles (
id [int] IDENTITY(1000,1) NOT NULL,
name varchar(50) NOT NULL
Expand Down Expand Up @@ -148,6 +156,21 @@ CREATE TABLE room_attribute_assignments (
room_attribute_id [int] NOT NULL
);

CREATE TABLE staff_rooms (
dorm_id [int] NOT NULL,
room_id [int] NOT NULL,
CONSTRAINT [staff_rooms_pk] PRIMARY KEY CLUSTERED
( [dorm_id], [room_id] )
);

CREATE TABLE staff_room_keys (
dorm_id [int] NOT NULL,
room_id [int] NOT NULL,
key_no [varchar](50) NOT NULL,
CONSTRAINT [staff_room_keys_pk] PRIMARY KEY CLUSTERED
( [dorm_id], [room_id] )
);

CREATE TABLE students (
id [int] IDENTITY(1000,1) PRIMARY KEY NOT NULL
);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/moderator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Moderator < ActiveRecord::Base
belongs_to :user, :foreign_key => :id, :inverse_of => :moderator
has_one :admin, :foreign_key => :id, :inverse_of => :moderator
end
5 changes: 4 additions & 1 deletion test/fixtures/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ class Room < ActiveRecord::Base
has_many :room_assignments, :foreign_key => [:dorm_id, :room_id]
has_many :room_attribute_assignments, :foreign_key => [:dorm_id, :room_id]
has_many :room_attributes, :through => :room_attribute_assignments


has_one :staff_room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :room
delegate :staff_room_key, :to => :staff_room, :allow_nil => true

def find_custom_room_attributes
room_attributes.where("room_attributes.name != ?", "type")
end
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/staff_room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StaffRoom < ActiveRecord::Base
self.primary_keys = :dorm_id, :room_id

belongs_to :room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room
has_one :staff_room_key, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room
end
6 changes: 6 additions & 0 deletions test/fixtures/staff_room_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StaffRoomKey < ActiveRecord::Base
self.primary_keys = :dorm_id, :room_id

belongs_to :staff_room, :foreign_key => [:dorm_id, :room_id], :inverse_of => :staff_room_key
has_one :room, :through => :staff_room
end
3 changes: 3 additions & 0 deletions test/fixtures/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class User < ActiveRecord::Base
has_many :comments, :as => :person
has_one :first_comment, :as => :person, :class_name => "Comment"

has_one :moderator, :foreign_key => :id, :inverse_of => :user
delegate :admin, :to => :moderator, :allow_nil => true

def find_custom_articles
articles.where("name = ?", "Article One")
end
Expand Down
Loading

0 comments on commit 358ef9b

Please sign in to comment.