Class: Wice::WiceGrid
Instance Attribute Summary collapse
-
#ar_options ⇒ Object
readonly
Returns the value of attribute ar_options.
-
#csv_file_name ⇒ Object
readonly
Returns the value of attribute csv_file_name.
-
#csv_tempfile ⇒ Object
Returns the value of attribute csv_tempfile.
-
#custom_order ⇒ Object
readonly
Returns the value of attribute custom_order.
-
#export_to_csv_enabled ⇒ Object
readonly
Returns the value of attribute export_to_csv_enabled.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
-
#query_store_model ⇒ Object
readonly
Returns the value of attribute query_store_model.
-
#renderer ⇒ Object
writeonly
Sets the attribute renderer.
-
#resultset ⇒ Object
readonly
:nodoc:.
-
#saved_query ⇒ Object
readonly
Returns the value of attribute saved_query.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#view_helper_finished ⇒ Object
Returns the value of attribute view_helper_finished.
Instance Method Summary collapse
-
#all_pages_records ⇒ Object
Returns the list of objects browsable through all pages with the current filters.
-
#all_record_mode? ⇒ Boolean
:nodoc:.
-
#count ⇒ Object
(also: #size)
:nodoc:.
-
#current_page_records ⇒ Object
Returns the list of objects displayed on current page.
-
#declare_column(column_name, model_class, custom_filter_active, table_alias) ⇒ Object
:nodoc:.
-
#distinct_values_for_column(column) ⇒ Object
with this variant we get even those values which do not appear in the resultset.
-
#distinct_values_for_column_in_resultset(messages) ⇒ Object
:nodoc:.
-
#dump_status ⇒ Object
:nodoc:.
-
#each ⇒ Object
:nodoc:.
-
#empty? ⇒ Boolean
:nodoc:.
-
#filter_params(view_column) ⇒ Object
Getters.
-
#filtered_by ⇒ Object
:nodoc:.
-
#filtered_by?(view_column) ⇒ Boolean
:nodoc:.
-
#filtering_on? ⇒ Boolean
:nodoc:.
-
#form_ar_options(opts = {}) ⇒ Object
:nodoc:.
-
#get_state_as_parameter_value_pairs(including_saved_query_request = false) ⇒ Object
:nodoc:.
-
#initialize(klass_or_relation, controller, opts = {}) ⇒ WiceGrid
constructor
core workflow methods START.
-
#order_direction ⇒ Object
:nodoc:.
-
#ordered_by ⇒ Object
:nodoc:.
-
#ordered_by?(column) ⇒ Boolean
:nodoc:.
-
#output_csv? ⇒ Boolean
:nodoc:.
-
#output_html? ⇒ Boolean
:nodoc:.
-
#process_loading_query ⇒ Object
:nodoc:.
-
#process_params ⇒ Object
:nodoc:.
-
#read ⇒ Object
:nodoc:.
-
#selected_records ⇒ Object
:nodoc:.
-
#with_paginated_resultset(&callback) ⇒ Object
A block executed from within the plugin to process records of the current page.
-
#with_resultset(&callback) ⇒ Object
A block executed from within the plugin to process all records browsable through all pages with the current filters.
Constructor Details
#initialize(klass_or_relation, controller, opts = {}) ⇒ WiceGrid
core workflow methods START
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 |
# File 'lib/wice_grid.rb', line 70 def initialize(klass_or_relation, controller, opts = {}) #:nodoc: @controller = controller @relation = klass_or_relation @klass = klass_or_relation.is_a?(ActiveRecord::Relation) ? klass_or_relation.klass : klass_or_relation unless @klass.kind_of?(Class) && @klass.ancestors.index(ActiveRecord::Base) raise WiceGridArgumentError.new("ActiveRecord model class (second argument) must be a Class derived from ActiveRecord::Base") end # validate :with_resultset & :with_paginated_resultset [:with_resultset, :with_paginated_resultset].each do |callback_symbol| unless [NilClass, Symbol, Proc].index(opts[callback_symbol].class) raise WiceGridArgumentError.new(":#{callback_symbol} must be either a Proc or Symbol object") end end opts[:order_direction].downcase! if opts[:order_direction].kind_of?(String) # validate :order_direction if opts[:order_direction] && ! (opts[:order_direction] == 'asc' || opts[:order_direction] == :asc || opts[:order_direction] == 'desc' || opts[:order_direction] == :desc) raise WiceGridArgumentError.new(":order_direction must be either 'asc' or 'desc'.") end # options that are understood @options = { :conditions => nil, :csv_file_name => nil, :custom_order => {}, :enable_export_to_csv => Defaults::ENABLE_EXPORT_TO_CSV, :group => nil, :include => nil, :joins => nil, :name => Defaults::GRID_NAME, :order => nil, :order_direction => Defaults::ORDER_DIRECTION, :page => 1, :per_page => Defaults::PER_PAGE, :saved_query => nil, :select => nil, :total_entries => nil, :with_paginated_resultset => nil, :with_resultset => nil } # validate parameters opts.assert_valid_keys(@options.keys) @options.merge!(opts) @export_to_csv_enabled = @options[:enable_export_to_csv] @csv_file_name = @options[:csv_file_name] case @name = @options[:name] when String when Symbol @name = @name.to_s else raise WiceGridArgumentError.new("name of the grid should be a string or a symbol") end raise WiceGridArgumentError.new("name of the grid can only contain alphanumeruc characters") unless @name =~ /^[a-zA-Z\d_]*$/ @table_column_matrix = TableColumnMatrix.new @table_column_matrix.default_model_class = @klass @ar_options = {} @status = HashWithIndifferentAccess.new if @options[:order] @options[:order] = @options[:order].to_s @options[:order_direction] = @options[:order_direction].to_s @status[:order_direction] = @options[:order_direction] @status[:order] = @options[:order] end @status[:total_entries] = @options[:total_entries] @status[:per_page] = @options[:per_page] @status[:page] = @options[:page] @status[:conditions] = @options[:conditions] @status[:f] = @options[:f] process_loading_query process_params @ar_options_formed = false end |
Instance Attribute Details
#ar_options ⇒ Object (readonly)
Returns the value of attribute ar_options.
64 65 66 |
# File 'lib/wice_grid.rb', line 64 def @ar_options end |
#csv_file_name ⇒ Object (readonly)
Returns the value of attribute csv_file_name.
64 65 66 |
# File 'lib/wice_grid.rb', line 64 def csv_file_name @csv_file_name end |
#csv_tempfile ⇒ Object
Returns the value of attribute csv_tempfile.
66 67 68 |
# File 'lib/wice_grid.rb', line 66 def csv_tempfile @csv_tempfile end |
#custom_order ⇒ Object (readonly)
Returns the value of attribute custom_order.
63 64 65 |
# File 'lib/wice_grid.rb', line 63 def custom_order @custom_order end |
#export_to_csv_enabled ⇒ Object (readonly)
Returns the value of attribute export_to_csv_enabled.
64 65 66 |
# File 'lib/wice_grid.rb', line 64 def export_to_csv_enabled @export_to_csv_enabled end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
63 64 65 |
# File 'lib/wice_grid.rb', line 63 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
63 64 65 |
# File 'lib/wice_grid.rb', line 63 def name @name end |
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
66 67 68 |
# File 'lib/wice_grid.rb', line 66 def output_buffer @output_buffer end |
#query_store_model ⇒ Object (readonly)
Returns the value of attribute query_store_model.
63 64 65 |
# File 'lib/wice_grid.rb', line 63 def query_store_model @query_store_model end |
#renderer=(value) ⇒ Object (writeonly)
Sets the attribute renderer
65 66 67 |
# File 'lib/wice_grid.rb', line 65 def renderer=(value) @renderer = value end |
#resultset ⇒ Object (readonly)
:nodoc:
313 314 315 |
# File 'lib/wice_grid.rb', line 313 def resultset @resultset end |
#saved_query ⇒ Object (readonly)
Returns the value of attribute saved_query.
64 65 66 |
# File 'lib/wice_grid.rb', line 64 def saved_query @saved_query end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
64 65 66 |
# File 'lib/wice_grid.rb', line 64 def status @status end |
#view_helper_finished ⇒ Object
Returns the value of attribute view_helper_finished.
66 67 68 |
# File 'lib/wice_grid.rb', line 66 def view_helper_finished @view_helper_finished end |
Instance Method Details
#all_pages_records ⇒ Object
Returns the list of objects browsable through all pages with the current filters. Should only be called after the grid
helper.
446 447 448 449 |
# File 'lib/wice_grid.rb', line 446 def all_pages_records raise WiceGridException.new("all_pages_records can only be called only after the grid view helper") unless self.view_helper_finished resultset_without_paging_with_user_filters end |
#all_record_mode? ⇒ Boolean
:nodoc:
428 429 430 |
# File 'lib/wice_grid.rb', line 428 def all_record_mode? #:nodoc: @status[:pp] end |
#count ⇒ Object Also known as: size
:nodoc:
383 384 385 386 |
# File 'lib/wice_grid.rb', line 383 def count #:nodoc: (:skip_ordering => true, :forget_generated_options => true) @relation.count(:conditions => @ar_options[:conditions], :joins => @ar_options[:joins], :include => @ar_options[:include], :group => @ar_options[:group]) end |
#current_page_records ⇒ Object
Returns the list of objects displayed on current page. Should only be called after the grid
helper.
452 453 454 455 |
# File 'lib/wice_grid.rb', line 452 def current_page_records raise WiceGridException.new("current_page_records can only be called only after the grid view helper") unless self.view_helper_finished @resultset end |
#declare_column(column_name, model_class, custom_filter_active, table_alias) ⇒ Object
:nodoc:
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/wice_grid.rb', line 210 def declare_column(column_name, model_class, custom_filter_active, table_alias) #:nodoc: if model_class # this is an included table column = @table_column_matrix.get_column_by_model_class_and_column_name(model_class, column_name) raise WiceGridArgumentError.new("Column '#{column_name}' is not found in table '#{model_class.table_name}'!") if column.nil? main_table = false table_name = model_class.table_name else column = @table_column_matrix.get_column_in_default_model_class_by_column_name(column_name) if column.nil? raise WiceGridArgumentError.new("Column '#{column_name}' is not found in table '#{@klass.table_name}'! " + "If '#{column_name}' belongs to another table you should declare it in :include or :join when initialising " + "the grid, and specify :model_class in column declaration.") end main_table = true table_name = @table_column_matrix.default_model_class.table_name end if column conditions, current_parameter_name = column.wg_initialize_request_parameters(@status[:f], main_table, table_alias, custom_filter_active) if @status[:f] && conditions.blank? @status[:f].delete(current_parameter_name) end @table_column_matrix.add_condition(column, conditions) [column, table_name , main_table] else nil end end |
#distinct_values_for_column(column) ⇒ Object
with this variant we get even those values which do not appear in the resultset
395 396 397 398 399 |
# File 'lib/wice_grid.rb', line 395 def distinct_values_for_column(column) #:nodoc: res = column.model_klass.find(:all, :select => "distinct #{column.name}", :order => "#{column.name} asc").collect{|ar| ar[column.name] }.reject{|e| e.blank?}.map{|i|[i,i]} end |
#distinct_values_for_column_in_resultset(messages) ⇒ Object
:nodoc:
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/wice_grid.rb', line 402 def distinct_values_for_column_in_resultset() #:nodoc: uniq_vals = Set.new resultset_without_paging_without_user_filters.each do |ar| v = ar.deep_send(*) uniq_vals << v unless v.nil? end return uniq_vals.to_a.map{|i| if i.is_a?(Array) && i.size == 2 i elsif i.is_a?(Hash) && i.size == 1 i.to_a.flatten else [i,i] end }.sort{|a,b| a[0]<=>b[0]} end |
#dump_status ⇒ Object
:nodoc:
432 433 434 435 436 |
# File 'lib/wice_grid.rb', line 432 def dump_status #:nodoc: " params: #{params[name].inspect}\n" + " status: #{@status.inspect}\n" + " ar_options #{@ar_options.inspect}\n" end |
#each ⇒ Object
:nodoc:
318 319 320 321 322 323 |
# File 'lib/wice_grid.rb', line 318 def each #:nodoc: self.read unless @resultset # database querying is late! @resultset.each do |r| yield r end end |
#empty? ⇒ Boolean
:nodoc:
390 391 392 |
# File 'lib/wice_grid.rb', line 390 def empty? #:nodoc: self.count == 0 end |
#filter_params(view_column) ⇒ Object
Getters
304 305 306 307 308 309 310 311 |
# File 'lib/wice_grid.rb', line 304 def filter_params(view_column) #:nodoc: column_name = view_column.attribute_name_fully_qualified_for_all_but_main_table_columns if @status[:f] && @status[:f][column_name] @status[:f][column_name] else {} end end |
#filtered_by ⇒ Object
:nodoc:
347 348 349 |
# File 'lib/wice_grid.rb', line 347 def filtered_by #:nodoc: @status[:f].nil? ? [] : @status[:f].keys end |
#filtered_by?(view_column) ⇒ Boolean
:nodoc:
351 352 353 |
# File 'lib/wice_grid.rb', line 351 def filtered_by?(view_column) #:nodoc: @status[:f].nil? ? false : @status[:f].has_key?(view_column.attribute_name_fully_qualified_for_all_but_main_table_columns) end |
#filtering_on? ⇒ Boolean
:nodoc:
343 344 345 |
# File 'lib/wice_grid.rb', line 343 def filtering_on? #:nodoc: not @status[:f].blank? end |
#form_ar_options(opts = {}) ⇒ Object
:nodoc:
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/wice_grid.rb', line 240 def (opts = {}) #:nodoc: return if @ar_options_formed @ar_options_formed = true unless opts[:forget_generated_options] # validate @status[:order_direction] @status[:order_direction] = case @status[:order_direction] when /desc/i 'desc' when /asc/i 'asc' else '' end # conditions if @table_column_matrix.generated_conditions.size == 0 @status.delete(:f) end @ar_options[:conditions] = klass.send(:merge_conditions, @status[:conditions], * @table_column_matrix.conditions ) # conditions processed if (! opts[:skip_ordering]) && @status[:order] @ar_options[:order] = add_custom_order_sql(complete_column_name(@status[:order])) @ar_options[:order] += ' ' + @status[:order_direction] end if self.output_html? @ar_options[:per_page] = if all_record_mode? # reset the :pp value in all records mode @status[:pp] = count_resultset_without_paging_without_user_filters else @status[:per_page] end @ar_options[:page] = @status[:page] @ar_options[:total_entries] = @status[:total_entries] if @status[:total_entries] end @ar_options[:joins] = @options[:joins] @ar_options[:include] = @options[:include] @ar_options[:group] = @options[:group] @ar_options[:select] = @options[:select] end |
#get_state_as_parameter_value_pairs(including_saved_query_request = false) ⇒ Object
:nodoc:
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/wice_grid.rb', line 355 def get_state_as_parameter_value_pairs(including_saved_query_request = false) #:nodoc: res = [] unless status[:f].blank? status[:f].parameter_names_and_values([name, 'f']).collect do |param_name, value| if value.is_a?(Array) param_name_ar = param_name + '[]' value.each do |v| res << [param_name_ar, v] end else res << [param_name, value] end end end if including_saved_query_request && @saved_query res << ["#{name}[q]", @saved_query.id ] end [:order, :order_direction].select{|parameter| status[parameter] }.collect do |parameter| res << ["#{name}[#{parameter}]", status[parameter] ] end res end |
#order_direction ⇒ Object
:nodoc:
339 340 341 |
# File 'lib/wice_grid.rb', line 339 def order_direction #:nodoc: @status[:order_direction] end |
#ordered_by ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/wice_grid.rb', line 334 def ordered_by #:nodoc: @status[:order] end |
#ordered_by?(column) ⇒ Boolean
:nodoc:
325 326 327 328 329 330 331 332 |
# File 'lib/wice_grid.rb', line 325 def ordered_by?(column) #:nodoc: return nil if @status[:order].blank? if column.main_table && ! offs = @status[:order].index('.') @status[:order] == column.attribute_name else @status[:order] == column.table_alias_or_table_name + '.' + column.attribute_name end end |
#output_csv? ⇒ Boolean
:nodoc:
420 421 422 |
# File 'lib/wice_grid.rb', line 420 def output_csv? #:nodoc: @status[:export] == 'csv' end |
#output_html? ⇒ Boolean
:nodoc:
424 425 426 |
# File 'lib/wice_grid.rb', line 424 def output_html? #:nodoc: @status[:export].blank? end |
#process_loading_query ⇒ Object
:nodoc:
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 |
# File 'lib/wice_grid.rb', line 176 def process_loading_query #:nodoc: @saved_query = nil if params[name] && params[name][:q] @saved_query = load_query(params[name][:q]) params[name].delete(:q) elsif @options[:saved_query] if @options[:saved_query].is_a? ActiveRecord::Base @saved_query = @options[:saved_query] else @saved_query = load_query(@options[:saved_query]) end else return end unless @saved_query.nil? params[name] = HashWithIndifferentAccess.new if params[name].blank? [:f, :order, :order_direction].each do |key| if @saved_query.query[key].blank? params[name].delete(key) else params[name][key] = @saved_query.query[key] end end end end |
#process_params ⇒ Object
:nodoc:
203 204 205 206 207 208 |
# File 'lib/wice_grid.rb', line 203 def process_params #:nodoc: if this_grid_params @status.merge!(this_grid_params) @status.delete(:export) unless self.export_to_csv_enabled end end |
#read ⇒ Object
:nodoc:
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/wice_grid.rb', line 287 def read #:nodoc: @klass.unscoped do @resultset = if self.output_csv? @relation.find(:all, @ar_options) else @relation.paginate(@ar_options) end end invoke_resultset_callbacks end |
#selected_records ⇒ Object
:nodoc:
439 440 441 442 |
# File 'lib/wice_grid.rb', line 439 def selected_records #:nodoc: STDERR.puts "WiceGrid: Parameter :#{selected_records} is deprecated, use :#{all_pages_records} or :#{current_page_records} instead!" all_pages_records end |
#with_paginated_resultset(&callback) ⇒ Object
A block executed from within the plugin to process records of the current page. The argument to the callback is the array of the records. See the README for more details.
165 166 167 |
# File 'lib/wice_grid.rb', line 165 def with_paginated_resultset(&callback) @options[:with_paginated_resultset] = callback end |
#with_resultset(&callback) ⇒ Object
A block executed from within the plugin to process all records browsable through all pages with the current filters. The argument to the callback is a lambda object which returns the list of records when called. See the README for the explanation.
172 173 174 |
# File 'lib/wice_grid.rb', line 172 def with_resultset(&callback) @options[:with_resultset] = callback end |