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

Fixes polymorphic joins. #1122

Merged
Merged
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
21 changes: 15 additions & 6 deletions lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
module Polyamorous
module ReflectionExtensions
def build_join_constraint(table, foreign_table)
if polymorphic?
super(table, foreign_table)
.and(foreign_table[foreign_type].eq(klass.name))
else
super(table, foreign_table)
if ActiveRecord.version > ::Gem::Version.new('5.2.3')
def join_scope(table, foreign_table, foreign_klass)
if respond_to?(:polymorphic?) && polymorphic?
super.where!(foreign_table[foreign_type].eq(klass.name))
else
super
end
end
else
def build_join_constraint(table, foreign_table)
if polymorphic?
super.and(foreign_table[foreign_type].eq(klass.name))
else
super
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ module Ransack
let(:children_people_name_field) {
"#{quote_table_name("children_people")}.#{quote_column_name("name")}"
}
let(:notable_type_field) {
"#{quote_table_name("notes")}.#{quote_column_name("notable_type")}"
}
it 'evaluates conditions contextually' do
s = Search.new(Person, children_name_eq: 'Ernie')
expect(s.result).to be_an ActiveRecord::Relation
Expand Down Expand Up @@ -328,6 +331,7 @@ module Ransack
s = Search.new(Note, notable_of_Person_type_name_eq: 'Ernie').result
expect(s).to be_an ActiveRecord::Relation
expect(s.to_sql).to match /#{people_name_field} = 'Ernie'/
expect(s.to_sql).to match /#{notable_type_field} = 'Person'/
end

it 'evaluates nested conditions' do
Expand Down