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

Fix handling attribute-name with _and_ _or_ #562

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Condition < Node

class << self
def extract(context, key, values)
attributes, predicate = extract_attributes_and_predicate(key)
attributes, predicate = extract_attributes_and_predicate(key, context)
if attributes.size > 0 && predicate
combinator = key.match(/_(or|and)_/) ? $1 : nil
condition = self.new(context)
Expand All @@ -31,14 +31,18 @@ def extract(context, key, values)

private

def extract_attributes_and_predicate(key)
def extract_attributes_and_predicate(key, context=nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surrounding space missing in default value assignment.

str = key.dup
name = Predicate.detect_and_strip_from_string!(str)
predicate = Predicate.named(name)
unless predicate || Ransack.options[:ignore_unknown_conditions]
raise ArgumentError, "No valid predicate for #{key}"
end
attributes = str.split(/_and_|_or_/)
if context.present? && context.attribute_method?(str)
attributes = [str]
else
attributes = str.split(/_and_|_or_/)
end
[attributes, predicate]
end
end
Expand Down
7 changes: 1 addition & 6 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,9 @@ def self.sane_adapter?
end

it "should function correctly when an attribute name has 'and' in it" do
# FIXME: this test does not pass!
p = Person.create!(:terms_and_conditions => true)
s = Person.ransack(:terms_and_conditions_eq => true)
# search is not detecting the attribute
puts "
FIXME: Search not detecting the `terms_and_conditions` attribute in
base_spec.rb, line 178: #{s.result.to_sql}"
# expect(s.result.to_a).to eq [p]
expect(s.result.to_a).to eq [p]
end

it 'allows sort by "only_sort" field' do
Expand Down