diff --git a/.github/workflows/cronjob.yml b/.github/workflows/cronjob.yml index 941d3a5b..ccb0e3d7 100644 --- a/.github/workflows/cronjob.yml +++ b/.github/workflows/cronjob.yml @@ -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: @@ -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: @@ -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: diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index 7d68b9f6..ce26b50e 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -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: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0025eb93..66d31d77 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index af69c037..31287ae3 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -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) @@ -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. @@ -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 @@ -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 @@ -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 && @@ -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]}" } diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 1bf70550..9f51d10b 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -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 diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index d41cd6d0..e80ba5e2 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -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)