From 6840f4a08fac27fba379c67c31b14204eed8b0f6 Mon Sep 17 00:00:00 2001 From: Doug Edey Date: Thu, 3 Oct 2024 12:27:17 -0400 Subject: [PATCH] Add support for Ransack::Nodes::Grouping#read_attribute to use ransack aliases --- lib/ransack/nodes/grouping.rb | 7 +++++++ spec/ransack/nodes/grouping_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 1bf705508..aeee6ae74 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -178,6 +178,13 @@ def write_attribute(name, val) end def read_attribute(name) + if self[name].nil? + stripped_name = name.dup + predicate = Predicate.detect_and_strip_from_string!(stripped_name) + aliased_attribute = context.ransackable_alias(stripped_name) + name = "#{aliased_attribute}_#{predicate}" unless aliased_attribute == stripped_name + end + if self[name].respond_to?(:value) self[name].value else diff --git a/spec/ransack/nodes/grouping_spec.rb b/spec/ransack/nodes/grouping_spec.rb index 2f23baf7f..72f1ede6c 100644 --- a/spec/ransack/nodes/grouping_spec.rb +++ b/spec/ransack/nodes/grouping_spec.rb @@ -106,6 +106,23 @@ module Nodes end end + describe "#read_attribute" do + let(:conditions) do + { + '0' => { + 'a' => { '0'=> { 'name' => 'parent_name', 'ransacker_args' => '' } }, + 'p' => 'cont', + 'v' => { '0' => { 'value' => 'John' } } + }, + } + end + before { subject.conditions = conditions } + + it "works with aliases" do + expect(subject.parent_name_cont).to eq("John") + expect(subject.daddy_cont).to eq("John") + end + end end end end