From 27242fe8ab5d78a93aa4cfa582f2aa61119bcc63 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 26 Jan 2021 09:33:03 -0500 Subject: [PATCH 1/5] (refactor) Tidy Up Sort Clause Construction --- lib/smart_listing.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/smart_listing.rb b/lib/smart_listing.rb index 329fd12..1771ac2 100644 --- a/lib/smart_listing.rb +++ b/lib/smart_listing.rb @@ -109,7 +109,10 @@ def setup params, cookies else # let's sort by all attributes # - @collection = @collection.order(sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}" if @sort[s[0]]}.compact) if @sort && !@sort.empty? + if @sort && !@sort.empty? + sorting = sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}" if @sort[s[0]]}.compact + @collection = @collection.order(sorting) + end if @options[:paginate] && @per_page > 0 @collection = @collection.page(@page).per(@per_page) From d0359f36f28bb7f5e403979007ae77a3556002fe Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 26 Jan 2021 09:34:12 -0500 Subject: [PATCH 2/5] (fix) Resolve Error with Sort Clause Construction Smart Listing supports three states for sorting: 1. "asc": ascending order using a given column of a table 2. "desc": descending order using a given column of a table 3. "": default ordering for the table (e.g. if you want to order by "name" by default, but allow sorting on "created at", you'd want this state to return the table to its default order) When attempting to use the default ordering _as requested by the user_ Smart Listing would construct a query attribute like this: `"fulfillment_order_sources.name "` (note extra space at the end). Rails 6.0 was okay with this, 6.1 is not. --- lib/smart_listing.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/smart_listing.rb b/lib/smart_listing.rb index 1771ac2..33f2bef 100644 --- a/lib/smart_listing.rb +++ b/lib/smart_listing.rb @@ -110,7 +110,7 @@ def setup params, cookies # let's sort by all attributes # if @sort && !@sort.empty? - sorting = sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}" if @sort[s[0]]}.compact + sorting = sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}".strip if @sort[s[0]]}.compact @collection = @collection.order(sorting) end From 96965df6c113d50d89f357e3dd2fccb569b32069 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Tue, 26 Jan 2021 09:41:00 -0500 Subject: [PATCH 3/5] (fix) Attribute Method Detection Rails 6.0 is okay with something like this: "fulfillment_order_sources.name" Rails 6.1 is not. You have to parse this first. --- lib/smart_listing.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/smart_listing.rb b/lib/smart_listing.rb index 33f2bef..4d15419 100644 --- a/lib/smart_listing.rb +++ b/lib/smart_listing.rb @@ -203,6 +203,16 @@ def set_param key, value, store = @params end end + def attribute_method?(klass, attr) + klass_name = klass.name.to_s.camelize + + attr = if attr.starts_with?(klass_name) && attr.include?('.') + attr.split('.')[1] + end + + klass.attribute_method?(attr) + end + def parse_sort sort_params sort = nil @@ -210,7 +220,8 @@ def parse_sort sort_params return sort if sort_params.blank? sort_params.map do |attr, dir| - key = attr.to_s if @options[:array] || @collection.klass.attribute_method?(attr) + key = attr.to_s if @options[:array] || attribute_method?(@collection.klass, attr) + if key && ALLOWED_DIRECTIONS[dir.to_s] sort ||= {} sort[key] = dir.to_s From ee03c0ef7e9e6cd93a0021b6b4a803f29aabccca Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 29 Jan 2021 15:58:07 -0500 Subject: [PATCH 4/5] Update Formatting to Make Justin Happy --- lib/smart_listing.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/smart_listing.rb b/lib/smart_listing.rb index 4d15419..f3a79e5 100644 --- a/lib/smart_listing.rb +++ b/lib/smart_listing.rb @@ -206,9 +206,7 @@ def set_param key, value, store = @params def attribute_method?(klass, attr) klass_name = klass.name.to_s.camelize - attr = if attr.starts_with?(klass_name) && attr.include?('.') - attr.split('.')[1] - end + attr = attr.split('.')[1] if attr.starts_with?(klass_name) && attr.include?('.') klass.attribute_method?(attr) end From 504a4734c88797897ec64db3a4370e4fc812b424 Mon Sep 17 00:00:00 2001 From: James Herdman Date: Mon, 1 Feb 2021 12:36:59 -0500 Subject: [PATCH 5/5] Update lib/smart_listing.rb Co-authored-by: Justin Giancola --- lib/smart_listing.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/smart_listing.rb b/lib/smart_listing.rb index f3a79e5..313b33a 100644 --- a/lib/smart_listing.rb +++ b/lib/smart_listing.rb @@ -206,7 +206,7 @@ def set_param key, value, store = @params def attribute_method?(klass, attr) klass_name = klass.name.to_s.camelize - attr = attr.split('.')[1] if attr.starts_with?(klass_name) && attr.include?('.') + attr = attr.split('.')[1] if attr.starts_with?("#{klass_name}.") klass.attribute_method?(attr) end