Class: MightyGrid::Base

Inherits:
AbstractController::Base
  • Object
show all
Defined in:
lib/mighty_grid/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, opts = {}) ⇒ Base

:nodoc:


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mighty_grid/base.rb', line 6

def initialize(params, opts = {})  #:nodoc:
  @controller_params = params

  # Get active controller through params
  if controller = "#{params[:controller].camelize}Controller".safe_constantize
    @controller = ObjectSpace.each_object(controller).first
  end

  @filters = {}

  @options = {
    page:       1,
    per_page:   MightyGrid.per_page,
    name:       MightyGrid.grid_name,
    include:    nil,
    joins:      nil,
    conditions: nil,
    group:      nil,
    order:      nil
  }

  opts.assert_valid_keys(@options.keys)
  @options.merge!(opts)

  @klass = self.class.klass
  @relation = self.class.relation

  @name = @options[:name].to_s

  load_grid_params
end

Class Attribute Details

.klassObject (readonly)

Returns the value of attribute klass


39
40
41
# File 'lib/mighty_grid/base.rb', line 39

def klass
  @klass
end

.relationObject (readonly)

Returns the value of attribute relation


39
40
41
# File 'lib/mighty_grid/base.rb', line 39

def relation
  @relation
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def controller
  @controller
end

#filtersObject

Returns the value of attribute filters


4
5
6
# File 'lib/mighty_grid/base.rb', line 4

def filters
  @filters
end

#klassObject (readonly)

Returns the value of attribute klass


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def klass
  @klass
end

#mg_paramsObject (readonly)

Returns the value of attribute mg_params


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def mg_params
  @mg_params
end

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def options
  @options
end

#output_bufferObject

Returns the value of attribute output_buffer


4
5
6
# File 'lib/mighty_grid/base.rb', line 4

def output_buffer
  @output_buffer
end

#paramsObject (readonly)

Get controller parameters


103
104
105
# File 'lib/mighty_grid/base.rb', line 103

def params
  @params
end

#relationObject (readonly)

Returns the value of attribute relation


3
4
5
# File 'lib/mighty_grid/base.rb', line 3

def relation
  @relation
end

Class Method Details

.filter(name) ⇒ Object


49
50
51
# File 'lib/mighty_grid/base.rb', line 49

def filter(name)

end

.scope(&block) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/mighty_grid/base.rb', line 41

def scope(&block)
  if block_given?
    klass_or_relation = yield
    @relation = klass_or_relation
    @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation
  end
end

Instance Method Details

#add_filter_param(param, value) ⇒ Object

Add param in filters


136
137
138
# File 'lib/mighty_grid/base.rb', line 136

def add_filter_param(param, value)
  @mg_params[filter_param_name.to_sym][param] = value unless @mg_params[filter_param_name.to_sym].key?(param)
end

#another_order_directionObject

Get another order direction


173
174
175
# File 'lib/mighty_grid/base.rb', line 173

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

#apply_filtersObject

Apply filters


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
# File 'lib/mighty_grid/base.rb', line 72

def apply_filters
  filter_params.each do |filter_name, filter_value|
    name, table_name = filter_name.split('.').reverse

    if table_name && Object.const_defined?(table_name.classify)
      model = table_name.classify.constantize
    else
      model = klass
    end

    next if filter_value.blank? || !model.column_names.include?(name)

    if model && model.superclass == ActiveRecord::Base
      field_type = model.columns_hash[name].type
    else
      next
    end

    table_name = model.table_name
    if @filters.key?(filter_name.to_sym) && @filters[filter_name.to_sym].is_a?(Array)
      @relation = @relation.where(table_name => { filter_name => filter_value })
    elsif field_type == :boolean
      value = %w(true 1 t).include?(filter_value) ? true : false
      @relation = @relation.where(table_name => { filter_name => value })
    elsif [:string, :text].include?(field_type)
      @relation = @relation.where("#{table_name}.#{name} #{like_operator} ?", "%#{filter_value}%")
    end
  end
end

#current_grid_paramsObject

Get current grid parameters


147
148
149
# File 'lib/mighty_grid/base.rb', line 147

def current_grid_params
  params[name] || {}
end

#current_order_directionObject

Get current order direction


164
165
166
167
168
169
170
# File 'lib/mighty_grid/base.rb', line 164

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

#filter_param_nameObject

Get filter parameter name


131
132
133
# File 'lib/mighty_grid/base.rb', line 131

def filter_param_name
  'f'
end

#filter_paramsObject

Get filter parameters


126
127
128
# File 'lib/mighty_grid/base.rb', line 126

def filter_params
  @mg_params[filter_param_name.to_sym] || {}
end

#get_active_order_direction(parameters) ⇒ Object

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


159
160
161
# File 'lib/mighty_grid/base.rb', line 159

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


121
122
123
# File 'lib/mighty_grid/base.rb', line 121

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

#get_filter_name(field, model = nil) ⇒ Object

Get filter name by field


141
142
143
144
# File 'lib/mighty_grid/base.rb', line 141

def get_filter_name(field, model = nil)
  field_name = model.present? ? "#{model.table_name}.#{field}" : field
  "#{name}[#{filter_param_name}][#{field_name}]"
end

#like_operatorObject

Get like or ilike operator depending on the database adapter


178
179
180
181
182
183
184
# File 'lib/mighty_grid/base.rb', line 178

def like_operator
  if ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
    'ILIKE'
  else
    'LIKE'
  end
end

#load_grid_paramsObject

Load grid parameters


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mighty_grid/base.rb', line 108

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] = "#{klass.table_name}.#{@mg_params[:order]}"
    end
  end
end

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

Get order parameters


152
153
154
155
156
# File 'lib/mighty_grid/base.rb', line 152

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

#readObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mighty_grid/base.rb', line 54

def read
  apply_filters
  if @mg_params[:order].present? && current_order_direction.present?
    @relation = @relation.order("#{@mg_params[:order]} #{current_order_direction.to_sym}")
  else
    @relation = @relation.order(@mg_params[:order])
  end

  @relation = @relation
                .page(@mg_params[:page])
                .per(@mg_params[:per_page])
                .includes(@options[:include])
                .joins(@options[:joins])
                .where(@options[:conditions])
                .group(@options[:group])
end