diff --git a/db/migrations/20240317161918_create_interviews.cr b/db/migrations/20240317161918_create_interviews.cr new file mode 100644 index 000000000..f2c1d8833 --- /dev/null +++ b/db/migrations/20240317161918_create_interviews.cr @@ -0,0 +1,14 @@ +class CreateInterviews::V20240317161918 < Avram::Migrator::Migration::V1 + def migrate + create table_for(Interview) do + primary_key id : Int64 + add_timestamps + add_belongs_to interviewer : User, on_delete: :cascade + add_belongs_to interviewee : User, on_delete: :cascade + end + end + + def rollback + drop table_for(Interview) + end +end diff --git a/spec/avram/query_associations_spec.cr b/spec/avram/query_associations_spec.cr index 311d7460b..49424a49e 100644 --- a/spec/avram/query_associations_spec.cr +++ b/spec/avram/query_associations_spec.cr @@ -176,4 +176,16 @@ describe "Query associations" do result.should eq(product) end + + it "handles aliases", focus: true do + interviewer = UserFactory.create(&.available_for_hire(false).name("Interviewer")) + interviewee = UserFactory.create(&.available_for_hire(true).name("Interviewee")) + UserFactory.create(&.available_for_hire(false).name("Employed")) + InterviewFactory.create(&.interviewee(interviewee).interviewer(interviewer)) + + InterviewQuery.new + .where_interviewer(UserQuery.new.available_for_hire(false)) + .where_interviewee(UserQuery.new.available_for_hire(true)) + .to_prepared_sql.should eq("nothing") + end end diff --git a/spec/support/factories/interview_factory.cr b/spec/support/factories/interview_factory.cr new file mode 100644 index 000000000..9613b38f2 --- /dev/null +++ b/spec/support/factories/interview_factory.cr @@ -0,0 +1,20 @@ +class InterviewFactory < BaseFactory + def initialize + before_save do + if operation.interviewer_id.value.nil? + interviewer(UserFactory.create) + end + if operation.interviewee_id.value.nil? + interviewee(UserFactory.create) + end + end + end + + def interviewer(u : User) + interviewer_id(u.id) + end + + def interviewee(u : User) + interviewee_id(u.id) + end +end diff --git a/spec/support/models/interview.cr b/spec/support/models/interview.cr new file mode 100644 index 000000000..b42025886 --- /dev/null +++ b/spec/support/models/interview.cr @@ -0,0 +1,9 @@ +class Interview < BaseModel + table do + belongs_to interviewer : User + belongs_to interviewee : User + end +end + +class InterviewQuery < Interview::BaseQuery +end