Class: Effective::Datatable
- Inherits:
-
Object
- Object
- Effective::Datatable
- Defined in:
- app/models/effective/datatable.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Any attributes set on initialize will be echoed back and available to the class.
-
#display_records ⇒ Object
Returns the value of attribute display_records.
-
#total_records ⇒ Object
Returns the value of attribute total_records.
-
#view ⇒ Object
Returns the value of attribute view.
Class Method Summary collapse
- .actions_column(options = {}, proc = nil, &block) ⇒ Object
- .all ⇒ Object
- .array_column(name, options = {}, proc = nil, &block) ⇒ Object
- .array_columns(*names) ⇒ Object
- .default_entries(entries) ⇒ Object
- .default_order(name, direction = :asc) ⇒ Object
- .find(obj, attributes = nil) ⇒ Object
-
.model_name ⇒ Object
Searching & Filters.
- .table_column(name, options = {}, proc = nil, &block) ⇒ Object
- .table_columns(*names) ⇒ Object
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_class ⇒ Object
- #default_entries ⇒ Object
- #default_order ⇒ Object
-
#display_table_columns ⇒ Object
This is for the ColReorder plugin It sends us a list of columns that are different than our table_columns order So this method just returns an array of column names, as per ColReorder.
- #empty? ⇒ Boolean
-
#finalize(collection) ⇒ Object
Override me if you like.
-
#initialize(*args) ⇒ Datatable
constructor
A new instance of Datatable.
-
#model_name ⇒ Object
Instance method.
- #order_direction ⇒ Object
- #order_name ⇒ Object
- #page ⇒ Object
- #per_page ⇒ Object
- #per_page=(length) ⇒ Object
- #present? ⇒ Boolean
-
#search_column(collection, table_column, search_term) ⇒ Object
This is here so classes that inherit from Datatables can can override the specific where clauses on a search column.
- #search_terms ⇒ Object
- #table_columns ⇒ Object
- #to_json ⇒ Object
-
#to_key ⇒ Object
Searching & Filters.
- #to_param ⇒ Object
Constructor Details
#initialize(*args) ⇒ Datatable
Returns a new instance of Datatable.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/effective/datatable.rb', line 71 def initialize(*args) if args.present? raise 'Effective::Datatable.new() can only be called with a Hash like arguments' unless args.first.kind_of?(Hash) args.first.each { |k, v| self.attributes[k] = v } end unless active_record_collection? || (collection.kind_of?(Array) && collection.first.kind_of?(Array)) raise "Unsupported collection type. Should be ActiveRecord class, ActiveRecord relation, or an Array of Arrays [[1, 'something'], [2, 'something else']]" end # Any pre-selected search terms should be assigned now search_terms.each { |column, term| self.send("#{column}=", term) } end |
Instance Attribute Details
#attributes ⇒ Object
Any attributes set on initialize will be echoed back and available to the class
86 87 88 |
# File 'app/models/effective/datatable.rb', line 86 def attributes @attributes end |
#display_records ⇒ Object
Returns the value of attribute display_records.
3 4 5 |
# File 'app/models/effective/datatable.rb', line 3 def display_records @display_records end |
#total_records ⇒ Object
Returns the value of attribute total_records.
3 4 5 |
# File 'app/models/effective/datatable.rb', line 3 def total_records @total_records end |
#view ⇒ Object
Returns the value of attribute view.
3 4 5 |
# File 'app/models/effective/datatable.rb', line 3 def view @view end |
Class Method Details
.actions_column(options = {}, proc = nil, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/effective/datatable.rb', line 37 def actions_column( = {}, proc = nil, &block) show = .fetch(:show, false) edit = .fetch(:edit, true) destroy = .fetch(:destroy, true) name = .fetch(:name, 'actions') opts = { sortable: false, filter: false, partial_local: :resource, partial_locals: { show_action: show, edit_action: edit, destroy_action: destroy } } opts[:partial] = '/effective/datatables/actions_column' unless (block_given? || proc.present?) table_column(name, opts, proc, &block) end |
.all ⇒ Object
8 9 10 |
# File 'app/models/effective/datatable.rb', line 8 def all EffectiveDatatables.datatables.map { |klass| klass.new() } end |
.array_column(name, options = {}, proc = nil, &block) ⇒ Object
33 34 35 |
# File 'app/models/effective/datatable.rb', line 33 def array_column(name, = {}, proc = nil, &block) table_column(name, .merge!({:array_column => true}), proc, &block) end |
.array_columns(*names) ⇒ Object
54 55 56 |
# File 'app/models/effective/datatable.rb', line 54 def array_columns(*names) names.each { |name| array_column(name) } end |
.default_entries(entries) ⇒ Object
62 63 64 |
# File 'app/models/effective/datatable.rb', line 62 def default_entries(entries) @default_entries = entries end |
.default_order(name, direction = :asc) ⇒ Object
58 59 60 |
# File 'app/models/effective/datatable.rb', line 58 def default_order(name, direction = :asc) @default_order = {name => direction} end |
.find(obj, attributes = nil) ⇒ Object
12 13 14 15 |
# File 'app/models/effective/datatable.rb', line 12 def find(obj, attributes = nil) obj = obj.respond_to?(:to_param) ? obj.to_param : obj EffectiveDatatables.datatables.find { |klass| klass.name.underscore.parameterize == obj }.try(:new, attributes.presence || {}) end |
.model_name ⇒ Object
Searching & Filters
66 67 68 |
# File 'app/models/effective/datatable.rb', line 66 def model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self) end |
.table_column(name, options = {}, proc = nil, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/effective/datatable.rb', line 17 def table_column(name, = {}, proc = nil, &block) if block_given? raise "You cannot use :partial => '' with the block syntax" if [:partial] raise "You cannot use :proc => ... with the block syntax" if [:proc] [:block] = block end raise "You cannot use both :partial => '' and proc => ..." if [:partial] && [:proc] send(:attr_accessor, name) (@table_columns ||= HashWithIndifferentAccess.new())[name] = end |
.table_columns(*names) ⇒ Object
29 30 31 |
# File 'app/models/effective/datatable.rb', line 29 def table_columns(*names) names.each { |name| table_column(name) } end |
Instance Method Details
#collection ⇒ Object
101 102 103 |
# File 'app/models/effective/datatable.rb', line 101 def collection raise "You must define a collection. Something like an ActiveRecord User.all or an Array of Arrays [[1, 'something'], [2, 'something else']]" end |
#collection_class ⇒ Object
105 106 107 |
# File 'app/models/effective/datatable.rb', line 105 def collection_class collection.respond_to?(:klass) ? collection.klass : self.class end |
#default_entries ⇒ Object
177 178 179 180 181 182 183 |
# File 'app/models/effective/datatable.rb', line 177 def default_entries @default_entries ||= begin entries = (self.class.instance_variable_get(:@default_entries).presence || EffectiveDatatables.default_entries) entries = -1 if entries.to_s.downcase == 'all' [10, 25, 50, 100, 250, 1000, -1].include?(entries) ? entries : 25 end end |
#default_order ⇒ Object
173 174 175 |
# File 'app/models/effective/datatable.rb', line 173 def default_order self.class.instance_variable_get(:@default_order) end |
#display_table_columns ⇒ Object
This is for the ColReorder plugin It sends us a list of columns that are different than our table_columns order So this method just returns an array of column names, as per ColReorder
123 124 125 126 127 128 129 130 131 |
# File 'app/models/effective/datatable.rb', line 123 def display_table_columns if params[:columns].present? HashWithIndifferentAccess.new().tap do |display_columns| params[:columns].each do |_, values| display_columns[values[:name]] = table_columns[values[:name]] end end end end |
#empty? ⇒ Boolean
148 149 150 |
# File 'app/models/effective/datatable.rb', line 148 def empty? total_records.to_i == 0 end |
#finalize(collection) ⇒ Object
Override me if you like
109 110 111 |
# File 'app/models/effective/datatable.rb', line 109 def finalize(collection) # Override me if you like collection end |
#model_name ⇒ Object
Instance method. In Rails 4.2 this needs to be defined on the instance, before it was on the class
93 94 95 |
# File 'app/models/effective/datatable.rb', line 93 def model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self.class) end |
#order_direction ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'app/models/effective/datatable.rb', line 163 def order_direction @order_direction ||= if params[:order].present? params[:order].first[1][:dir] == 'desc' ? 'DESC' : 'ASC' elsif default_order.present? default_order.values.first.to_s.downcase == 'desc' ? 'DESC' : 'ASC' else 'ASC' end end |
#order_name ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'app/models/effective/datatable.rb', line 152 def order_name @order_name ||= begin if params[:order] && params[:columns] order_column_index = (params[:order].first[1][:column] rescue '0') (params[:columns][order_column_index] || {})[:name] elsif default_order.present? default_order.keys.first end || table_columns.keys.first end end |
#page ⇒ Object
231 232 233 |
# File 'app/models/effective/datatable.rb', line 231 def page params[:start].to_i / per_page + 1 end |
#per_page ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/models/effective/datatable.rb', line 210 def per_page length = (params[:length].presence || default_entries).to_i if length == -1 9999999 elsif length > 0 length else 25 end end |
#per_page=(length) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'app/models/effective/datatable.rb', line 222 def per_page=(length) case length when Integer params[:length] = length when :all params[:length] = -1 end end |
#present? ⇒ Boolean
144 145 146 |
# File 'app/models/effective/datatable.rb', line 144 def present? total_records.to_i > 0 end |
#search_column(collection, table_column, search_term) ⇒ Object
This is here so classes that inherit from Datatables can can override the specific where clauses on a search column
202 203 204 205 206 207 208 |
# File 'app/models/effective/datatable.rb', line 202 def search_column(collection, table_column, search_term) if table_column[:array_column] array_tool.search_column_with_defaults(collection, table_column, search_term) else table_tool.search_column_with_defaults(collection, table_column, search_term) end end |
#search_terms ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/models/effective/datatable.rb', line 185 def search_terms @search_terms ||= HashWithIndifferentAccess.new().tap do |terms| if params[:columns].present? # This is an AJAX request from the DataTable (params[:columns] || {}).each do |_, column| next if table_columns[column[:name]].blank? || (column[:search] || {})[:value].blank? terms[column[:name]] = column[:search][:value] end else # This is the initial render, and we have to apply default search terms only table_columns.each do |name, values| terms[name] = values[:filter][:selected] if values[:filter][:selected].present? end end end end |
#table_columns ⇒ Object
114 115 116 117 118 |
# File 'app/models/effective/datatable.rb', line 114 def table_columns @table_columns ||= table_columns_with_defaults().select do |_, col| col[:if] == nil || (col[:if].respond_to?(:call) ? (view || self).instance_exec(&col[:if]) : col[:if]) end.each_with_index { |(_, col), index| col[:index] = index } end |
#to_json ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/effective/datatable.rb', line 133 def to_json raise 'Effective::Datatable to_json called with a nil view. Please call render_datatable(@datatable) or @datatable.view = view before this method' unless view.present? @json ||= { :draw => (params[:draw] || 0), :data => (table_data || []), :recordsTotal => (total_records || 0), :recordsFiltered => (display_records || 0) } end |
#to_key ⇒ Object
Searching & Filters
90 |
# File 'app/models/effective/datatable.rb', line 90 def to_key; []; end |
#to_param ⇒ Object
97 98 99 |
# File 'app/models/effective/datatable.rb', line 97 def to_param self.class.name.underscore.parameterize end |