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

Check type before sending #value message to predicate #1468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def in_predicate?(predicate)
end

def casted_array?(predicate)
predicate.value.is_a?(Array) && predicate.is_a?(Arel::Nodes::Casted)
predicate.is_a?(Arel::Nodes::Casted) && predicate.value.is_a?(Array)
end

def format_values_for(predicate)
Expand Down
9 changes: 9 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ def self.simple_escaping?
expect(s.result.map(&:id)).to eq [3, 2, 1]
end

it 'should function correctly with HABTM associations' do
article = Article.first
tag = article.tags.first
s = Person.ransack(article_tags_in: [tag.id])

expect(s.result.count).to be 1
expect(s.result.map(&:id)).to eq [article.person.id]
end

it 'should function correctly when passing an array of strings' do
a, b = Person.select(:id).order(:id).limit(2).map { |a| a.id.to_s }

Expand Down
11 changes: 11 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ class Person < ApplicationRecord
Arel.sql(query)
end

ransacker :article_tags, formatter: proc { |id|
if Tag.exists?(id)
joins(articles: :tags)
.where(tags: { id: id })
.distinct
.select(:id).arel
end
} do |parent|
parent.table[:id]
end

def self.ransackable_attributes(auth_object = nil)
if auth_object == :admin
authorizable_ransackable_attributes - ['only_sort']
Expand Down