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 d010162 commit aa2062c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ def key
"_#{predicate.name}"
end

def same_name_or_key?(val)
return false 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
17 changes: 15 additions & 2 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,27 @@ def conditions=(conditions)
alias :c= :conditions=

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

def []=(key, value)
conditions.reject! { |c| c.same_name_or_key?(key) }
conditions.reject! do |c|
matched_condition(c, key.to_s, named_condition?(key.to_s))
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)
end

def values
conditions + groupings
end
Expand Down

0 comments on commit aa2062c

Please sign in to comment.