Module: Filterrific::ActionViewExtension

Includes:
HasResetFilterrificUrlMixin
Defined in:
lib/filterrific/action_view_extension.rb

Instance Method Summary collapse

Methods included from HasResetFilterrificUrlMixin

#reset_filterrific_url

Instance Method Details

Renders a link which indicates the current sorting and which can be used to toggle the list sorting (set column and direction).

NOTE: Make sure that this is used in the list partial that is re-rendered when the filterrific params are changed, so that the filterrific params in the URL are always current.

NOTE: Currently the filterrific_sorting_link is not synchronized with a SELECT input you may have in the filter form for sorting. We recommend you use one or the other to avoid conflicting sort settings in the UI.

Parameters:

  • filterrific (Filterrific::ParamSet)

    the current filterrific instance

  • sort_key (String, Symbol)

    the key to sort by, without direction. Example: ‘name’, ‘created_at’

  • opts (Hash, optional) (defaults to: {})


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
# File 'lib/filterrific/action_view_extension.rb', line 66

def filterrific_sorting_link(filterrific, sort_key, opts = {})
  opts = {
    :active_column_class => 'filterrific_current_sort_column',
    :inactive_column_class => 'filterrific_sort_column',
    :ascending_indicator => '',
    :default_sort_direction => 'asc',
    :descending_indicator => '',
    :html_attrs => {},
    :label => sort_key.to_s.humanize,
    :sorting_scope_name => :sorted_by,
    :url_for_attrs => {},
  }.merge(opts)
  opts.merge!(
    :html_attrs => opts[:html_attrs].with_indifferent_access,
    :current_sorting => (current_sorting = filterrific.send(opts[:sorting_scope_name])),
    :current_sort_key => current_sorting ? current_sorting.gsub(/_asc|_desc/, '') : nil,
    :current_sort_direction => current_sorting ? (current_sorting =~ /_desc\z/ ? 'desc' : 'asc') : nil,
    :current_sort_direction_indicator => (current_sorting =~ /_desc\z/ ? opts[:descending_indicator] : opts[:ascending_indicator]),
  )
  new_sort_key = sort_key.to_s
  if new_sort_key == opts[:current_sort_key]
    # same sort column, reverse order
    filterrific_sorting_link_reverse_order(filterrific, new_sort_key, opts)
  else
    # new sort column, default sort order
    filterrific_sorting_link_new_column(filterrific, new_sort_key, opts)
  end
end

#form_for_filterrific(record, options = {}, &block) ⇒ Object

Sets all options on form_for to defaults that work with Filterrific

Parameters:

  • record (Filterrific)

    the @filterrific object

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

    standard options for form_for

  • block (Proc)

    the form body



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/filterrific/action_view_extension.rb', line 14

def form_for_filterrific(record, options = {}, &block)
  options[:as] ||= :filterrific
  options[:html] ||= {}
  options[:html][:method] ||= :get
  options[:html][:id] ||= :filterrific_filter
  options[:url] ||= url_for(
    :controller => controller.controller_name,
    :action => controller.action_name
  )
  form_for(record, options, &block)
end

#render_filterrific_spinnerObject

Renders a spinner while the list is being updated



27
28
29
30
31
32
33
# File 'lib/filterrific/action_view_extension.rb', line 27

def render_filterrific_spinner
  %(
    <span class="filterrific_spinner" style="display:none;">
      #{ image_tag('filterrific/filterrific-spinner.gif') }
    </span>
  ).html_safe
end