Class: Acts::DataTable::Shared::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_data_table/shared/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, controller_path, action_name) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
# File 'lib/acts_as_data_table/shared/session.rb', line 6

def initialize(session, controller_path, action_name)
  @session         = session
  @controller_path = controller_path
  @action_name     = action_name
end

Instance Method Details

#active_column?(model_name, column_name) ⇒ TrueClass, FalseClass

Returns true if the given column is currently used for sorting.

Returns:

  • (TrueClass, FalseClass)

    true if the given column is currently used for sorting



207
208
209
# File 'lib/acts_as_data_table/shared/session.rb', line 207

def active_column?(model_name, column_name)
  !!column_array(model_name, column_name)
end

#active_columnsArray

Returns The sorting columns for the current request (controller + action) in the format [[‘col1’, ‘dir1’], [‘col2’, ‘dir2’], …]

If no columns are set, the given default ones are used.

Returns:

  • (Array)

    The sorting columns for the current request (controller + action) in the format [[‘col1’, ‘dir1’], [‘col2’, ‘dir2’], …]

    If no columns are set, the given default ones are used.



142
143
144
# File 'lib/acts_as_data_table/shared/session.rb', line 142

def active_columns
  current_action_session(sc_session, [])
end

#active_filter(group) ⇒ String, NilClass

Returns The name of the scope filter which is currently active in the given group or nil if no filter from this group is currently active.

Returns:

  • (String, NilClass)

    The name of the scope filter which is currently active in the given group or nil if no filter from this group is currently active



62
63
64
# File 'lib/acts_as_data_table/shared/session.rb', line 62

def active_filter(group)
  Acts::DataTable.lookup_nested_hash(active_filters, group.to_s).try(:keys).try(:first)
end

#active_filter?(group, scope, args) ⇒ TrueClass, FalseClass

Checks whether the given filter is currently active Note that it also checks if it is active with exactly the given arguments.

Returns:

  • (TrueClass, FalseClass)

    true if the filter is active AND the given args match the ones used in the filter.



51
52
53
54
55
# File 'lib/acts_as_data_table/shared/session.rb', line 51

def active_filter?(group, scope, args)
  args ||= {}
  used_args = Acts::DataTable.lookup_nested_hash(active_filters, group.to_s, scope.to_s)
  used_args && (args.stringify_keys.to_a - used_args.to_a).empty?
end

#active_filtersHash

Returns all active filters for the current controller action by the group they are registered in.

Returns:

  • (Hash)

    all active filters for the current controller action by the group they are registered in.



40
41
42
# File 'lib/acts_as_data_table/shared/session.rb', line 40

def active_filters
  sf_session[current_action_key] || {}
end

#add_filter(group, scope, args) ⇒ Object

Adds a new filter to the current controller action Before the filter is added, the following things are checked to ensure that no invalid filter is added (which could cause reoccurring errors in the application)

1. The given +scope+ has to be registered in the currently used model
2. The given arguments have to be sufficient for the given +scope+
3. The +scope+ has to pass the set up validation check

Parameters:

  • group (String)

    The group the given scope is part of in the current #model

  • scope (String)

    The scope name within group and #model

  • args (Hash)

    Arguments to be passed to the scope. They have to be in the format => arg_value as set up in the model. This is necessary to make validations as easy as possible.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_data_table/shared/session.rb', line 86

def add_filter(group, scope, args)
  reset_errors!

  #Ensure that the argument hash is set properly. The following methods
  #might fail for filters which do not require arguments otherwise.
  args = {} unless args.is_a?(Hash)

  #Check whether the given filter was registered properly in the model
  unless Acts::DataTable::ScopeFilters::ActiveRecord.registered_filter?(model, group, scope)
    add_error group, Acts::DataTable.t('scope_filters.add_filter.filter_not_registered', :model => model.name, :group => group, :scope_name => scope)
    return false
  end
  
  #Check whether the given arguments are sufficient for the given filter
  unless Acts::DataTable::ScopeFilters::ActiveRecord.matching_arity?(model, group, scope, args.size)
    add_error group, Acts::DataTable.t('scope_filters.add_filter.non_matching_arity', :model => model.name, :group => group, :scope_name => scope)
    return false
  end

  #Run possible validation methods on the given filter and add generated error messages
  if (errors = Acts::DataTable::ScopeFilters::Validator.new(model, group, scope, args).validate).any?
    errors.each {|e| add_error(group, e)}
    return false
  end

  #Add the new filter to the session
  current_action_session[group.to_s] = {scope.to_s => args.stringify_keys}
  true
