Class: Wice::WiceGrid

Inherits:
Object show all
Defined in:
lib/wice_grid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, controller, opts = {}) ⇒ WiceGrid

core workflow methods START


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
# File 'lib/wice_grid.rb', line 54

def initialize(klass, controller, opts = {})  #:nodoc:
  @controller = controller

  unless klass.kind_of?(Class) && klass.ancestors.index(Mongoid::Document)
    raise WiceGridArgumentError.new("The model class (second argument) must be a Class derived from Mongoid::Document")
  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,
    :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_]*$/

  @klass = klass
  @criteria = Mongoid::Criteria.new(@klass)
  @has_any_filter_criteria = false
  @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
  @criteria_formed = false
end

Instance Attribute Details

#ar_optionsObject (readonly)

Returns the value of attribute ar_options.


48
49
50
# File 'lib/wice_grid.rb', line 48

def ar_options
  @ar_options
end

#controllerObject (readonly)

Returns the value of attribute controller.


47
48
49
# File 'lib/wice_grid.rb', line 47

def controller
  @controller
end

#csv_file_nameObject (readonly)

Returns the value of attribute csv_file_name.


48
49
50
# File 'lib/wice_grid.rb', line 48

def csv_file_name
  @csv_file_name
end

#csv_tempfileObject

Returns the value of attribute csv_tempfile.


50
51
52
# File 'lib/wice_grid.rb', line 50

def csv_tempfile
  @csv_tempfile
end

#custom_orderObject (readonly)

Returns the value of attribute custom_order.


47
48
49
# File 'lib/wice_grid.rb', line 47

def custom_order
  @custom_order
end

#export_to_csv_enabledObject (readonly)

Returns the value of attribute export_to_csv_enabled.


48
49
50
# File 'lib/wice_grid.rb', line 48

def export_to_csv_enabled
  @export_to_csv_enabled
end

#extra_filterObject (readonly)

Returns the value of attribute extra_filter.


48
49
50
# File 'lib/wice_grid.rb', line 48

def extra_filter
  @extra_filter
end

#klassObject (readonly)

Returns the value of attribute klass.


47
48
49
# File 'lib/wice_grid.rb', line 47

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.


47
48
49
# File 'lib/wice_grid.rb', line 47

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.


47
48
49
# File 'lib/wice_grid.rb', line 47

def options
  @options
end

#output_bufferObject

Returns the value of attribute output_buffer.


50
51
52
# File 'lib/wice_grid.rb', line 50

def output_buffer
  @output_buffer
end

#query_store_modelObject (readonly)

Returns the value of attribute query_store_model.


47
48
49
# File 'lib/wice_grid.rb', line 47

def query_store_model
  @query_store_model
end

#renderer=(value) ⇒ Object (writeonly)

Sets the attribute renderer

Parameters:

  • value

    the value to set the attribute renderer to.


49
50
51
# File 'lib/wice_grid.rb', line 49

def renderer=(value)
  @renderer = value
end

#resultsetObject (readonly)

:nodoc:


270
271
272
# File 'lib/wice_grid.rb', line 270

def resultset
  @resultset
end

#saved_queryObject (readonly)

Returns the value of attribute saved_query.


48
49
50
# File 'lib/wice_grid.rb', line 48

def saved_query
  @saved_query
end

#statusObject (readonly)

Returns the value of attribute status.


48
49
50
# File 'lib/wice_grid.rb', line 48

def status
  @status
end

#view_helper_finishedObject

Returns the value of attribute view_helper_finished.


50
51
52
# File 'lib/wice_grid.rb', line 50

def view_helper_finished
  @view_helper_finished
end

Instance Method Details

#add_criteria(extra) ⇒ Object


137
138
139
140
# File 'lib/wice_grid.rb', line 137

def add_criteria(extra)
  @extra_filter = extra
  @criteria = @criteria.merge(extra)
end

#all_pages_recordsObject

Returns the list of objects browsable through all pages with the current filters. Should only be called after the grid helper.

Raises:


399
400
401
402
# File 'lib/wice_grid.rb', line 399

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:

Returns:

  • (Boolean)

381
382
383
# File 'lib/wice_grid.rb', line 381

def all_record_mode? #:nodoc:
  @status[:pp]
end

#countObject Also known as: size

:nodoc:


336
337
338
339
# File 'lib/wice_grid.rb', line 336

def count  #:nodoc:
  form_criteria(:skip_ordering => true, :forget_generated_options => true)
  @klass.count(:conditions => @criteria[:conditions], :joins => @criteria[:joins], :include => @criteria[:include], :group => @criteria[:group])
end

#current_page_recordsObject

Returns the list of objects displayed on current page. Should only be called after the grid helper.

Raises:


405
406
407
408
# File 'lib/wice_grid.rb', line 405

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(field_name, custom_filter_active) ⇒ Object

:nodoc:


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/wice_grid.rb', line 196

def declare_column(field_name, custom_filter_active)  #:nodoc:
  field = @klass.fields[field_name]
  raise WiceGridArgumentError.new("Model #{@klass.name} does not have field '#{field_name}'.! ") unless field

  criteria_added, criteria = field.wice_add_filter_criteria(@status[:f], @klass, custom_filter_active)
  @criteria = @criteria.merge(criteria) if criteria
  
  @status[:f].delete(field_name) if @status[:f] && !criteria_added

  @has_any_filter_criteria ||= criteria_added
  [field, nil , true]
end

#distinct_values_for_column(column) ⇒ Object

with this variant we get even those values which do not appear in the resultset


