Class: Tabulatr::Data
- Inherits:
-
Object
- Object
- Tabulatr::Data
- Defined in:
- lib/tabulatr/data/data.rb
Overview
– Copyright © 2010-2014 Peter Horn & Florian Thomas, metaminded UG
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Defined Under Namespace
Modules: DSL, Filtering, Formatting, Pagination, Sorting Classes: ButtonBuilder, Invoker
Instance Method Summary collapse
- #batch_params(params) ⇒ Object
- #check_params(params) ⇒ Object
- #data_for_table(params, locals: {}, controller: nil, &block) ⇒ Object
- #execute_batch_actions(batch_param, selected_ids) ⇒ Object
-
#filter_params(params) ⇒ Object
– Params ++.
- #filters ⇒ Object
-
#initialize(relation) ⇒ Data
constructor
A new instance of Data.
- #join_required_tables(params) ⇒ Object
- #search? ⇒ Boolean
- #search_param(params) ⇒ Object
- #sort_params(params) ⇒ Object
- #table_columns ⇒ Object
- #table_name_for_association(assoc) ⇒ Object
Constructor Details
#initialize(relation) ⇒ Data
Returns a new instance of Data.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tabulatr/data/data.rb', line 26 def initialize(relation) @relation = relation @base = relation.respond_to?(:klass) ? relation.klass : relation @table_name = @base.table_name @search = self.class.instance_variable_get('@search') || HashWithIndifferentAccess.new @includes = Set.new() @cname = @base.name.downcase @batch_actions = nil @row = self.class.instance_variable_get('@row') table_columns.map do |col| next if col.is_a? Tabulatr::Renderer::Checkbox col.klass = @base.reflect_on_association(col.table_name).try(:klass) || @base col.determine_appropriate_filter! if col..filter === true end end |
Instance Method Details
#batch_params(params) ⇒ Object
113 |
# File 'lib/tabulatr/data/data.rb', line 113 def batch_params(params) params["#{Tabulatr::Utility.formatted_name(@base.name)}_batch"] end |
#check_params(params) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/tabulatr/data/data.rb', line 114 def check_params(params) tabulatr_checked = params["tabulatr_checked"] if tabulatr_checked.present? tabulatr_checked['checked_ids'].split(',') end end |
#data_for_table(params, locals: {}, controller: nil, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tabulatr/data/data.rb', line 42 def data_for_table(params, locals: {}, controller: nil, &block) @batch_actions = block if block_given? # count total = @relation.count # prepare the query apply_filters(filter_params params) apply_search(search_param params) apply_sorting(sort_params params) join_required_tables(params) execute_batch_actions(batch_params(params), check_params(params)) pagination = compute_pagination(params[:page], params[:pagesize]) apply_pagination(pagination.slice(:offset, :pagesize)) # get the records found = apply_formats(locals: locals, controller: controller) append = params[:append].present? ? Tabulatr::Utility.string_to_boolean(params[:append]) : false # prepare result for rendering found.define_singleton_method(:__pagination) do { :page => pagination[:page], :pagesize => pagination[:pagesize], :count => pagination[:count], :pages => pagination[:pages], :total => total, :append => append, :table_id => params[:table_id] } end found.define_singleton_method(:to_tabulatr_json) do |klass=nil| Tabulatr::JsonBuilder.build found, klass, params[:arguments] end found end |
#execute_batch_actions(batch_param, selected_ids) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/tabulatr/data/data.rb', line 86 def execute_batch_actions batch_param, selected_ids if batch_param.present? && @batch_actions.present? batch_param = batch_param.keys.first.to_sym if batch_param.is_a?(Hash) selected_ids = @relation.map(&:id) if selected_ids.empty? @batch_actions.yield(Invoker.new(batch_param, selected_ids)) end end |
#filter_params(params) ⇒ Object
– Params ++
110 |
# File 'lib/tabulatr/data/data.rb', line 110 def filter_params(params) params["#{Tabulatr::Utility.formatted_name(@base.name)}_filter"] end |
#filters ⇒ Object
98 99 100 |
# File 'lib/tabulatr/data/data.rb', line 98 def filters self.class.instance_variable_get('@filters') end |
#join_required_tables(params) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tabulatr/data/data.rb', line 121 def join_required_tables(params) if params[:arguments].present? tt = (params[:arguments].split(",").select{|s| s[':']}.map do |s| s.split(':').first end.uniq.map(&:to_sym)) tt.delete(@table_name.to_sym) @includes = @includes + tt end # @relation = @relation.includes(@includes.map(&:to_sym)).references(@includes.map(&:to_sym)) @relation = @relation.eager_load(@includes.map(&:to_sym)) # @relation = @relation.group("#{@table_name}.#{@base.primary_key}") end |
#search? ⇒ Boolean
102 103 104 |
# File 'lib/tabulatr/data/data.rb', line 102 def search? self.class.instance_variable_get('@search') end |
#search_param(params) ⇒ Object
111 |
# File 'lib/tabulatr/data/data.rb', line 111 def search_param(params) params["#{Tabulatr::Utility.formatted_name(@base.name)}_search"] end |
#sort_params(params) ⇒ Object
112 |
# File 'lib/tabulatr/data/data.rb', line 112 def sort_params(params) params["#{Tabulatr::Utility.formatted_name(@base.name)}_sort"] end |
#table_columns ⇒ Object
94 95 96 |
# File 'lib/tabulatr/data/data.rb', line 94 def table_columns self.class.instance_variable_get("@table_columns") end |
#table_name_for_association(assoc) ⇒ Object
134 135 136 137 |
# File 'lib/tabulatr/data/data.rb', line 134 def table_name_for_association(assoc) @base.reflect_on_association(assoc.to_sym).table_name # assoc.to_sym end |