Module: SnFilterable::FilteredHelper

Included in:
ChipsComponent, FilterCategoryComponent
Defined in:
lib/sn_filterable.rb

Overview

View helper for [Filtered].

Defaults the URL of [Filtered] calls to use ‘url_for`.

See Also:

Instance Method Summary collapse

Instance Method Details

#add_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String

Helper function of [Filtered#add_filter_url]

Parameters:

  • filtered_instance (Filtered)
  • filter_key (String)
  • value (String)

Returns:

  • (String)


40
41
42
# File 'lib/sn_filterable.rb', line 40

def add_filter_url(filtered_instance, filter_key, value, url: url_for)
  filtered_instance.add_filter_url(url, filter_key, value)
end

#clear_all_url(filtered_instance, url: url_for) ⇒ String

Helper function of [Filtered#clear_all_url]

Parameters:

Returns:

  • (String)


83
84
85
# File 'lib/sn_filterable.rb', line 83

def clear_all_url(filtered_instance, url: url_for)
  filtered_instance.clear_all_url(url)
end

#clear_filter_url(filtered_instance, url: url_for) ⇒ String

Helper function of [Filtered#clear_filter_url]

Parameters:

Returns:

  • (String)


67
68
69
# File 'lib/sn_filterable.rb', line 67

def clear_filter_url(filtered_instance, url: url_for)
  filtered_instance.clear_filter_url(url)
end

#clear_sort_url(filtered_instance, url: url_for) ⇒ String

Helper function of [Filtered#clear_sort_url]

Parameters:

Returns:

  • (String)


75
76
77
# File 'lib/sn_filterable.rb', line 75

def clear_sort_url(filtered_instance, url: url_for)
  filtered_instance.clear_sort_url(url)
end

#filtered_form_inputs(filtered_instance) ⇒ Object

Creates hidden inputs elements representing the current filter / sort values. Useful for forms when we want to retain the user’s current filtering parameters.

When used on a form, this should be rendered first to avoid conflicts with other form inputs defined. Duplicate input name’s may exist but can be ignored as the last duplicate parameter occurrence takes precedence.

Parameters:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sn_filterable.rb', line 131

def filtered_form_inputs(filtered_instance)
  inputs = sanitize("")

  filtered_instance.queries.each do |key, value|
    case value
    when String
      inputs << (:input, "", type: :hidden, name: key, value: value)
    when Hash
      value.each do |subitem|
        inputs << (:input, "", type: :hidden, name: "#{key}[#{subitem[0]}]", value: subitem[1])
      end
    end
  end

  inputs
end

#filtered_form_with(filtered_instance, options = {}, &block) ⇒ Object

Creates a form tag for filtered instances. See [FilteredFormBuilder] for the helper methods

Parameters:

See Also:

  • FilteredFormBuilder


153
154
155
156
157
158
159
# File 'lib/sn_filterable.rb', line 153

def filtered_form_with(filtered_instance, options = {}, &block)
  options[:method] = :get
  options[:builder] = FilteredFormBuilder
  options[:filtered] = filtered_instance

  form_with options, &block
end

#filtered_paginate(filtered_instance) ⇒ Object

Renders a Tailwind paginated pagination section for a filtered instance. Uses kaminari’s paginate

Parameters:



165
166
167
# File 'lib/sn_filterable.rb', line 165

def filtered_paginate(filtered_instance)
  paginate filtered_instance.items, filtered: filtered_instance
end

#remove_filter_url(filtered_instance, filter_key, url: url_for) ⇒ String

Helper function of [Filtered#remove_filter_url]

Parameters:

  • filtered_instance (Filtered)
  • filter_key (String)

Returns:

  • (String)


49
50
51
# File 'lib/sn_filterable.rb', line 49

def remove_filter_url(filtered_instance, filter_key, url: url_for)
  filtered_instance.remove_filter_url(url, filter_key)
end

#remove_sub_filter_url(filtered_instance, filter_key, filter_value, url: url_for) ⇒ String

Helper function of [Filtered#remove_sub_filter_url]

Parameters:

  • filtered_instance (Filtered)
  • filter_key (String)
  • filter_value (String)

Returns:

  • (String)


59
60
61
# File 'lib/sn_filterable.rb', line 59

def remove_sub_filter_url(filtered_instance, filter_key, filter_value, url: url_for)
  filtered_instance.remove_sub_filter_url(url, filter_key, filter_value)
end

#set_filter_url(filtered_instance, filter_key, value, url: url_for) ⇒ String

Helper function of [Filtered#set_filter_url]

Parameters:

  • filtered_instance (Filtered)
  • filter_key (String)
  • value (String)

Returns:

  • (String)


30
31
32
# File 'lib/sn_filterable.rb', line 30

def set_filter_url(filtered_instance, filter_key, value, url: url_for)
  filtered_instance.set_filter_url(url, filter_key, value)
end

#sort_url(filtered_instance, sort_key, order: nil, url: url_for, &block) ⇒ String

Helper function of [Filtered#sort_url]

Parameters:

  • filtered_instance (Filtered)
  • sort_key (String)
  • order (Symbol, nil) (defaults to: nil)

Returns:

  • (String)


93
94
95
# File 'lib/sn_filterable.rb', line 93

def sort_url(filtered_instance, sort_key, order: nil, url: url_for, &block)
  filtered_instance.sort_url(url, sort_key, order, &block)
end

Creates an anchor element using ‘sort_url` and the block. Additionally appends a suffix containing a triangle symbol representing the direction of the sort if we are currently sorting the items of [Filtered] using the `sort_key`.

Parameters:

  • filtered_instance (Filtered)
  • sort_key (String)

    The sorting key to toggle

  • in_filterable_component (Boolean) (defaults to: true)

    If true, will add filterable component Alpine scripts



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sn_filterable.rb', line 105

def table_header_sort_link(filtered_instance, sort_key, scope: nil, html_options: nil, url: url_for, in_filterable_component: true, &block)
  url, state = filtered_instance.sort_url(url, sort_key, scope: scope)

  link_html_options = in_filterable_component ? { "@click": "filtersLoading = true", "class": "whitespace-nowrap" } : {}
  link_html_options = link_html_options.merge(html_options) if html_options.present?

  suffix = sanitize case state
                    when :asc
                      "&nbsp;&#9650;"
                    when :desc
                      "&nbsp;&#9660;"
                    else
                      ""
          end

  link_to(capture(&block) + suffix, url, link_html_options)
end