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
- .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.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/effective/datatable.rb', line 55 def initialize(*args) 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 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 # 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
70 71 72 |
# File 'app/models/effective/datatable.rb', line 70 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
.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
37 38 39 |
# File 'app/models/effective/datatable.rb', line 37 def array_columns(*names) names.each { |name| array_column(name) } end |
.default_entries(entries) ⇒ Object
45 46 47 |
# File 'app/models/effective/datatable.rb', line 45 def default_entries(entries) @default_entries = entries end |
.default_order(name, direction = :asc) ⇒ Object
41 42 43 |
# File 'app/models/effective/datatable.rb', line 41 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
49 50 51 |
# File 'app/models/effective/datatable.rb', line 49 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
85 86 87 |
# File 'app/models/effective/datatable.rb', line 85 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
89 90 91 |
# File 'app/models/effective/datatable.rb', line 89 def collection_class collection.respond_to?(:klass) ? collection.klass : self.class end |
#default_entries ⇒ Object
161 162 163 164 165 166 167 |
# File 'app/models/effective/datatable.rb', line 161 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
157 158 159 |
# File 'app/models/effective/datatable.rb', line 157 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
107 108 109 110 111 112 113 114 115 |
# File 'app/models/effective/datatable.rb', line 107 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
132 133 134 |
# File 'app/models/effective/datatable.rb', line 132 def empty? total_records.to_i == 0 end |
#finalize(collection) ⇒ Object
Override me if you like
93 94 95 |
# File 'app/models/effective/datatable.rb', line 93 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
77 78 79 |
# File 'app/models/effective/datatable.rb', line 77 def model_name # Searching & Filters @model_name ||= ActiveModel::Name.new(self.class) end |
#order_direction ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'app/models/effective/datatable.rb', line 147 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
136 137 138 139 140 141 142 143 144 145 |
# File 'app/models/effective/datatable.rb', line 136 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
215 216 217 |
# File 'app/models/effective/datatable.rb', line 215 def page params[:start].to_i / per_page + 1 end |
#per_page ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/models/effective/datatable.rb', line 194 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
206 207 208 209 210 211 212 213 |
# File 'app/models/effective/datatable.rb', line 206 def per_page=(length) case length when Integer params[:length] = length when :all params[:length] = -1 end end |
#present? ⇒ Boolean
128 129 130 |
# File 'app/models/effective/datatable.rb', line 128 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
186 187 188 189 190 191 192 |
# File 'app/models/effective/datatable.rb', line 186 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'app/models/effective/datatable.rb', line 169 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
98 99 100 101 102 |
# File 'app/models/effective/datatable.rb', line 98 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
117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/effective/datatable.rb', line 117 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
74 |
# File 'app/models/effective/datatable.rb', line 74 def to_key; []; end |
#to_param ⇒ Object
81 82 83 |
# File 'app/models/effective/datatable.rb', line 81 def to_param self.class.name.underscore.parameterize end |