Skip to content

Commit

Permalink
improve handling for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Sep 14, 2024
1 parent aa2062c commit dec8307
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 4 additions & 8 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,20 @@ def conditions=(conditions)

def [](key)
conditions.detect do |c|
matched_condition(c, key.to_s, named_condition?(key.to_s))
matched_condition(c, key)
end
end

def []=(key, value)
conditions.reject! do |c|
matched_condition(c, key.to_s, named_condition?(key.to_s))
matched_condition(c, key)
end

self.conditions << value
end

def matched_condition(c, key, named = false)
named ? c.name == key : c.key == key
end

def named_condition?(key)
conditions.map(&:name).include?(key)
def matched_condition(c, key)
c.name == key.to_s
end

def values
Expand Down
15 changes: 13 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,22 @@ module ActiveRecord
it 'return alias correctly from search' do
s = Person.ransack(term_cont: 'atlo')
expect(s.term_cont).to eq 'atlo'
expect(s.name_or_email_cont).to eq 'atlo'
expect(s.name_or_email_cont).to eq nil

s = Person.ransack(daddy_cont: 'babi')
expect(s.daddy_cont).to eq 'babi'
expect(s.parent_name_cont).to eq 'babi'
expect(s.parent_name_cont).to eq nil

s = Person.ransack(
term_cont: 'atlo',
name_or_email_cont: 'different'
)
expect(s.term_cont).to eq 'atlo'
expect(s.name_or_email_cont).to eq 'different'
expect(s.result.to_sql).to include('"people"."name" LIKE \'%different%\'')
expect(s.result.to_sql).to include('"people"."email" LIKE \'%different%\'')
expect(s.result.to_sql).to include('"people"."name" LIKE \'%atlo%\'')
expect(s.result.to_sql).to include('"people"."email" LIKE \'%atlo%\'')
end

it 'also works with associations' do
Expand Down

0 comments on commit dec8307

Please sign in to comment.