|
|
module PagyHelper include Pagy::Frontend
# preferable to sort_link, prepend page=1 as second args (options like) automatically def pagy_sort_link(search_object, attribute, *args, &) prepend_args_with_common_pagy_options(args) sort_link(search_object, attribute.to_s, *args, &) end
# TODO: needs some refactoring def pagy_toggle_link(search_object, attribute, *args, &) params = preserve_q state = params.dig(:q, attribute).to_s == 'true' params[:q] = params[:q].merge(archived: !state)
url = url_for(params:)
# build_toggle_field_tag("q[#{attribute}]", t("actions.#{attribute}"), state, on_change: "Turbo.visit('#{url}')") build_toggle_field_tag("q[#{attribute}]", '', state, on_change: "Turbo.visit('#{url}')") end
# TODO: needs some refactoring def pagy_search_field(search_object, attribute, *args) params = preserve_q value = params.dig(:q, attribute)
args << {} unless args.last.is_a?(Hash) url = url_for(params:) url += url.include?('?') ? '&' : '?' url += "q[#{attribute}]=" args.last.merge!({ onkeypress: "if (event.key === \"Enter\") {Turbo.visit('#{url}'+event.target.value)}" })
search_field_tag("q[#{attribute}]", value, *args) end
# preserve ransack search parameters from default parameter 'q' def preserve_q(search_object = nil) { q: request.params[:q] || {} } end
# in case q exists and q[:sym] exists, its content is returned else nil def optional_q(sym) = request.params.fetch(:q, nil)&.fetch(sym, nil)
private
def prepend_args_with_common_pagy_options(args) args << {} unless args.last.is_a?(Hash) args.last.merge!( { page: 1, data: { 'turbo-action': 'advance' } } ) end end
|