Module: Tabulatr::Finder
- Defined in:
- lib/tabulatr/tabulatr/finder.rb,
lib/tabulatr/tabulatr/finder/find_for_table.rb
Overview
These are extensions for use from ActionController instances In a seperate class call only for clearity
Defined Under Namespace
Classes: Invoker
Class Method Summary collapse
-
.compress_id_list(list) ⇒ Object
compress the list of ids as good as I could imagine ;) uses fancy base twisting.
-
.find_for_table(klaz, params, options = {}, &block) ⇒ Object
——————————————————————- Called if SomeActveRecordSubclass::find_for_table(params) is called.
-
.uncompress_id_list(str) ⇒ Object
inverse of compress_id_list.
Class Method Details
.compress_id_list(list) ⇒ Object
compress the list of ids as good as I could imagine ;) uses fancy base twisting
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tabulatr/tabulatr/finder.rb', line 31 def self.compress_id_list(list) if list.length == 0 "" elsif list.first.is_a?(Fixnum) IdStuffer.stuff(list) else "GzB" + Base64.encode64( Zlib::Deflate.deflate( list.join(Tabulatr.[:checked_separator]))) end end |
.find_for_table(klaz, params, options = {}, &block) ⇒ Object
Called if SomeActveRecordSubclass::find_for_table(params) is called
32 33 34 35 36 37 38 39 40 41 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/tabulatr/tabulatr/finder/find_for_table.rb', line 32 def self.find_for_table(klaz, params, ={}, &block) adapter = if klaz.respond_to?(:descends_from_active_record?) then ::Tabulatr::Adapter::ActiveRecordAdapter.new(klaz) elsif klaz.include?(Mongoid::Document) then ::Tabulatr::Adapter::MongoidAdapter.new(klaz) else raise("Don't know how to deal with class '#{klaz}'") end = Tabulatr. opts = Tabulatr..merge() params ||= {} # just to be sure cname = adapter.class_to_param pagination_name = "#{cname}#{[:pagination_postfix]}" sort_name = "#{cname}#{[:sort_postfix]}" filter_name = "#{cname}#{[:filter_postfix]}" batch_name = "#{cname}#{[:batch_postfix]}" check_name = "#{cname}#{[:checked_postfix]}" # before we do anything else, we find whether there's something to do for batch actions checked_param = ActiveSupport::HashWithIndifferentAccess.new({:checked_ids => '', :current_page => []}). merge(params[check_name] || {}) id = adapter.primary_key id_type = adapter.key_type # checkboxes checked_ids = uncompress_id_list(checked_param[:checked_ids]) new_ids = checked_param[:current_page] new_ids.map!(&:to_i) if id_type==:integer selected_ids = checked_ids + new_ids batch_param = params[batch_name] if batch_param.present? and block_given? batch_param = batch_param.keys.first.to_sym if batch_param.is_a?(Hash) yield(Invoker.new(batch_param, selected_ids)) end # then, we obey any "select" buttons if pushed if checked_param[:select_all] selected_ids = adapter.selected_ids(opts).to_a.map { |r| r.send(id) } elsif checked_param[:select_none] selected_ids = [] elsif checked_param[:select_visible] visible_ids = uncompress_id_list(checked_param[:visible]) selected_ids = (selected_ids + visible_ids).sort.uniq elsif checked_param[:unselect_visible] visible_ids = uncompress_id_list(checked_param[:visible]) selected_ids = (selected_ids - visible_ids).sort.uniq end # at this point, we've retrieved the filter settings, the sorting setting, the pagination settings and # the selected_ids. filter_param = (params[filter_name] || {}) sortparam = params[sort_name] pops = params[pagination_name] || {} # store the state if appropriate if opts[:stateful] session = [:stateful] sname = "#{cname}#{[:state_session_postfix]}" raise "give the session as the :stateful parameter in find_for_table, not a '#{session.class}'" \ unless session.respond_to? :[] session[sname] ||= {} if params["#{cname}#{[:reset_state_postfix]}"] # clicked reset button, reset all and clear session selected_ids = [] filter_param = {} sortparam = nil pops = {} session.delete sname elsif !pops.present? && !selected_ids.present? && !sortparam.present? && !filter_param.present? # we're supposed to retrieve the state from the session if applicable state = session[sname] selected_ids = state[:selected_ids] || [] filter_param = state[:filter_param] || {} sortparam = state[:sortparam] pops = state[:paging_param] || {} else # store the current settings into the session to be stateful ;) session[sname][:selected_ids] = selected_ids session[sname][:filter_param] = filter_param session[sname][:sortparam] = sortparam session[sname][:paging_param] = pops end end # firstly, get the conditions from the filters includes = [] maps = opts[:name_mapping] || {} conditions = filter_param.each do |t| n, v = t next unless v.present? # FIXME n = name_escaping(n) if (n != [:associations_filter]) table_name = adapter.table_name nn = if maps[n] then maps[n] else t = "#{table_name}.#{n}" raise "SECURITY violation, field name is '#{t}'" unless /^[\d\w]+(\.[\d\w]+)?$/.match t t end # puts ">>>>>1>> #{n} -> #{nn}" adapter.add_conditions_from(nn, v) else v.each do |t| n,v = t assoc, att = n.split(".").map(&:to_sym) includes << assoc table_name = adapter.table_name_for_association(assoc) nn = if maps[n] then maps[n] else t = "#{table_name}.#{att}" raise "SECURITY violation, field name is '#{t}'" unless /^[\d\w]+(\.[\d\w]+)?$/.match t t end # puts ">>>>>2>> #{n} -> #{nn}" adapter.add_conditions_from(nn, v) end end end # more button handling if checked_param[:select_filtered] all = adapter.all selected_ids = (selected_ids + all.map { |r| i=r.send(id); i.is_a?(Fixnum) ? i : i.to_s }).sort.uniq elsif checked_param[:unselect_filtered] all = adapter.dup.all selected_ids = (selected_ids - all.map { |r| i=r.send(id); i.is_a?(Fixnum) ? i : i.to_s }).sort.uniq end # secondly, find the order_by stuff order = adapter.order_for_query(sortparam, opts[:default_order]) # thirdly, get the pagination data = Tabulatr..merge(opts).merge(pops) pagesize = (pops[:pagesize] || opts[:default_pagesize] || [:pagesize]).to_f page = [:page].to_i page += 1 if [:page_right] page -= 1 if [:page_left] c = adapter.includes(includes).count # Group statments return a hash c = c.count unless c.class == Fixnum pages = (c/pagesize).ceil page = [1, [page, pages].min].max total = adapter.preconditions_scope(opts).count # here too total = total.count unless total.class == Fixnum # Now, actually find the stuff found = adapter.limit(pagesize.to_i).offset(((page-1)*pagesize).to_i).order(order).to_a # finally, inject methods to retrieve the current 'settings' found.define_singleton_method(:__filters) { filter_param } found.define_singleton_method(:__classinfo) { [klaz, cname, id, id_type] } found.define_singleton_method(:__pagination) do { :page => page, :pagesize => pagesize, :count => c, :pages => pages, :pagesizes => [:pagesizes], :total => total } end found.define_singleton_method(:__sorting) { adapter.order(sortparam, opts[:default_order]) } visible_ids = (found.map { |r| r.send(id) }) checked_ids = compress_id_list(selected_ids - visible_ids) visible_ids = compress_id_list(visible_ids) found.define_singleton_method(:__checked) do { :selected => selected_ids, :checked_ids => checked_ids, :visible => visible_ids } end found.define_singleton_method(:__stateful) { (opts[:stateful] ? true : false) } found.define_singleton_method(:__store_data) { opts[:store_data] || {} } found end |
.uncompress_id_list(str) ⇒ Object
inverse of compress_id_list
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tabulatr/tabulatr/finder.rb', line 44 def self.uncompress_id_list(str) if !str.present? [] elsif str.starts_with?("GzB") Zlib::Inflate.inflate(Base64.decode64(str[3..-1])).split( Tabulatr.[:checked_separator]) else IdStuffer.unstuff(str) end end |