Module: WithFilters::ActionViewExtension

Defined in:
lib/with_filters/action_view_extension.rb

Instance Method Summary collapse

Instance Method Details

#filter_form_for(records, options = {}) {|filter_form| ... } ⇒ Object

Create a filter form.

Examples:

<%= filter_form_for(@data, theme: 'foo', class: 'bar') do |f|
  f.input(:full_name)
  f.input(:email)
end %>

Parameters:

  • records (ActiveRecord::Relation, Array)

    The data being filtered.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :theme (String)

    The theme to use when rendering the form.

  • remaining (Varied) — default: "{'novalidate: 'novalidate', method: 'get'}"

    All other options are passed as options to the form_tag helper.

Yields:

  • (filter_form)

Since:

  • 0.1.0



20
21
22
23
24
25
# File 'lib/with_filters/action_view_extension.rb', line 20

def filter_form_for(records, options = {})
  filter_form = WithFilters::FilterForm.new(records, self.extract_hash_value(params, records.with_filters_data[:param_namespace]) || {}, options)
  yield(filter_form)

  render(partial: filter_form.to_partial_path, locals: {filter_form: filter_form})
end

#with_filters_action_tag(action) ⇒ Object

Parameters:

Since:

  • 0.1.0



101
102
103
104
105
106
107
108
# File 'lib/with_filters/action_view_extension.rb', line 101

def with_filters_action_tag(action)
  case action.type
  when :submit
    submit_tag(action.attrs.delete(:value), action.attrs)
  when :reset
    button_tag(action.attrs.delete(:value) || 'Reset', action.attrs)
  end
end

#with_filters_hidden(hidden_filters) ⇒ Object

Create hidden inputs.

Parameters:

  • hidden_filters (Array<Filter>)

Since:

  • 0.1.0



32
33
34
35
36
# File 'lib/with_filters/action_view_extension.rb', line 32

def with_filters_hidden(hidden_filters)
  hidden_filters.map{|hidden_filter|
    hidden_field_tag(hidden_filter.field_name, hidden_filter.value, hidden_filter.attrs)
  }.join("\n").html_safe
end

#with_filters_input(filter) ⇒ Object

Create an input based on the type of filter provided.

Parameters:

  • filter (Filter)

Since:

  • 0.1.0



43
44
45
# File 'lib/with_filters/action_view_extension.rb', line 43

def with_filters_input(filter)
  render(partial: filter.to_partial_path, locals: {filter: filter})
end

#with_filters_label(filter) ⇒ Object

Create a label tag for individual fields or a div tag in its place in the case of fields where the label tags are used for individual items.

Parameters:

  • filter (Filter)

Since:

  • 0.1.0



71
72
73
74
75
76
77
# File 'lib/with_filters/action_view_extension.rb', line 71

def with_filters_label(filter)
  if [WithFilters::Filter::Radio, WithFilters::Filter::CheckBox].include?(filter.class) and filter.collection.any?
    (:div, filter.label, filter.label_attrs)
  else
    with_filters_label_tag(filter)
  end
end

#with_filters_label_tag(filter) ⇒ Object

Create a label tag based on the filter.

Parameters:

  • filter (Filter)

Since:

  • 0.1.0



61
62
63
# File 'lib/with_filters/action_view_extension.rb', line 61

def with_filters_label_tag(filter)
  label_tag(filter.field_name, filter.label, filter.label_attrs)
end

#with_filters_select_tag(filter) ⇒ Object

Create a select tag based on the filter.

Parameters:

  • filter (Filter)

Since:

  • 0.1.0



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/with_filters/action_view_extension.rb', line 84

def with_filters_select_tag(filter)
  collection = filter.collection
  unless filter.collection.is_a?(String)
    collection = filter.collection.map do |choice|
      html_attributes = choice.attrs.length > 0 ? ' ' + choice.attrs.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : ''
      selected_attribute = choice.selected? ? ' selected="selected"' : ''

      %(<option value="#{ERB::Util.html_escape(choice.value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(choice.label)}</option>)
    end.join("\n")
  end

  select_tag(filter.field_name, collection.html_safe, filter.attrs)
end

#with_filters_text_field_tag(filter) ⇒ Object

Create an text like input tag based on the filter.

Parameters:

  • filter (Filter)

Since:

  • 0.1.0



52
53
54
# File 'lib/with_filters/action_view_extension.rb', line 52

def with_filters_text_field_tag(filter)
  text_field_tag(filter.field_name, filter.value, filter.attrs)
end