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

Correct ransack_alias to work with form #1525

Open
wants to merge 1 commit 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
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
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16
cache: yarn
Expand All @@ -29,7 +29,7 @@ jobs:
working-directory: docs

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/build
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
4 changes: 2 additions & 2 deletions .github/workflows/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16
cache: yarn
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
26 changes: 16 additions & 10 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
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)
def extract(context, name, values)
attributes, predicate, combinator =
extract_values_for_condition(key, context)
extract_values_for_condition(name, context)

if attributes.size > 0 && predicate
condition = self.new(context)
condition.build(
a: attributes,
p: predicate.name,
m: combinator,
v: predicate.wants_array ? Array(values) : [values]
v: predicate.wants_array ? Array(values) : [values],
n: name
)
# 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 @@ -193,12 +197,13 @@ def eql?(other)
self.attributes == other.attributes &&
self.predicate == other.predicate &&
self.values == other.values &&
self.combinator == other.combinator
self.combinator == other.combinator &&
self.name == other.name
end
alias :== :eql?

def hash
[attributes, predicate, values, combinator].hash
[attributes, predicate, values, combinator, name].hash
end

def predicate_name=(name)
Expand Down Expand Up @@ -271,7 +276,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
21 changes: 19 additions & 2 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ def conditions=(conditions)
alias :c= :conditions=

def [](key)
conditions.detect { |c| c.key == key.to_s }
conditions.detect do |c|
matched_condition(c, key)
end
end

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

self.conditions << value
end

def matched_condition(c, key)
c.name == key.to_s
end

def values
conditions + groupings
end
Expand Down Expand Up @@ -107,6 +116,14 @@ def groupings=(groupings)
end
alias :g= :groupings=

def respond_to_missing?(method_id, include_private = false)
method_name = method_id.to_s.dup
writer = method_name.sub!(/\=$/, ''.freeze)
return true if attribute_method?(method_name)

super
end

def method_missing(method_id, *args)
method_name = method_id.to_s.dup
writer = method_name.sub!(/\=$/, ''.freeze)
Expand Down
9 changes: 9 additions & 0 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ def new_sort(opts = {})
Nodes::Sort.new(@context).build(opts)
end

def respond_to_missing?(method_id, include_private = false)
method_name = method_id.to_s
getter_name = method_name.sub(/=$/, ''.freeze)
return true if base.attribute_method?(getter_name)
return true if @context.ransackable_scope?(getter_name, @context.object)

super
end

def method_missing(method_id, *args)
method_name = method_id.to_s
getter_name = method_name.sub(/=$/, ''.freeze)
Expand Down
21 changes: 21 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,27 @@ 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 nil

s = Person.ransack(daddy_cont: 'babi')
expect(s.daddy_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("|`) I?LIKE '%different%'/i)
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)email("|`) I?LIKE '%different%'/i)
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)name("|`) I?LIKE '%atlo%'/i)
expect(s.result.to_sql).to include(/("|`)people("|`)\.("|`)email("|`) I?LIKE '%atlo%'/i)
end

it 'also works with associations' do
dad = Person.create!(name: 'Birdman')
son = Person.create!(name: 'Weezy', parent: dad)
Expand Down
5 changes: 4 additions & 1 deletion spec/ransack/nodes/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ module Nodes
Ransack.configure { |c| c.default_predicate = 'eq' }
end

specify { expect(subject).to eq Condition.extract(Context.for(Person), 'full_name_eq', Person.first.name) }
specify do
expect(subject).not_to be_nil
expect(subject.predicate.name).to eq 'eq'
end
end
end
end
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 @@ -689,6 +689,14 @@ def remove_quotes_and_backticks(str)
]
expect(@s.groupings.first.children_name_eq).to eq 'Ernie'
end

it 'respond_to? method_missing' do
@s.groupings = [
{ m: 'or', name_eq: 'Ernie', children_name_eq: 'Ernie' }
]
expect(@s.groupings.first.respond_to?(:children_name_eq)).to eq true
expect(@s.groupings.first.method(:children_name_eq)).to be_truthy
end
end
end
end