Module: ActsAsDataTableHelper

Defined in:
app/helpers/acts_as_data_table_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_scope_filter(group) ⇒ Object



127
128
129
# File 'app/helpers/acts_as_data_table_helper.rb', line 127

def active_scope_filter(group)
  acts_as_data_table_session.active_filter(group)
end

#scope_filter_arg(group, scope, name) ⇒ Object, NilClass

Looks up a set argument for a given filter, e.g. to highlight it in the search results

Returns:

  • (Object, NilClass)

    the argument used for the given scope or nil if the filter is currently not active



108
109
110
# File 'app/helpers/acts_as_data_table_helper.rb', line 108

def scope_filter_arg(group, scope, name)
  Acts::DataTable.lookup_nested_hash(acts_as_data_table_session.active_filters, group.to_s, scope.to_s, name.to_s)
end

#scope_filter_args(group, scope) ⇒ Hash

Returns Arguments used for the given filter.

Returns:

  • (Hash)

    Arguments used for the given filter



115
116
117
# File 'app/helpers/acts_as_data_table_helper.rb', line 115

def scope_filter_args(group, scope)
  Acts::DataTable.lookup_nested_hash(acts_as_data_table_session.active_filters, group.to_s, scope.to_s) || {}
end

#scope_filter_caption(group, scope, args = {}) ⇒ Object

Generates a scope filter caption



122
123
124
125
# File 'app/helpers/acts_as_data_table_helper.rb', line 122

def scope_filter_caption(group, scope, args = {})
  model = Acts::DataTable::ScopeFilters::ActionController.get_request_model
  Acts::DataTable::ScopeFilters::ActiveRecord.scope_filter_caption(model, group, scope, args)
end

#scope_filter_form(group, scope, options = {}, &proc) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/acts_as_data_table_helper.rb', line 79

def scope_filter_form(group, scope, options = {}, &proc)
  content = capture(Acts::DataTable::ScopeFilters::FormHelper.new(self, group, scope), &proc)
  method  = options[:method] || :get
  if options.delete(:remote)
    options.delete(:method)
    form_remote_tag :url => scope_filter_form_url(group, scope), :method => method, :html => options do
      concat(content)
    end
  else
    form_tag scope_filter_form_url(group, scope), options do
      concat(content)
    end
  end
end

#scope_filter_form_url(group, scope) ⇒ String

Generates a URL to be used as form action for filters which require dynamic arguments

Examples:

Using the rails form helper with a scope filter url


- form_tag(scope_filter_form_url) do
  ...

Returns:

  • (String)

    the generated URL



74
75
76
# File 'app/helpers/acts_as_data_table_helper.rb', line 74

def scope_filter_form_url(group, scope)
  url_for({:scope_filters => {:action => 'add', :group => group, :scope => scope}})
end

Generates a link to add/remove a certain scope filter

Parameters:

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

    Options to customize the generated link

Options Hash (options):

  • :scope (String, Symbol)

    The scope within the given group to be added/removed

  • :toggle (TrueClass, FalseClass) — default: false

    If set to true, the link will automatically remove the filter if it’s currently active without having to explicitly set :remove

  • :remove (TrueClass, FalseClass) — default: false

    If set to true, the link will always attempt to remove the given :scope within the given group and cannot be used to add it.



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

def scope_filter_link(group, scope, options = {})
  caption         = options.delete(:caption) || scope_filter_caption(group, scope, options[:args])
  surrounding_tag = options.delete(:surrounding)
  remote          = options.delete(:remote)
  args            = options[:args]
  url             = scope_filter_link_url(group, scope, options)

  classes = options[:class].try(:split, ' ') || []
  classes << 'active' if scope && acts_as_data_table_session.active_filter?(group, scope, args)

  options[:class] = classes.join(' ')

  if remote
    link = link_to_remote caption, :url => url, :method => :get, :html => options
  else
    link = link_to caption, url, options
  end

  surrounding_tag ? (surrounding_tag, link, :class => options[:class]) : link
end

Generates a URL to add/remove/toggle a given scope filter

Returns:

  • (String)

    The generated URL

See Also:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/acts_as_data_table_helper.rb', line 50

def scope_filter_link_url(group, scope, options)
  args            = options.delete(:args)
  toggle          = options.delete(:toggle)
  auto_remove     = scope && toggle && acts_as_data_table_session.active_filter?(group, scope, args)
  remove          = options.delete(:remove) || auto_remove

  if remove
    url_for({:scope_filters => {:action => 'remove', :group => group}})
  else
    url_for({:scope_filters => {:action => 'add', :group => group, :scope => scope, :args => args}})
  end
end

#scope_filter_reset_urlString

Returns URL to remove all active scope filters for the current action.

Returns:

  • (String)

    URL to remove all active scope filters for the current action



97
98
99
# File 'app/helpers/acts_as_data_table_helper.rb', line 97

def scope_filter_reset_url
  url_for({:scope_filters => {:action => :reset}})
end

#sortable_column(model, column, caption, options = {}, &proc) ⇒ Object


Sortable Columns



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/helpers/acts_as_data_table_helper.rb', line 135

def sortable_column(model, column, caption, options = {}, &proc)
  renderer_class = options.delete(:renderer) || Acts::DataTable::SortableColumns::Renderers.default_renderer

  sortable                 = sortable_column_data(model, column)
  sortable[:html_options]  = options
  sortable[:caption]       = caption
  sortable[:remote]        = options.delete(:remote)
  sortable[:remote]        = true if sortable[:remote].blank?
  sortable                 = OpenStruct.new(sortable)

  #If a block is given, we let the user handle the content of the table
  #header himself. Otherwise, we'll generate the default links.
  if block_given?
    yield sortable
  else
    renderer = renderer_class.is_a?(Class) ? renderer_class : renderer_class.constantize
    renderer.new(sortable, self).to_html
  end
end

#sortable_column_data(model, column) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/acts_as_data_table_helper.rb', line 155

def sortable_column_data(model, column)
  sortable                = {}

  sortable[:direction]    = acts_as_data_table_session.sorting_direction(model, column)
  sortable[:active]       = acts_as_data_table_session.active_column?(model, column)

  urls                    = {}
  urls[:toggle]           = url_for({:sortable_columns => {:action => :toggle, :model => model, :column => column}})
  urls[:change_direction] = url_for({:sortable_columns => {:action => :change_direction, :model => model, :column => column}})
  urls[:set_base]         = url_for({:sortable_columns => {:action => :set_base, :model => model, :column => column}})
  sortable[:urls]         = OpenStruct.new(urls)

  sortable
end