Skip to content

Commit

Permalink
fix method_missing
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Sep 13, 2024
1 parent 5a9676c commit b338cd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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
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

0 comments on commit b338cd4

Please sign in to comment.