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

Fix issue evaluating polymorphic joins #1077

Closed
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
5 changes: 4 additions & 1 deletion polyamorous/lib/polyamorous.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ module Polyamorous
require "polyamorous/activerecord_#{ar_version}_ruby_2/#{file}"
end

ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions)
if ar_version >= "6.0.0"
require "polyamorous/activerecord_#{ar_version}_ruby_2/reflection.rb"
::ActiveRecord::Reflection::AbstractReflection.send(:prepend, Polyamorous::ReflectionExtensions)
end

Polyamorous::JoinDependency.send(:prepend, Polyamorous::JoinDependencyExtensions)
Polyamorous::JoinDependency.singleton_class.send(:prepend, Polyamorous::JoinDependencyExtensions::ClassMethods)
Expand Down
8 changes: 8 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ module Ransack
expect(s.to_sql).to match /#{people_name_field} = 'Ernie'/
end

it "evaluates polymorphic joins" do
s = Search.new(Person, article_notes_id_in: [1,2,3]).result
expect(s).to be_an ActiveRecord::Relation

notes_id_field = "#{quote_table_name("notes")}.#{quote_column_name("id")}"
expect(s.to_sql).to match /#{notes_id_field} IN \(1, 2, 3\)/
end

it 'evaluates nested conditions' do
s = Search.new(Person, children_name_eq: 'Ernie',
g: [
Expand Down
2 changes: 2 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Person < ActiveRecord::Base
has_many :authored_article_comments, through: :articles,
source: :comments, foreign_key: :person_id
has_many :notes, as: :notable
has_many :article_notes, through: :articles, source: :item, source_type: "Note"

scope :restricted, lambda { where("restricted = 1") }
scope :active, lambda { where("active = 1") }
Expand Down Expand Up @@ -132,6 +133,7 @@ class Article < ActiveRecord::Base
has_many :comments
has_and_belongs_to_many :tags
has_many :notes, as: :notable
belongs_to :item, polymorphic: true

alias_attribute :content, :body

Expand Down