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
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.
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
@@data = {} @@options = {}.
-
#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
@@data = {} @@options = {}
36 37 38 39 40 41 42 |
# File 'lib/table_me/table_me_presenter.rb', line 36 def initialize model, = {}, params = {} @options = ActiveSupport::HashWithIndifferentAccess.new() set_defaults_for model parse_params_for params get_data_for model 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 |
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 [: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
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([: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
45 46 47 |
# File 'lib/table_me/table_me_presenter.rb', line 45 def parse_params_for params .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 [: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
92 93 94 |
# File 'lib/table_me/table_me_presenter.rb', line 92 def start_item ([:page].to_i - 1) * [:per_page].to_i end |