Class: TrkDatatables::Base
- Inherits:
-
Object
- Object
- TrkDatatables::Base
- Extended by:
- BaseHelpers
- Defined in:
- lib/trk_datatables/base.rb
Overview
rubocop:todo Metrics/ClassLength
Direct Known Subclasses
Instance Attribute Summary collapse
-
#column_key_options ⇒ Object
Returns the value of attribute column_key_options.
Instance Method Summary collapse
- #additional_data_for_json ⇒ Object
-
#all_items ⇒ ActiveRecord::Relation
Get all items from db.
-
#all_items_count ⇒ Object
helper for github.com/trkin/trk_datatables/issues/9 which you can override to support group query.
-
#as_json(_attr = nil) ⇒ Object
_attr is given by Rails template, prefix, layout…
-
#columns ⇒ Object
Define columns of a table For simplest version you can notate column_keys as Array of strings When you need customisation of some columns, you need to define Hash of column_key => { column_options }.
- #default_order ⇒ Object
- #default_page_length ⇒ Object
-
#dt_orders_or_default_index_and_direction ⇒ Object
Returns dt_orders or default as array of index and direction datatables.net/reference/option/order.
- #dt_per_page_or_default ⇒ Object
- #filter_by_columns(_all) ⇒ Object
- #filter_by_search_all(_all) ⇒ Object
- #filtered_items ⇒ Object
- #filtered_items_count ⇒ Object
-
#global_search_columns ⇒ Object
Define columns that are not returned to page but only used as mathing for global search.
-
#index_by_column_key(column_key) ⇒ Object
We need this method publicly available since we use it for class method param_set.
-
#initialize(view) ⇒ Base
constructor
In tests you can use ‘spy(:view, default_proc: false)` when you want to initialize without exceptions when view.params is called or @params = ActiveSupport::HashWithIndifferentAccess.new params.
- #link_to_rdoc(klass, method) ⇒ Object
- #order_and_paginate_items(_filtered_items) ⇒ Object
- #ordered_paginated_filtered_items ⇒ Object
-
#param_get(column_key) ⇒ Object
Helper to populate column search from params, used in RenderHtml#thead.
-
#predefined_date_ranges ⇒ Object
rubocop:todo Metrics/AbcSize.
-
#predefined_datetime_ranges ⇒ Object
rubocop:todo Metrics/AbcSize.
- #predefined_ranges ⇒ Object
-
#preferences_field ⇒ Object
Override if you use different than :preferences You can generate with this command:.
-
#preferences_holder ⇒ Object
Override this to set model where you can store order, index, page length.
- #render_html(search_link = nil, html_options = {}) ⇒ Object
-
#rows(_page_items) ⇒ Object
Define page data.
Methods included from BaseHelpers
form_field_name, order_set, param_set, range_string
Constructor Details
#initialize(view) ⇒ Base
In tests you can use ‘spy(:view, default_proc: false)` when you want to initialize without exceptions when view.params is called or @params = ActiveSupport::HashWithIndifferentAccess.new params
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/trk_datatables/base.rb', line 17 def initialize(view) @view = view @dt_params = DtParams.new view.params @column_key_options = ColumnKeyOptions.new columns, global_search_columns, predefined_ranges @preferences = Preferences.new preferences_holder, preferences_field, self.class.name # if @dt_params.dt_columns.size != @column_key_options.size # raise Error, "dt_columns size of columns is #{@dt_params.dt_columns.size} \ # but column_key_options size is #{@column_key_options.size}" # end end |
Instance Attribute Details
#column_key_options ⇒ Object
Returns the value of attribute column_key_options.
13 14 15 |
# File 'lib/trk_datatables/base.rb', line 13 def @column_key_options end |
Instance Method Details
#additional_data_for_json ⇒ Object
178 179 180 |
# File 'lib/trk_datatables/base.rb', line 178 def additional_data_for_json {} end |
#all_items ⇒ ActiveRecord::Relation
Get all items from db
36 37 38 |
# File 'lib/trk_datatables/base.rb', line 36 def all_items raise NotImplementedError, "You should implement #{__method__} method" end |
#all_items_count ⇒ Object
helper for github.com/trkin/trk_datatables/issues/9 which you can override to support group query
170 171 172 |
# File 'lib/trk_datatables/base.rb', line 170 def all_items_count all_items.count end |
#as_json(_attr = nil) ⇒ Object
_attr is given by Rails template, prefix, layout… not used
159 160 161 162 163 164 165 166 |
# File 'lib/trk_datatables/base.rb', line 159 def as_json(_attr = nil) @dt_params.as_json( all_items_count, filtered_items_count, rows(ordered_paginated_filtered_items), additional_data_for_json ) end |
#columns ⇒ Object
Define columns of a table For simplest version you can notate column_keys as Array of strings When you need customisation of some columns, you need to define Hash of column_key => { column_options }
57 58 59 |
# File 'lib/trk_datatables/base.rb', line 57 def columns raise NotImplementedError, "You should implement #{__method__} method #{link_to_rdoc self.class, __method__}" end |
#default_order ⇒ Object
123 124 125 |
# File 'lib/trk_datatables/base.rb', line 123 def default_order [[0, :desc]].freeze end |
#default_page_length ⇒ Object
127 128 129 |
# File 'lib/trk_datatables/base.rb', line 127 def default_page_length 10 end |
#dt_orders_or_default_index_and_direction ⇒ Object
Returns dt_orders or default as array of index and direction datatables.net/reference/option/order
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/trk_datatables/base.rb', line 106 def dt_orders_or_default_index_and_direction return @dt_orders_or_default if defined? @dt_orders_or_default if columns.blank? @dt_orders_or_default = [] elsif @dt_params.dt_orders.present? @dt_orders_or_default = @dt_params.dt_orders @preferences.set :order, @dt_params.dt_orders else check_value = lambda { |r| r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) && r[0][0] < @column_key_options.size } @dt_orders_or_default = @preferences.get(:order, check_value) || default_order end @dt_orders_or_default end |
#dt_per_page_or_default ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/trk_datatables/base.rb', line 131 def dt_per_page_or_default return @dt_per_page_or_default if defined? @dt_per_page_or_default @dt_per_page_or_default = if @dt_params.dt_per_page.present? @preferences.set :per_page, @dt_params.dt_per_page @dt_params.dt_per_page else @preferences.get(:per_page) || default_page_length end end |
#filter_by_columns(_all) ⇒ Object
91 92 93 94 |
# File 'lib/trk_datatables/base.rb', line 91 def filter_by_columns(_all) raise "filter_by_columns_is_defined_in_specific_orm" \ "\n Extent from TrkDatatables::ActiveRecord instead of TrkDatatables::Base" end |
#filter_by_search_all(_all) ⇒ Object
87 88 89 |
# File 'lib/trk_datatables/base.rb', line 87 def filter_by_search_all(_all) raise "filter_by_columns_is_defined_in_specific_orm" end |
#filtered_items ⇒ Object
182 183 184 |
# File 'lib/trk_datatables/base.rb', line 182 def filtered_items filter_by_search_all filter_by_columns all_items end |
#filtered_items_count ⇒ Object
174 175 176 |
# File 'lib/trk_datatables/base.rb', line 174 def filtered_items_count filtered_items.count end |
#global_search_columns ⇒ Object
Define columns that are not returned to page but only used as mathing for global search
67 68 69 |
# File 'lib/trk_datatables/base.rb', line 67 def global_search_columns [] end |
#index_by_column_key(column_key) ⇒ Object
We need this method publicly available since we use it for class method param_set
145 146 147 |
# File 'lib/trk_datatables/base.rb', line 145 def index_by_column_key(column_key) @column_key_options.index_by_column_key column_key end |
#link_to_rdoc(klass, method) ⇒ Object
190 191 192 |
# File 'lib/trk_datatables/base.rb', line 190 def link_to_rdoc(klass, method) "http://localhost:8808/docs/TrkDatatables/#{klass.name}##{method}-instance_method" end |
#order_and_paginate_items(_filtered_items) ⇒ Object
96 97 98 |
# File 'lib/trk_datatables/base.rb', line 96 def order_and_paginate_items(_filtered_items) raise "order_and_paginate_items_is_defined_in_specific_orm" end |
#ordered_paginated_filtered_items ⇒ Object
186 187 188 |
# File 'lib/trk_datatables/base.rb', line 186 def ordered_paginated_filtered_items order_and_paginate_items filter_by_search_all filter_by_columns all_items end |
#param_get(column_key) ⇒ Object
Helper to populate column search from params, used in RenderHtml#thead
153 154 155 156 |
# File 'lib/trk_datatables/base.rb', line 153 def param_get(column_key) column_index = index_by_column_key column_key @dt_params.param_get column_index end |
#predefined_date_ranges ⇒ Object
rubocop:todo Metrics/AbcSize
228 229 230 231 232 233 234 235 236 |
# File 'lib/trk_datatables/base.rb', line 228 def predefined_date_ranges # rubocop:todo Metrics/AbcSize { Today: Time.zone.today..Time.zone.today, Yesterday: [Time.zone.today - 1.day, Time.zone.today - 1.day], "This Month": Time.zone.today.beginning_of_month...Time.zone.today, "Last Month": Time.zone.today.prev_month.beginning_of_month...Time.zone.today.prev_month.end_of_month, "This Year": Time.zone.today.beginning_of_year...Time.zone.today } end |
#predefined_datetime_ranges ⇒ Object
rubocop:todo Metrics/AbcSize
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/trk_datatables/base.rb', line 238 def predefined_datetime_ranges # rubocop:todo Metrics/AbcSize { Today: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day, Yesterday: [Time.zone.now.beginning_of_day - 1.day, Time.zone.now.end_of_day - 1.day], "This Month": Time.zone.today.beginning_of_month.beginning_of_day...Time.zone.now.end_of_day, "Last Month": Time.zone.today.prev_month.beginning_of_month.beginning_of_day...Time.zone.today.prev_month.end_of_month.end_of_day, "This Year": Time.zone.today.beginning_of_year.beginning_of_day...Time.zone.today.end_of_day } end |
#predefined_ranges ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/trk_datatables/base.rb', line 220 def predefined_ranges Time.zone ||= "UTC" { date: predefined_date_ranges, datetime: predefined_datetime_ranges } end |
#preferences_field ⇒ Object
Override if you use different than :preferences You can generate with this command:
216 217 218 |
# File 'lib/trk_datatables/base.rb', line 216 def preferences_field :preferences end |
#preferences_holder ⇒ Object
Override this to set model where you can store order, index, page length
208 209 210 |
# File 'lib/trk_datatables/base.rb', line 208 def preferences_holder nil end |
#render_html(search_link = nil, html_options = {}) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/trk_datatables/base.rb', line 194 def render_html(search_link = nil, = {}) if search_link.is_a? Hash = search_link search_link = nil end render = RenderHtml.new(search_link, self, ) render.result end |
#rows(_page_items) ⇒ Object
Define page data
83 84 85 |
# File 'lib/trk_datatables/base.rb', line 83 def rows(_page_items) raise NotImplementedError, "You should implement #{__method__} method" end |