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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TableMePresenter.



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

def initialize model, options = {}, params = {}
  # this was more of a patch for the code. I need to go back through and normalize all
  # the hash access so a normal has can be used with confidence
  # TODO normalize hash access to strings or symbols
  @options = ActiveSupport::HashWithIndifferentAccess.new(options)

  set_defaults_for model
  parse_params_for params
  get_data_for model
  @@options[self.name] = @options
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

Class Method Details

.dataObject



23
24
25
# File 'lib/table_me/table_me_presenter.rb', line 23

def data
  @@data
end

.optionsObject



27
28
29
# File 'lib/table_me/table_me_presenter.rb', line 27

def options
  @@options
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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/table_me/table_me_presenter.rb', line 77

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/table_me/table_me_presenter.rb', line 62

def get_data_for model
  model = apply_search_to(model)

  @@data[self.name] = @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



48
49
50
# File 'lib/table_me/table_me_presenter.rb', line 48

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

#set_defaults_for(model) ⇒ Object

set defaults for options



53
54
55
56
57
58
59
# File 'lib/table_me/table_me_presenter.rb', line 53

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



97
98
99
# File 'lib/table_me/table_me_presenter.rb', line 97

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