Class: TrkDatatables::DtParams
- Inherits:
-
Object
- Object
- TrkDatatables::DtParams
- Defined in:
- lib/trk_datatables/dt_params.rb
Overview
This class wraps databases format datatables.net/manual/server-side#Returned-data
In future we can use some another datatables like github.com/gregnb/mui-datatables github.com/handsontable/handsontable github.com/cloudflarearchive/backgrid (archived)
Class Method Summary collapse
- .form_field_name(column_index) ⇒ Object
- .order_set(column_index, direction) ⇒ Object
- .param_set(column_index, value) ⇒ Object
- .sample_params(options = {}) ⇒ Object
- .sample_view_params(options = {}) ⇒ Object
Instance Method Summary collapse
- #as_json(all_count, filtered_count, data, additional = {}) ⇒ Object
- #dt_columns ⇒ Object
- #dt_offset ⇒ Object
- #dt_orders ⇒ Object
- #dt_per_page ⇒ Object
-
#initialize(params) ⇒ DtParams
constructor
A new instance of DtParams.
- #param_get(column_index) ⇒ Object
- #search_all ⇒ Object
Constructor Details
#initialize(params) ⇒ DtParams
Returns a new instance of DtParams.
11 12 13 14 |
# File 'lib/trk_datatables/dt_params.rb', line 11 def initialize(params) params.permit! if params.respond_to? :permit! @params = ActiveSupport::HashWithIndifferentAccess.new params end |
Class Method Details
.form_field_name(column_index) ⇒ Object
117 118 119 |
# File 'lib/trk_datatables/dt_params.rb', line 117 def self.form_field_name(column_index) "columns[#{column_index}][search][value]" end |
.order_set(column_index, direction) ⇒ Object
113 114 115 |
# File 'lib/trk_datatables/dt_params.rb', line 113 def self.order_set(column_index, direction) {order: {"0": {column: column_index, dir: direction}}} end |
.param_set(column_index, value) ⇒ Object
109 110 111 |
# File 'lib/trk_datatables/dt_params.rb', line 109 def self.param_set(column_index, value) {columns: {column_index.to_s => {search: {value: value}}}} end |
.sample_params(options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/trk_datatables/dt_params.rb', line 134 def self.sample_params( = {}) HashWithIndifferentAccess.new( draw: "1", start: "0", length: "10", search: { value: "", regex: "false" }, order: { "0": {column: "0", dir: "desc"} }, # [:columns] should have the same size as column_key_options since we # ignore keys, and use positions columns: { "0": { searchable: "true", orderable: "true", search: { value: "", regex: "false" } }, "1": { searchable: "true", orderable: "true", search: { value: "", regex: "false" } }, "2": { searchable: "true", orderable: "false", search: { value: "", regex: "false" } } } ).merge end |
.sample_view_params(options = {}) ⇒ Object
128 129 130 131 132 |
# File 'lib/trk_datatables/dt_params.rb', line 128 def self.sample_view_params( = {}) OpenStruct.new( params: sample_params() ) end |
Instance Method Details
#as_json(all_count, filtered_count, data, additional = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/trk_datatables/dt_params.rb', line 95 def as_json(all_count, filtered_count, data, additional = {}) additional = {} if additional.nil? raise Error, "additional_data_for_json needs to be a hash" unless additional.is_a? Hash draw = @params[:draw].to_i { draw: draw, recordsTotal: all_count, recordsFiltered: filtered_count, **additional, data: data } end |
#dt_columns ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/trk_datatables/dt_params.rb', line 63 def dt_columns return @dt_columns if defined? @dt_columns @dt_columns = [] return @dt_columns unless @params[:columns].respond_to? :each @params[:columns].each.map do |(dt_position, dt_column)| @dt_columns[dt_position.to_i] = { index: dt_position.to_i, searchable: dt_column[:searchable].to_s != "false", # if nil as it is in set_params, than use true orderable: dt_column[:orderable].to_s != "false", # if nil as it is in set_params, than use true search_value: (dt_column[:search] && dt_column[:search][:value]) || "" } end @dt_columns.each_with_index do |dt_column, i| next unless dt_column.nil? @dt_columns[i] = { index: i, searchable: true, orderable: true, search_value: "" } end end |
#dt_offset ⇒ Object
16 17 18 |
# File 'lib/trk_datatables/dt_params.rb', line 16 def dt_offset @params[:start].to_i end |
#dt_orders ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/trk_datatables/dt_params.rb', line 37 def dt_orders return @dt_orders if defined? @dt_orders @dt_orders = [] return @dt_orders if @params[:order].blank? @dt_orders = @params[:order].each_with_object([]) do |(_index, dt_order), a| # for order we ignore key (_index) since order is preserved a << [ dt_order[:column].to_i, dt_order[:dir]&.to_s&.casecmp("ASC")&.zero? ? :asc : :desc ] end @dt_orders end |
#dt_per_page ⇒ Object
20 21 22 23 24 |
# File 'lib/trk_datatables/dt_params.rb', line 20 def dt_per_page return if @params[:length].blank? @params[:length].to_i end |
#param_get(column_index) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/trk_datatables/dt_params.rb', line 121 def param_get(column_index) @params.dig :columns, column_index.to_s, :search, :value rescue TypeError => e raise Error, "#{e.}. Column search is in a format: { \"columns\": { \"0\": { \"search\": { \"value\": { \"ABC\" } } } } }" end |
#search_all ⇒ Object
89 90 91 92 93 |
# File 'lib/trk_datatables/dt_params.rb', line 89 def search_all @params.dig(:search, :value) || "" rescue TypeError => e raise Error, e. + '. Global search is in a format: { "search": { "value": "ABC" } }' end |