Class: Wice::WiceGrid
Overview
Main class responsible for keeping the state of the grid, building an ActiveRelation, and running queries
Instance Attribute Summary collapse
-
#ar_options ⇒ Object
readonly
:nodoc:.
-
#csv_encoding ⇒ Object
readonly
:nodoc:.
-
#csv_field_separator ⇒ Object
readonly
:nodoc:.
-
#csv_file_name ⇒ Object
readonly
:nodoc:.
-
#csv_tempfile ⇒ Object
:nodoc:.
-
#custom_order ⇒ Object
readonly
:nodoc:.
-
#export_to_csv_enabled ⇒ Object
readonly
:nodoc:.
-
#klass ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
-
#output_buffer ⇒ Object
:nodoc:.
-
#query_store_model ⇒ Object
readonly
:nodoc:.
-
#renderer ⇒ Object
writeonly
:nodoc:.
-
#resultset ⇒ Object
readonly
:nodoc:.
-
#saved_query ⇒ Object
readonly
:nodoc:.
-
#status ⇒ Object
readonly
:nodoc:.
-
#view_helper_finished ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_references(relation) ⇒ Object
:nodoc:.
-
#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: nil, model: nil, custom_filter_active: nil, table_alias: nil, filter_type: nil, assocs: []) ⇒ Object
declare_column(String, ActiveRecord, CustomFilterSpec, nil | string, nil | Boolean).
-
#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:.
-
#do_count ⇒ 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
TO DO: what to do with other @ar_options values?.
-
#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
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 |
# File 'lib/wice_grid_ms.rb', line 78 def initialize(klass_or_relation, controller, opts = {}) #:nodoc: @controller = controller @relation = klass_or_relation @klass = if @relation.is_a?(Class) && @relation.ancestors.index(ActiveRecord::Base) klass_or_relation else klass_or_relation.klass end unless @klass.is_a?(Class) && @klass.ancestors.index(ActiveRecord::Base) fail 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) fail WiceGridArgumentError.new(":#{callback_symbol} must be either a Proc or Symbol object") end end opts[:order_direction].downcase! if opts[:order_direction].is_a?(String) # validate :order_direction if opts[:order_direction] && ! (opts[:order_direction] == 'asc' || opts[:order_direction] == :asc || opts[:order_direction] == 'desc' || opts[:order_direction] == :desc) fail WiceGridArgumentError.new(":order_direction must be either 'asc' or 'desc'.") end begin # options that are understood = { conditions: nil, csv_file_name: nil, csv_field_separator: ConfigurationProvider.value_for(:CSV_FIELD_SEPARATOR), csv_encoding: ConfigurationProvider.value_for(:CSV_ENCODING), custom_order: {}, enable_export_to_csv: ConfigurationProvider.value_for(:ENABLE_EXPORT_TO_CSV), group: nil, include: nil, joins: nil, name: ConfigurationProvider.value_for(:GRID_NAME), order: nil, order_direction: ConfigurationProvider.value_for(:ORDER_DIRECTION), page: 1, page_method_name: ConfigurationProvider.value_for(:PAGE_METHOD_NAME), per_page: ConfigurationProvider.value_for(:PER_PAGE), saved_query: nil, with_paginated_resultset: nil, with_resultset: nil, use_default_scope: ConfigurationProvider.value_for(:USE_DEFAULT_SCOPE) } rescue NameError raise NameError.new('A constant is missing in wice_grid_config.rb: ' + $ERROR_INFO. + '. This can happen when you upgrade the WiceGrid to a newer version with a new configuration constant. ' \ 'Add the constant manually or re-run `bundle exec rails g wice_grid:install`.') end # validate parameters opts.assert_valid_keys(.keys) .merge!(opts) @export_to_csv_enabled = [:enable_export_to_csv] @csv_file_name = [:csv_file_name] @csv_field_separator = [:csv_field_separator] @csv_encoding = [:csv_encoding] case @name = [:name] when String when Symbol @name = @name.to_s else fail WiceGridArgumentError.new('name of the grid should be a string or a symbol') end fail 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 = {} @status = HashWithIndifferentAccess.new if [:order] [:order] = [:order].to_s [:order_direction] = [:order_direction].to_s @status[:order_direction] = [:order_direction] @status[:order] = [:order] end @status[:per_page] = [:per_page] @status[:page] = [:page] @status[:conditions] = [:conditions] @status[:f] = [:f] process_loading_query process_params = false end |
Instance Attribute Details
#csv_encoding ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def csv_encoding @csv_encoding end |
#csv_field_separator ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def csv_field_separator @csv_field_separator end |
#csv_file_name ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def csv_file_name @csv_file_name end |
#csv_tempfile ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/wice_grid_ms.rb', line 74 def csv_tempfile @csv_tempfile end |
#custom_order ⇒ Object (readonly)
:nodoc:
71 72 73 |
# File 'lib/wice_grid_ms.rb', line 71 def custom_order @custom_order end |
#export_to_csv_enabled ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def export_to_csv_enabled @export_to_csv_enabled end |
#klass ⇒ Object (readonly)
:nodoc:
71 72 73 |
# File 'lib/wice_grid_ms.rb', line 71 def klass @klass end |
#output_buffer ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/wice_grid_ms.rb', line 74 def output_buffer @output_buffer end |
#query_store_model ⇒ Object (readonly)
:nodoc:
71 72 73 |
# File 'lib/wice_grid_ms.rb', line 71 def query_store_model @query_store_model end |
#renderer=(value) ⇒ Object (writeonly)
:nodoc:
73 74 75 |
# File 'lib/wice_grid_ms.rb', line 73 def renderer=(value) @renderer = value end |
#resultset ⇒ Object (readonly)
:nodoc:
71 72 73 |
# File 'lib/wice_grid_ms.rb', line 71 def resultset @resultset end |
#saved_query ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def saved_query @saved_query end |
#status ⇒ Object (readonly)
:nodoc:
72 73 74 |
# File 'lib/wice_grid_ms.rb', line 72 def status @status end |
#view_helper_finished ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/wice_grid_ms.rb', line 74 def view_helper_finished @view_helper_finished end |
Instance Method Details
#add_references(relation) ⇒ Object
:nodoc:
322 323 324 325 326 327 328 |
# File 'lib/wice_grid_ms.rb', line 322 def add_references(relation) #:nodoc: if [:include] && relation.respond_to?(:references) # refs = [@ar_options[:include]] unless @ar_options[:include].is_a?(Array) relation = relation.references(* [:include]) end relation end |
#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.
510 511 512 513 |
# File 'lib/wice_grid_ms.rb', line 510 def all_pages_records fail 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:
499 500 501 |
# File 'lib/wice_grid_ms.rb', line 499 def all_record_mode? #:nodoc: @status[:pp] end |
#count ⇒ Object Also known as: size
:nodoc:
445 446 447 448 |
# File 'lib/wice_grid_ms.rb', line 445 def count #:nodoc: (skip_ordering: true, forget_generated_options: true) do_count end |
#current_page_records ⇒ Object
Returns the list of objects displayed on current page. Should only be called after the grid helper.
516 517 518 519 |
# File 'lib/wice_grid_ms.rb', line 516 def current_page_records fail 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: nil, model: nil, custom_filter_active: nil, table_alias: nil, filter_type: nil, assocs: []) ⇒ Object
declare_column(String, ActiveRecord, CustomFilterSpec, nil | string, nil | Boolean)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 |
# File 'lib/wice_grid_ms.rb', line 226 def declare_column( column_name: nil, model: nil, custom_filter_active: nil, table_alias: nil, filter_type: nil, assocs: []) #:nodoc: [:include] = Wice.build_includes([:include], assocs) if model # this is an included table column = @table_column_matrix.get_column_by_model_class_and_column_name(model, column_name) fail WiceGridArgumentError.new("Column '#{column_name}' is not found in table '#{model.table_name}'!") if column.nil? main_table = false table_name = model.table_name else column = @table_column_matrix.get_column_in_default_model_class_by_column_name(column_name) if column.nil? fail 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 in column declaration.') end main_table = true table_name = @table_column_matrix.default_model_class.table_name end if column conditions_generator = ActiveRecordColumnWrapper.new(column, @status[:f], main_table, table_alias, custom_filter_active, filter_type) conditions, current_parameter_name = conditions_generator.wg_initialize_request_parameters if @status[:f] && conditions.blank? @status[:f].delete(current_parameter_name) end @table_column_matrix.add_condition(column, conditions) # [ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::Column, String, Boolean] [column, table_name, main_table] end end |
#distinct_values_for_column(column) ⇒ Object
with this variant we get even those values which do not appear in the resultset
467 468 469 470 471 |
# File 'lib/wice_grid_ms.rb', line 467 def distinct_values_for_column(column) #:nodoc: column.model.select("distinct #{column.name}").order("#{column.name} asc").collect do|ar| ar[column.name] end.reject(&:blank?).map { |i| [i, i] } end |
#distinct_values_for_column_in_resultset(messages) ⇒ Object
:nodoc:
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/wice_grid_ms.rb', line 473 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 uniq_vals.to_a.map do|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 end.sort { |a, b| a[0] <=> b[0] } end |
#do_count ⇒ Object
:nodoc:
450 451 452 453 454 455 456 457 458 |
# File 'lib/wice_grid_ms.rb', line 450 def do_count #:nodoc: @relation .all .merge([:conditions]).count( joins: [:joins], include: [:include], group: [:group] ) end |
#dump_status ⇒ Object
:nodoc:
503 504 505 506 |
# File 'lib/wice_grid_ms.rb', line 503 def dump_status #:nodoc: " params: #{params[name].inspect}\n" + " status: #{@status.inspect}\n" \ " ar_options #{@ar_options.inspect}\n" end |
#each ⇒ Object
:nodoc:
381 382 383 384 385 386 |
# File 'lib/wice_grid_ms.rb', line 381 def each #:nodoc: self.read unless @resultset # database querying is late! @resultset.each do |r| yield r end end |
#empty? ⇒ Boolean
:nodoc:
462 463 464 |
# File 'lib/wice_grid_ms.rb', line 462 def empty? #:nodoc: self.count == 0 end |
#filter_params(view_column) ⇒ Object
Getters
367 368 369 370 371 372 373 374 |
# File 'lib/wice_grid_ms.rb', line 367 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:
409 410 411 |
# File 'lib/wice_grid_ms.rb', line 409 def filtered_by #:nodoc: @status[:f].nil? ? [] : @status[:f].keys end |
#filtered_by?(view_column) ⇒ Boolean
:nodoc:
413 414 415 |
# File 'lib/wice_grid_ms.rb', line 413 def filtered_by?(view_column) #:nodoc: @status[:f].nil? ? false : @status[:f].key?(view_column.attribute_name_fully_qualified_for_all_but_main_table_columns) end |
#filtering_on? ⇒ Boolean
:nodoc:
405 406 407 |
# File 'lib/wice_grid_ms.rb', line 405 def filtering_on? #:nodoc: !@status[:f].blank? end |
#form_ar_options(opts = {}) ⇒ Object
:nodoc:
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/wice_grid_ms.rb', line 268 def (opts = {}) #:nodoc: return if = 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 # do not delete for a while # https://github.com/leikind/wice_grid/issues/144 # if @table_column_matrix.generated_conditions.size == 0 # @status.delete(:f) # end initial_conditions_active_relation = @klass.where(@status[:conditions]) [:conditions] = @table_column_matrix.conditions.reduce(initial_conditions_active_relation) do |active_relation_accu, cond| conditions_active_relation = @klass.where(cond) active_relation_accu.merge(conditions_active_relation) end # conditions processed if (!opts[:skip_ordering]) && ! @status[:order].blank? [:order] = add_custom_order_sql(complete_column_name(@status[:order])) [:order] += ' ' + @status[:order_direction] end [:joins] = [:joins] [:include] = [:include] [:group] = [:group] if self.output_html? [:per_page] = @status[:per_page] [:page] = @status[:page] if (show_all_limit = Wice::ConfigurationProvider.value_for(:SHOW_ALL_ALLOWED_UP_TO, strict: false)) && all_record_mode? if do_count > show_all_limit # force-reset SHOW-ALL to pagination @status[:pp] = nil end end end end |
#get_state_as_parameter_value_pairs(including_saved_query_request = false) ⇒ Object
:nodoc:
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/wice_grid_ms.rb', line 417 def get_state_as_parameter_value_pairs(including_saved_query_request = false) #:nodoc: res = [] unless status[:f].blank? Wice::WgHash.parameter_names_and_values(status[:f], [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 do|parameter| status[parameter] end.collect do |parameter| res << ["#{name}[#{parameter}]", status[parameter]] end res end |
#order_direction ⇒ Object
:nodoc:
401 402 403 |
# File 'lib/wice_grid_ms.rb', line 401 def order_direction #:nodoc: @status[:order_direction] end |
#ordered_by ⇒ Object
:nodoc:
397 398 399 |
# File 'lib/wice_grid_ms.rb', line 397 def ordered_by #:nodoc: @status[:order] end |
#ordered_by?(column) ⇒ Boolean
:nodoc:
388 389 390 391 392 393 394 395 |
# File 'lib/wice_grid_ms.rb', line 388 def ordered_by?(column) #:nodoc: return nil if @status[:order].blank? if column.main_table && ! @status[:order].index('.') @status[:order] == column.attribute else @status[:order] == column.table_alias_or_table_name + '.' + column.attribute end end |
#output_csv? ⇒ Boolean
:nodoc:
491 492 493 |
# File 'lib/wice_grid_ms.rb', line 491 def output_csv? #:nodoc: @status[:export] == 'csv' end |
#output_html? ⇒ Boolean
:nodoc:
495 496 497 |
# File 'lib/wice_grid_ms.rb', line 495 def output_html? #:nodoc: @status[:export].blank? end |
#process_loading_query ⇒ Object
:nodoc:
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/wice_grid_ms.rb', line 191 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 [:saved_query] if [:saved_query].is_a? ActiveRecord::Base @saved_query = [:saved_query] else @saved_query = load_query([: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:
218 219 220 221 222 223 |
# File 'lib/wice_grid_ms.rb', line 218 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
TO DO: what to do with other @ar_options values?
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/wice_grid_ms.rb', line 331 def read #:nodoc: use_default_or_unscoped do @resultset = if self.output_csv? || all_record_mode? relation = @relation .includes([:include]) .joins([:joins]) .order([:order]) .group([:group]) .merge([:conditions]) relation = add_references relation relation else # p @ar_options relation = @relation .send([:page_method_name], [:page]) .per([:per_page]) .includes([:include]) .joins([:joins]) .order([:order]) .group([:group]) .merge([:conditions]) relation = add_references relation relation end end invoke_resultset_callbacks 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.
180 181 182 |
# File 'lib/wice_grid_ms.rb', line 180 def with_paginated_resultset(&callback) [: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.
187 188 189 |
# File 'lib/wice_grid_ms.rb', line 187 def with_resultset(&callback) [:with_resultset] = callback end |