Class: GridTable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_table/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



4
5
6
# File 'lib/grid_table/table.rb', line 4

def initialize
  @controls = []
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



2
3
4
# File 'lib/grid_table/table.rb', line 2

def records
  @records
end

#total_rowsObject (readonly)

Returns the value of attribute total_rows.



2
3
4
# File 'lib/grid_table/table.rb', line 2

def total_rows
  @total_rows
end

Instance Method Details

#add_control(model, attribute, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/grid_table/table.rb', line 8

def add_control(model, attribute, options)
  @controls << GridTable::Control.new(
    model: model.name.underscore.to_sym,
    attribute: attribute,
    source: options[:source],
    source_class: options[:source_class],
    source_column: options[:source_column],
    source_sql: options[:source_sql],
    filter: options[:filter],
    polymorphic: options[:polymorphic]
  )
end

#populate!(resource, params, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grid_table/table.rb', line 21

def populate!(resource, params, options)
  # In Rails 5 ActionController::Parameters returns an object rather than a hash
  # It provides the to_h method in order to return a hash (with indifferent access) of safe parameters
  # Rails 4 and below returns a regular hash so we need to account for that
  @params   = params.to_h.with_indifferent_access
  @records  = resource
  aggregate = options[:aggregate] || false

  select(aggregate: aggregate)
  filter! unless params[:skip_filtering]
  @total_rows = @records.length
  sort! unless params[:skip_sorting]
  page! unless params[:skip_paging]

  @records
end

#strong_paramsObject



38
39
40
41
42
# File 'lib/grid_table/table.rb', line 38

def strong_params
  @controls.inject(common_strong_params) do |all_params, control|
    all_params << control.url_param
  end
end