Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Auto alias joins #1012

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions db/migrations/20240317161918_create_interviews.cr
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions spec/avram/query_associations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,19 @@ describe "Query associations" do

result.should eq(product)
end

it "handles aliases" do
interviewer = UserFactory.create(&.available_for_hire(false).name("Interviewer"))
interviewee = UserFactory.create(&.available_for_hire(true).name("Interviewee"))
employed = UserFactory.create(&.available_for_hire(false).name("Employed"))
InterviewFactory.create(&.interviewee(interviewee).interviewer(interviewer))
InterviewFactory.create(&.interviewee(employed).interviewer(interviewer))

InterviewQuery.new
.join(Avram::Join::Inner.new(:interviews, :users, alias_to: :interviewers, primary_key: :interviewer_id, foreign_key: :id))
.join(Avram::Join::Inner.new(:interviews, :users, alias_to: :interviewees, primary_key: :interviewee_id, foreign_key: :id))
.where_interviewer(UserQuery.new("interviewers").available_for_hire(false), auto_inner_join: false)
.where_interviewee(UserQuery.new("interviewees").available_for_hire(true), auto_inner_join: false)
.select_count.should eq(1)
end
end
20 changes: 20 additions & 0 deletions spec/support/factories/interview_factory.cr
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/support/models/interview.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Interview < BaseModel
table do
belongs_to interviewer : User
belongs_to interviewee : User
end
end

class InterviewQuery < Interview::BaseQuery
end
5 changes: 5 additions & 0 deletions src/avram/base_query_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class Avram::BaseQueryTemplate
def_clone
include Avram::Queryable({{ type }})

getter table_name : String = {{ type }}.table_name

def initialize(@table_name : String = {{ type }}.table_name)
end

{% if type.resolve.has_constant?("PRIMARY_KEY_NAME") %}
include Avram::PrimaryKeyQueryable({{ type }})
{% end %}
Expand Down
2 changes: 1 addition & 1 deletion src/avram/queryable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Avram::Queryable(T)
.select(schema_class.column_names)
end

delegate :database, :table_name, :primary_key_name, to: T
delegate :database, :primary_key_name, to: T

macro included
def self.new_with_existing_query(query : Avram::QueryBuilder)
Expand Down
Loading