class GridTable::Table
Attributes
records[R]
total_rows[R]
Public Class Methods
new()
click to toggle source
# File lib/grid_table/table.rb, line 4 def initialize @controls = [] end
Public Instance Methods
add_control(model, attribute, options)
click to toggle source
# 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)
click to toggle source
# 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_params()
click to toggle source
# 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
Private Instance Methods
common_strong_params()
click to toggle source
# File lib/grid_table/table.rb, line 46 def common_strong_params %w[page page_size sort sort_order] end
filter!()
click to toggle source
# File lib/grid_table/table.rb, line 58 def filter! filter_params = @params.reject { |k| common_strong_params.include?(k) } filter_params.each do |attribute, attribute_value| control = GridTable::Control.find_by_param(attribute, @controls) @records = control.filter(attribute_value, @records) if control.present? end end
page()
click to toggle source
# File lib/grid_table/table.rb, line 50 def page [@params[:page].to_i, 0].max end
page!()
click to toggle source
# File lib/grid_table/table.rb, line 79 def page! @records = @records.offset(page * page_size).limit(page_size) end
page_size()
click to toggle source
# File lib/grid_table/table.rb, line 54 def page_size (@params[:page_size] || 10).to_i end
select(aggregate: false)
click to toggle source
# File lib/grid_table/table.rb, line 66 def select(aggregate: false) select_fields = @controls.map(&:select) select_fields.push('*') unless aggregate @records = @records.select(select_fields.join(',')) end
sort!()
click to toggle source
# File lib/grid_table/table.rb, line 73 def sort! control = GridTable::Control.find_by_param(@params[:sort], @controls) @records = control.sort(@params[:sort_order], records) if control.present? end