From b338cd40f06607fcb9be0c8c521ba11c07560f6a Mon Sep 17 00:00:00 2001 From: Alexey Vasiliev Date: Sat, 14 Sep 2024 01:19:04 +0300 Subject: [PATCH] fix method_missing --- lib/ransack/nodes/grouping.rb | 8 ++++++++ lib/ransack/search.rb | 9 +++++++++ spec/ransack/search_spec.rb | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 9f51d10b..2b1a3620 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -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) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index a8bf95f8..512c54d9 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -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) diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 6426a20a..a5aa89e3 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -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