Module: PureAdmin::TableFiltersHelper

Defined in:
app/helpers/pure_admin/table_filters_helper.rb

Overview

Helper methods for table filters functionality.

The recommended way of using this helper is

 filters_for admin_members_path do |filter|
   filter.on :query                           the default is a text field
   filter.on :answer, options: %w(Yes No)     if given an options hash, a select will be used
   filter.on :status, as: :active_status      when given :active_status it will use the options
                                                All, Active, and Inactive and the default of
                                                Active. However these can be overridden by
                                                :options and :default respectively.
   filter.on :starts_on, as: :date            when given :date, it will enable the pure_date
                                                input type
end

Defined Under Namespace

Classes: TableFiltersBuilder

Instance Method Summary collapse

Instance Method Details

#filters_for(path, options = {}) { ... } ⇒ Object

Renders a table filters section to the view using the table filter builder DSL.

Parameters:

  • resource (ActiveRecord::Base)

    the model instance to build the table filters for.

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

    all options that can be passed to content_tag are respected here.

Yields:

  • The contents of the table filters section



113
114
115
116
117
118
# File 'app/helpers/pure_admin/table_filters_helper.rb', line 113

def filters_for(path, options = {}, &block)
  builder = TableFiltersBuilder.new(self)
  table_filters(path, options) do
    capture(builder, &block)
  end
end

#table_filter_item(attribute, options = {}) { ... } ⇒ Object

Renders a table filter item to the view.

Parameters:

  • attribute (String, Symbol)
  • options (Hash) (defaults to: {})

Yields:

  • The contents of the table filter with the label (unless options is false)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/pure_admin/table_filters_helper.rb', line 57

def table_filter_item(attribute, options = {}, &block)
  if block_given?
    field = capture(&block)
  else
    type = options.delete(:as) || :string

    # If we're given an options array, we assume it to be a select filter.
    # This allows for more concise code in the form
    #   table_filter_item :status, options: %w(Yes No)
    # or
    #   filter.on :status, options %W(Yes No)
    type = :select if options[:options].present?

    # We define the :active_status type to shortcut a common filter type.
    if type == :active_status
      type = :select
      options[:options] ||= %w(All Active Inactive)
      options[:default] ||= 'Active'
    end

    input_html = options.delete(:input_html) || {}
    input_html[:class] = merge_html_classes('filter-control', input_html[:class])

    # Defaulting to a simple text field, we choose which field to output here.
    # @note if at some point we wish to add a new field type, here is the place to add it
    case type
      when :select
        options[:options] ||= []
        field = select_tag(attribute,
          options_for_select(options[:options], params[attribute.to_sym] || options[:default]),
          input_html)
      when :date
        input_html[:class] = merge_html_classes('pure-admin-date', input_html[:class])
        field = text_field_tag(attribute, params[attribute.to_sym], input_html)
        icon = (:span, nil, class: 'input-addon fa fa-fw fa-calendar')
        field = (:div, icon + field, class: 'addon-wrapper')
      else # :string
        field = text_field_tag(attribute, params[attribute.to_sym], input_html)
    end
  end

  label = ''.html_safe
  if options[:label] != false
    label_html = options.delete(:label_html) || {}
    label_text = options.delete(:label) || attribute.to_s.titleize
    label = label_tag(attribute, label_text, label_html)
  end

  (:div, label + field, class: 'filter-group')
end

#table_filters(path, options = {}) { ... } ⇒ Object

Renders the table filters form to the view.

Parameters:

  • path (String)

    the path for the form_tag

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

    all options that can be passed to form_tag are respected here.

Yields:

  • The contents for the table filters



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/pure_admin/table_filters_helper.rb', line 22

def table_filters(path, options = {}, &block)
  options[:class] = merge_html_classes('pure-form pure-form-stacked table-filters
    js-partial-refresh clear-fix', options[:class])

  options[:remote] = false || options[:remote]
  options[:method] ||= :get

  content = capture(&block)

  if content
    content << (:div, submit_tag('Go', class: 'pure-button pure-button-primary'),
      class: 'filter-group filter-submit')
    content << hidden_field_tag(:sort, params[:sort])
    content << hidden_field_tag(:reverse_order, params[:reverse_order])

    form_tag path, options do
      content
    end
  end
end