Class: TableMe::TableMePresenter
- Inherits:
-
Object
- Object
- TableMe::TableMePresenter
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
-
#apply_search_to(model) ⇒ Object
Apply the search query to the appropriate table columns.
-
#get_data_for(model) ⇒ Object
make the model queries to pull back the data based on pagination and search results if given.
-
#initialize(model, options = {}, params = {}) ⇒ TableMePresenter
constructor
A new instance of TableMePresenter.
-
#parse_params_for(params) ⇒ Object
parse the params into an options hash that we can use.
-
#set_defaults_for(model) ⇒ Object
set defaults for options.
-
#start_item ⇒ Object
beginning item for the offset relation call.
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, = {}, 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() set_defaults_for model parse_params_for params get_data_for model @@options[self.name] = @options end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/table_me/table_me_presenter.rb', line 20 def data @data end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/table_me/table_me_presenter.rb', line 19 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/table_me/table_me_presenter.rb', line 20 def @options end |
#params ⇒ Object
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
.data ⇒ Object
23 24 25 |
# File 'lib/table_me/table_me_presenter.rb', line 23 def data @@data end |
.options ⇒ Object
27 28 29 |
# File 'lib/table_me/table_me_presenter.rb', line 27 def @@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 [:search] if [:new_search] [:page] = 1 .delete(:new_search) end column_hash = model.columns_hash || model.class.columns_hash if column_hash[[:search][:column].to_s].sql_type.include?('char') model.where(model.arel_table[[:search][:column]].matches("%#{[:search][:query]}%")) else model.where([:search][:column].to_sym => [: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([:per_page]) .offset(start_item) .order([:order]) [:total_count] = model.count [:page_total] = ([:total_count] / [: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 .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 [:page] = 1 [:per_page] ||= 10 [:name] ||= model.to_s.downcase [:order] ||= 'created_at ASC' self.name = [:name] end |
#start_item ⇒ Object
beginning item for the offset relation call
97 98 99 |
# File 'lib/table_me/table_me_presenter.rb', line 97 def start_item ([:page].to_i - 1) * [:per_page].to_i end |