You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

  1. module PagyHelper
  2. include Pagy::Frontend
  3. # preferable to sort_link, prepend page=1 as second args (options like) automatically
  4. def pagy_sort_link(search_object, attribute, *args, &)
  5. prepend_args_with_common_pagy_options(args)
  6. sort_link(search_object, attribute.to_s, *args, &)
  7. end
  8. # TODO: needs some refactoring
  9. def pagy_toggle_link(search_object, attribute, *args, &)
  10. params = preserve_q
  11. state = params.dig(:q, attribute).to_s == 'true'
  12. params[:q] = params[:q].merge(archived: !state)
  13. url = url_for(params:)
  14. # build_toggle_field_tag("q[#{attribute}]", t("actions.#{attribute}"), state, on_change: "Turbo.visit('#{url}')")
  15. build_toggle_field_tag("q[#{attribute}]", '', state, on_change: "Turbo.visit('#{url}')")
  16. end
  17. # TODO: needs some refactoring
  18. def pagy_search_field(search_object, attribute, *args)
  19. params = preserve_q
  20. value = params.dig(:q, attribute)
  21. args << {} unless args.last.is_a?(Hash)
  22. url = url_for(params:)
  23. url += url.include?('?') ? '&' : '?'
  24. url += "q[#{attribute}]="
  25. args.last.merge!({ onkeypress: "if (event.key === \"Enter\") {Turbo.visit('#{url}'+event.target.value)}" })
  26. search_field_tag("q[#{attribute}]", value, *args)
  27. end
  28. # preserve ransack search parameters from default parameter 'q'
  29. def preserve_q(search_object = nil)
  30. { q: request.params[:q] || {} }
  31. end
  32. # in case q exists and q[:sym] exists, its content is returned else nil
  33. def optional_q(sym) = request.params.fetch(:q, nil)&.fetch(sym, nil)
  34. private
  35. def prepend_args_with_common_pagy_options(args)
  36. args << {} unless args.last.is_a?(Hash)
  37. args.last.merge!(
  38. { page: 1,
  39. data: {
  40. 'turbo-action': 'advance'
  41. } }
  42. )
  43. end
  44. end