348
349
350
351
352
# File 'lib/wice_grid.rb', line 348

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:


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/wice_grid.rb', line 355

def distinct_values_for_column_in_resultset(messages)  #:nodoc:
  uniq_vals = Set.new

  resultset_without_paging_without_user_filters.each do |ar|
    v = ar.deep_send(*messages)
    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_statusObject

:nodoc:


385
386
387
388
389
# File 'lib/wice_grid.rb', line 385

def dump_status #:nodoc:
  "   params: #{params[name].inspect}\n"  +
  "   status: #{@status.inspect}\n" +
  "   ar_options #{@criteria.inspect}\n"
end

#eachObject

:nodoc:


275
276
277
278
279
280
# File 'lib/wice_grid.rb', line 275

def each   #:nodoc:
  self.read unless @resultset # database querying is late!
  @resultset.each do |r|
    yield r
  end
end

#empty?Boolean

:nodoc:

Returns:

  • (Boolean)

343
344
345
# File 'lib/wice_grid.rb', line 343

def empty?  #:nodoc:
  self.count == 0
end

#filter_params(view_column) ⇒ Object

Getters


265
266
267
268
# File 'lib/wice_grid.rb', line 265

def filter_params(view_column)  #:nodoc:
  return (@status[:f][view_column.attribute_name] || "") if @status[:f]
  ""
end

#filtered_byObject

:nodoc:


300
301
302
# File 'lib/wice_grid.rb', line 300

def filtered_by  #:nodoc:
  @status[:f].nil? ? [] : @status[:f].keys
end

#filtered_by?(view_column) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)

304
305
306
# File 'lib/wice_grid.rb', line 304

def filtered_by?(view_column)  #:nodoc:
  @status[:f] && @status[:f].has_key?(view_column.attribute_name)
end

#filtering_on?Boolean

:nodoc:

Returns:

  • (Boolean)

296
297
298
# File 'lib/wice_grid.rb', line 296

def filtering_on?  #:nodoc:
  not @status[:f].blank?
end

#form_criteria(opts = {}) ⇒ Object

:nodoc:


209
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
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/wice_grid.rb', line 209

def form_criteria(opts = {})  #:nodoc:

  return if @criteria_formed
  @criteria_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

  @status.delete(:f) if !@has_any_filter_criteria

  if !opts[:skip_ordering] && @status[:order]
    order_by = @status[:order].to_sym.send( @status[:order_direction].to_sym )
    @criteria = @criteria.order_by(order_by)
  end

  @criteria = @criteria.limit(@status[:per_page].to_i)
#       #fix-this, Criteria must respect options
#       if self.output_html?
#         @criteria[: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

#         @criteria[:page] = @status[:page]
#         @criteria[:total_entries] = @status[:total_entries] if @status[:total_entries]
#       end

#       @criteria[:joins]   = @options[:joins]
#       @criteria[:include] = @options[:include]
#       @criteria[:group] = @options[:group]
#       @criteria[:select]  = @options[:select]
end

#get_state_as_parameter_value_pairs(including_saved_query_request = false) ⇒ Object

:nodoc:


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/wice_grid.rb', line 308

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

#has_any_filter_criteria?Boolean

Returns:

  • (Boolean)

142
143
144
# File 'lib/wice_grid.rb', line 142

def has_any_filter_criteria?
  @has_any_filter_criteria
end

#has_more_to_show?Boolean

Returns:

  • (Boolean)

146
147
148
# File 'lib/wice_grid.rb', line 146

def has_more_to_show?
  @status[:per_page].to_i < resultset.count
end

#order_directionObject

:nodoc:


292
293
294
# File 'lib/wice_grid.rb', line 292

def order_direction  #:nodoc:
  @status[:order_direction]
end

#ordered_byObject

:nodoc:


287
288
289
# File 'lib/wice_grid.rb', line 287

def ordered_by  #:nodoc:
  @status[:order]
end

#ordered_by?(column) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)

282
283
284
285
# File 'lib/wice_grid.rb', line 282

def ordered_by?(column)  #:nodoc:
  return nil if @status[:order].blank?
  @status[:order] == column.attribute_name
end

#output_csv?Boolean

:nodoc:

Returns:

  • (Boolean)

373
374
375
# File 'lib/wice_grid.rb', line 373

def output_csv? #:nodoc:
  @status[:export] == 'csv'
end

#output_html?Boolean

:nodoc:

Returns:

  • (Boolean)

377
378
379
# File 'lib/wice_grid.rb', line 377

def output_html? #:nodoc:
  @status[:export].blank?
end

#process_loading_queryObject

:nodoc:


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
# File 'lib/wice_grid.rb', line 162

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_paramsObject

:nodoc:


189
190
191
192
193
194
# File 'lib/wice_grid.rb', line 189

def process_params  #:nodoc:
  if this_grid_params
    @status.merge!(this_grid_params)
    @status.delete(:export) unless self.export_to_csv_enabled
  end
end

#readObject

:nodoc:


251
252
253
254
255
256
257
258
# File 'lib/wice_grid.rb', line 251

def read  #:nodoc:
  form_criteria
  with_exclusive_scope do
    @criteria.options[:limit] = nil if @resultset = self.output_csv?
    @resultset = @criteria
  end
  invoke_resultset_callbacks
end

#selected_recordsObject

:nodoc:


392
393
394
395
# File 'lib/wice_grid.rb', line 392

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.


151
152
153
# File 'lib/wice_grid.rb', line 151

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.


158
159
160
# File 'lib/wice_grid.rb', line 158

def with_resultset(&callback)
  @options[:with_resultset] = callback
end