Skip to content

Commit

Permalink
Correct usage ransack_alias in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Sep 13, 2024
1 parent 2d56e78 commit a41d4e9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cronjob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
DB: sqlite3
RAILS: main
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand All @@ -39,7 +39,7 @@ jobs:
MYSQL_USERNAME: root
MYSQL_PASSWORD: root
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
DB: sqlite3
RAILS: ${{ matrix.rails }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
MYSQL_USERNAME: root
MYSQL_PASSWORD: root
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down Expand Up @@ -103,7 +103,7 @@ jobs:
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand All @@ -118,7 +118,7 @@ jobs:
bug-report-templates:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
24 changes: 18 additions & 6 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Ransack
module Nodes
class Condition < Node
i18n_word :attribute, :predicate, :combinator, :value
i18n_word :attribute, :predicate, :combinator, :value, :name
i18n_alias a: :attribute, p: :predicate,
m: :combinator, v: :value
m: :combinator, v: :value, n: :name

attr_accessor :predicate
attr_accessor :predicate, :name

class << self
def extract(context, key, values)
Expand All @@ -18,7 +18,8 @@ def extract(context, key, values)
a: attributes,
p: predicate.name,
m: combinator,
v: predicate.wants_array ? Array(values) : [values]
v: predicate.wants_array ? Array(values) : [values],
n: key
)
# TODO: Figure out what to do with multiple types of attributes,
# if anything. Tempted to go with "garbage in, garbage out" here.
Expand Down Expand Up @@ -127,6 +128,9 @@ def combinator=(val)
alias :m= :combinator=
alias :m :combinator

alias :n= :name=
alias :n :name

# == build_attribute
#
# This method was originally called from Nodes::Grouping#new_condition
Expand Down Expand Up @@ -171,7 +175,7 @@ def value

def build(params)
params.with_indifferent_access.each do |key, value|
if key.match(/^(a|v|p|m)$/)
if key.match(/^(a|v|p|m|n)$/)
self.send("#{key}=", value)
end
end
Expand All @@ -188,6 +192,13 @@ def key
"_#{predicate.name}"
end

def has_name_or_key?(val)
return nil if val.nil?

val = val.to_s
name == val || key == val
end

def eql?(other)
self.class == other.class &&
self.attributes == other.attributes &&
Expand Down Expand Up @@ -271,7 +282,8 @@ def inspect
['attributes'.freeze, a.try(:map, &:name)],
['predicate'.freeze, p],
[Constants::COMBINATOR, m],
['values'.freeze, v.try(:map, &:value)]
['values'.freeze, v.try(:map, &:value)],
['name'.freeze, n]
]
.reject { |e| e[1].blank? }
.map { |v| "#{v[0]}: #{v[1]}" }
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def conditions=(conditions)
alias :c= :conditions=

def [](key)
conditions.detect { |c| c.key == key.to_s }
conditions.detect { |c| c.has_name_or_key?(key) }
end

def []=(key, value)
conditions.reject! { |c| c.key == key.to_s }
conditions.reject! { |c| c.has_name_or_key?(key) }
self.conditions << value
end

Expand Down
10 changes: 10 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ module ActiveRecord
expect(s.result.to_a).to eq []
end

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'

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

it 'also works with associations' do
dad = Person.create!(name: 'Birdman')
son = Person.create!(name: 'Weezy', parent: dad)
Expand Down

0 comments on commit a41d4e9

Please sign in to comment.