Skip to content

Commit 08bddc3

Browse files
fix: extra_params is now retained when sorting
1 parent 7a82a32 commit 08bddc3

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## [Unreleased]
1+
## See GitHub releases for changelog
22

33
## [0.1.0] - 2022-12-05
44

app/components/sn_filterable/main_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def initialize(frame_id:, filtered:, filters:, url: nil, search_filter_name: nil
3434
@show_sidebar = show_sidebar
3535
@update_url_on_submit = update_url_on_submit
3636
@extra_params = extra_params
37+
@filtered.extra_params = extra_params if extra_params.present?
3738
end
3839

3940
def search_field

lib/models/filtered.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
#
88
# @see Filterable
99
class Filtered
10-
attr_accessor :items, :queries
10+
attr_accessor :items, :queries, :extra_params
1111

1212
# @param [Class] model_class The class of the ActiveRecord::Base subclass
1313
# @param [ActiveRecord::Relation] items The items sorted and filtered by [Filterable]
1414
# @param [Hash] queries A hash of the sorting / filtering parameters
1515
# @param [Symbol] sort_name The current sorting name
1616
# @param [Boolean] sort_reversed True when the current sorting order is reversed
17-
def initialize(model_class, items, queries, sort_name, sort_reversed)
17+
# @param [Hash] extra_params Optional hash of additional parameters to include in URLs
18+
def initialize(model_class, items, queries, sort_name, sort_reversed, extra_params = {})
1819
@model_class = model_class
1920
@items = items
2021
@queries = queries
2122
@sort_name = sort_name
2223
@sort_reversed = sort_reversed
24+
@extra_params = extra_params || {}
2325
end
2426

2527
# Returns if any filters are active
@@ -162,6 +164,11 @@ def modify_url_queries(url)
162164
uri = URI.parse(url)
163165
query = Rack::Utils.parse_nested_query(uri.query).deep_merge(@queries.deep_dup)
164166

167+
# Add extra_params to the URL
168+
@extra_params.each do |key, value|
169+
query[key.to_s] = value
170+
end if @extra_params.present?
171+
165172
yield(query) if block_given?
166173

167174
uri.query = Rack::Utils.build_nested_query(query)

lib/sn_filterable/filterable.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ module Filterable
2626
# @param [ActiveRecord::Relation] items Optional, the items to scope from the model
2727
# @param [String, Array, nil] default_sort Optional, similar to the `DEFAULT_SORT` constant, sets the default sort of items when no sorting parameter is set. Can be either a [String], which returns the sorting name or an [Array], where the first item is the sorting name and the second item is the sort direction (either `:asc` or `:desc`). Will take precedence over the `DEFAULT_SORT` constant.
2828
# @param [Boolean] pagination_enabled Optional, toggles pagination
29+
# @param [Hash] extra_params Optional, allows for custom query parameters to be included in URLs
2930
# @return [Filtered] the filtered and sorted items
30-
def filter(params:, items: where(nil), default_sort: nil, pagination_enabled: true)
31+
def filter(params:, items: where(nil), default_sort: nil, pagination_enabled: true, extra_params: {})
3132
filter_params = filter_params(params)
3233
sort_params = sort_params(params)
3334
other_params = other_params(params, items)
3435
items, sort_name, reverse_order = perform_sort(items, sort_params, default_sort)
3536
items = perform_filter(items, filter_params)
3637
items = items.page(other_params[:page]).per(other_params[:per]) if pagination_enabled
3738

38-
Filtered.new(self, items, generate_url_queries(filter_params, sort_params, other_params), sort_name, reverse_order)
39+
Filtered.new(self, items, generate_url_queries(filter_params, sort_params, other_params), sort_name, reverse_order, extra_params)
3940
end
4041

4142
private

0 commit comments

Comments
 (0)