Class: TableMe::TableMePresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/table_me/table_me_presenter.rb

Overview

name - Label for the the table per_page - The amount of items per page of the table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}, params = {}) ⇒ TableMePresenter

@@data = {} @@options = {}



36
37
38
39
40
41
42
# File 'lib/table_me/table_me_presenter.rb', line 36

def initialize model, options = {}, params = {}
  @options = ActiveSupport::HashWithIndifferentAccess.new(options)

  set_defaults_for model
  parse_params_for params
  get_data_for model
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/table_me/table_me_presenter.rb', line 20

def data
  @data
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/table_me/table_me_presenter.rb', line 19

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/table_me/table_me_presenter.rb', line 20

def options
  @options
end

#paramsObject

Returns the value of attribute params.



19
20
21
# File 'lib/table_me/table_me_presenter.rb', line 19

def params
  @params
end

Instance Method Details

#apply_search_to(model) ⇒ Object

Apply the search query to the appropriate table columns. This is sort of ugly at the moment and not as reliable as it could be. It needs to be refactored to account for different column types and use appropriate search methods. Ex. LIKE doesn’t work for integers TODO refactor this to be more reliable for all column types



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/table_me/table_me_presenter.rb', line 72

def apply_search_to model
  if options[:search]
    if options[:new_search]
      options[:page] = 1
      options.delete(:new_search)
    end

    column_hash = model.columns_hash || model.class.columns_hash

    if column_hash[options[:search][:column].to_s].sql_type.include?('char')
      model.where(model.arel_table[options[:search][:column]].matches("%#{options[:search][:query]}%"))
    else
      model.where(options[:search][:column].to_sym => options[:search][:query])
    end
  else
    model
  end
end

#get_data_for(model) ⇒ Object

make the model queries to pull back the data based on pagination and search results if given



59
60
61
62
63
64
65
66
# File 'lib/table_me/table_me_presenter.rb', line 59

def get_data_for model
  model = apply_search_to(model)

  @data = model.limit(options[:per_page]).offset(start_item).order(options[:order])

  options[:total_count] = model.count
  options[:page_total] = (options[:total_count] / options[:per_page].to_f).ceil
end

#parse_params_for(params) ⇒ Object

parse the params into an options hash that we can use



45
46
47
# File 'lib/table_me/table_me_presenter.rb', line 45

def parse_params_for params
  options.merge! URLParser.parse_params_for(params, self.name)
end

#set_defaults_for(model) ⇒ Object

set defaults for options



50
51
52
53
54
55
56
# File 'lib/table_me/table_me_presenter.rb', line 50

def set_defaults_for model
  options[:page] = 1
  options[:per_page] ||= 10
  options[:name] ||= model.to_s.downcase
  options[:order] ||= 'created_at ASC'
  self.name = options[:name]
end

#start_itemObject

beginning item for the offset relation call



92
93
94
# File 'lib/table_me/table_me_presenter.rb', line 92

def start_item
  (options[:page].to_i - 1) * options[:per_page].to_i
end