Module: MightyGrid::Parameters::InstanceMethods

Defined in:
lib/mighty_grid/parameters.rb

Instance Method Summary collapse

Instance Method Details

#another_order_directionObject

Get another order direction



57
58
59
# File 'lib/mighty_grid/parameters.rb', line 57

def another_order_direction
  current_grid_params.key?('order_direction') ? (%w(asc desc) - [current_grid_params['order_direction'].to_s]).first : MightyGrid.order_direction
end

#current_grid_paramsObject

Get current grid parameters



31
32
33
# File 'lib/mighty_grid/parameters.rb', line 31

def current_grid_params
  params[name] || {}
end

#current_order_directionObject

Get current order direction



48
49
50
51
52
53
54
# File 'lib/mighty_grid/parameters.rb', line 48

def current_order_direction
  direction = nil
  if current_grid_params.key?('order_direction') && %w(asc desc).include?(current_grid_params['order_direction'].downcase)
    direction = current_grid_params['order_direction'].downcase
  end
  direction
end

#get_active_order_direction(parameters) ⇒ Object

Get current order direction if current order parameter coincides with the received parameter



43
44
45
# File 'lib/mighty_grid/parameters.rb', line 43

def get_active_order_direction(parameters)
  parameters[@name]['order'] == current_grid_params['order'] ? current_order_direction : nil
end

#get_current_grid_param(name) ⇒ Object

Get current grid parameter by name



26
27
28
# File 'lib/mighty_grid/parameters.rb', line 26

def get_current_grid_param(name)
  current_grid_params.key?(name) ? current_grid_params[name] : nil
end

#load_grid_paramsObject

Load grid parameters



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mighty_grid/parameters.rb', line 9

def load_grid_params
  @mg_params = {}
  @mg_params[filter_param_name.to_sym] = {}
  @mg_params.merge!(@options)
  if current_grid_params
    @mg_params.merge!(current_grid_params.symbolize_keys)
    if @mg_params[:order].present? && !@mg_params[:order].to_s.include?('.') && !@mg_params[:order].kind_of?(Hash)
      if @use_sphinx
        @mg_params[:order] = @mg_params[:order].to_s
      else
        @mg_params[:order] = "#{klass.table_name}.#{@mg_params[:order]}"
      end
    end
  end
end

#order_params(attribute, model = nil, direction = nil) ⇒ Object

Get order parameters



36
37
38
39
40
# File 'lib/mighty_grid/parameters.rb', line 36

def order_params(attribute, model = nil, direction = nil)
  order = model.present? ? "#{model.table_name}.#{attribute}" : attribute.to_s
  direction ||= order == current_grid_params['order'] ? another_order_direction : 'asc'
  { @name => { order: order, order_direction: direction } }
end