end

#change_direction!(model_name, column_name, direction = nil) ⇒ Object

Changes the sorting direction for the given column If no direction is given, it will change it to the opposite of the current direction



162
163
164
165
166
167
# File 'lib/acts_as_data_table/shared/session.rb', line 162

def change_direction!(model_name, column_name, direction = nil)
  if active_column?(model_name, column_name)
    ca    = column_array(model_name, column_name)
    ca[1] = (direction || opposite_direction(ca.last)).to_s.upcase
  end
end

#errorsObject



24
25
26
# File 'lib/acts_as_data_table/shared/session.rb', line 24

def errors
  @errors ||= {}
end

#errors_on(context) ⇒ Object



28
29
30
# File 'lib/acts_as_data_table/shared/session.rb', line 28

def errors_on(context)
  errors[context.to_s] || []
end

#modelObject



20
21
22
# File 'lib/acts_as_data_table/shared/session.rb', line 20

def model
  Acts::DataTable::ScopeFilters::ActionController.get_request_model
end

#remove_all_filters!Object

Resets all filters for the current controller action



128
129
130
# File 'lib/acts_as_data_table/shared/session.rb', line 128

def remove_all_filters!
  sf_session[current_action_key] = {}
end

#remove_filter!(group) ⇒ Object

Removes the given filter group from the current controller action It is sufficient to only specify the group here as only one filter in a group may be active at a time.



121
122
123
# File 'lib/acts_as_data_table/shared/session.rb', line 121

def remove_filter!(group)
  current_action_session.delete(group.to_s)
end

#sc_sessionObject



16
17
18
# File 'lib/acts_as_data_table/shared/session.rb', line 16

def sc_session
  @session[:sortable_columns] ||= {}
end

#set_base_column!(model, column, direction = nil) ⇒ Object

Replaces all current sorting columns with the given one



172
173
174
175
# File 'lib/acts_as_data_table/shared/session.rb', line 172

def set_base_column!(model, column, direction = nil)
  reset_columns!
  add_column!(model, column, direction)
end

#set_columns!(columns) ⇒ Object

Sets all sorting columns for the current controller action at once. This can be used when supplying the user with a form to choose the sorting in a separate area of the page instead of clicking on table column headers and adding one column after the other

Parameters:

  • columns (Array<String>)

    A 2D array of the form [[‘model_name’, ‘column_name’, ‘direction’], …]



186
187
188
189
190
191
# File 'lib/acts_as_data_table/shared/session.rb', line 186

def set_columns!(columns)
  reset_columns!
  columns.each do |model, column, direction|
    add_column!(model, column, direction)
  end
end

#sf_sessionObject



12
13
14
# File 'lib/acts_as_data_table/shared/session.rb', line 12

def sf_session
  @session[:scope_filters] ||= {}
end

#sorting_direction(model_name, column_name) ⇒ String, NilClass

Retrieves the current sorting direction for a given column.

Returns:

  • (String, NilClass)

    ‘ASC’ or ‘DESC’ if the given column is currently active, nil otherwise



199
200
201
# File 'lib/acts_as_data_table/shared/session.rb', line 199

def sorting_direction(model_name, column_name)
  column_array(model_name, column_name).try(:last)
end

#toggle_column!(model_name, column_name) ⇒ Object

Adds or removes a column from the current sorting columns This happens whenever the user decides to sort a table by multiple columns



150
151
152
153
154
155
156
# File 'lib/acts_as_data_table/shared/session.rb', line 150

def toggle_column!(model_name, column_name)
  if active_column?(model_name, column_name)
    remove_column!(model_name, column_name)
  else
    add_column!(model_name, column_name)
  end
end