wice_grid_mongoid 0.5.7 → 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -1
- data/Gemfile +17 -0
- data/Gemfile.lock +140 -0
- data/README.rdoc +19 -3
- data/Rakefile +1 -3
- data/VERSION +1 -1
- data/lib/filter_conditions_generators.rb +126 -0
- data/{generators/wice_grid_assets_prototype/templates/stylesheets → lib/generators/wice_grid/templates}/calendarview.css +0 -0
- data/{generators/wice_grid_assets_prototype/templates/javascripts → lib/generators/wice_grid/templates}/calendarview.js +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/arrow_down.gif +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/arrow_up.gif +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/calendar_view_month.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/delete.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/expand.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/page_white_excel.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/page_white_find.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/table.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/table_refresh.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/tick_all.png +0 -0
- data/{generators/common_templates → lib/generators/wice_grid/templates}/icons/untick_all.png +0 -0
- data/{generators/common_templates/stylesheets → lib/generators/wice_grid/templates}/wice_grid.css +0 -0
- data/{generators/common_templates/locales → lib/generators/wice_grid/templates}/wice_grid.yml +0 -0
- data/{generators/common_templates/initializers → lib/generators/wice_grid/templates}/wice_grid_config.rb +0 -0
- data/{generators/wice_grid_assets_jquery/templates/javascripts → lib/generators/wice_grid/templates}/wice_grid_jquery.js +0 -0
- data/{generators/wice_grid_assets_prototype/templates/javascripts → lib/generators/wice_grid/templates}/wice_grid_prototype.js +0 -0
- data/lib/generators/wice_grid/wice_grid_assets_jquery_generator.rb +32 -0
- data/lib/generators/wice_grid/wice_grid_assets_prototype_generator.rb +34 -0
- data/lib/grid_renderer.rb +36 -20
- data/lib/helpers/js_calendar_helpers.rb +0 -5
- data/lib/helpers/wice_grid_view_helpers.rb +113 -133
- data/lib/mongoid_field.rb +50 -0
- data/{tasks → lib/tasks}/wice_grid_tasks.rake +0 -0
- data/lib/view_columns.rb +23 -29
- data/lib/wice_grid.rb +91 -379
- data/lib/wice_grid_misc.rb +3 -3
- data/mongoid_wice_grid.gemspec +122 -0
- data/test/blueprint.rb +17 -0
- data/test/public/javascripts/jquery-1.4.2.min.js +154 -0
- data/test/public/javascripts/wice_grid.js +163 -0
- data/test/rails_mongoid_test.rb +104 -0
- data/test/rails_test_app.rb +71 -0
- data/test/require_gems.rb +19 -0
- data/test/spec_helper.rb +22 -0
- data/test/wice_grid_initializer.rb +215 -0
- data/wice_grid_mongoid.gemspec +43 -30
- metadata +45 -34
- data/generators/wice_grid_assets_jquery/templates/USAGE +0 -6
- data/generators/wice_grid_assets_jquery/wice_grid_assets_jquery_generator.rb +0 -35
- data/generators/wice_grid_assets_prototype/USAGE +0 -8
- data/generators/wice_grid_assets_prototype/wice_grid_assets_prototype_generator.rb +0 -37
- data/init.rb +0 -1
- data/install.rb +0 -1
- data/lib/table_column_matrix.rb +0 -51
- data/uninstall.rb +0 -1
data/lib/wice_grid.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
require 'will_paginate.rb'
|
3
2
|
require 'wice_grid_misc.rb'
|
4
3
|
require 'wice_grid_core_ext.rb'
|
5
4
|
require 'grid_renderer.rb'
|
6
|
-
require 'table_column_matrix.rb'
|
7
5
|
require 'helpers/wice_grid_view_helpers.rb'
|
8
6
|
require 'helpers/wice_grid_misc_view_helpers.rb'
|
9
7
|
require 'helpers/wice_grid_serialized_queries_view_helpers.rb'
|
@@ -11,6 +9,7 @@ require 'helpers/wice_grid_view_helpers.rb'
|
|
11
9
|
require 'helpers/js_calendar_helpers.rb'
|
12
10
|
require 'grid_output_buffer.rb'
|
13
11
|
require 'wice_grid_controller.rb'
|
12
|
+
require 'mongoid_field'
|
14
13
|
require 'wice_grid_spreadsheet.rb'
|
15
14
|
require 'wice_grid_serialized_queries_controller.rb'
|
16
15
|
require 'js_adaptors/js_adaptor.rb'
|
@@ -18,15 +17,34 @@ require 'js_adaptors/jquery_adaptor.rb'
|
|
18
17
|
require 'js_adaptors/prototype_adaptor.rb'
|
19
18
|
require 'view_columns.rb'
|
20
19
|
|
21
|
-
|
22
|
-
ActionController::Base.send(:include, Wice::Controller)
|
20
|
+
|
23
21
|
ActionController::Base.send(:helper_method, :wice_grid_custom_filter_params)
|
24
22
|
|
25
23
|
module Wice
|
26
24
|
|
25
|
+
class WiceGridRailtie < Rails::Railtie
|
26
|
+
|
27
|
+
initializer "wice_grid_railtie.configure_rails_initialization" do |app|
|
28
|
+
ActionController::Base.send(:include, Wice::Controller)
|
29
|
+
Mongoid::Field.send(:include, ::Wice::MongoidField)
|
30
|
+
::ActionView::Base.class_eval { include Wice::GridViewHelper }
|
31
|
+
|
32
|
+
[ActionView::Helpers::AssetTagHelper,
|
33
|
+
ActionView::Helpers::TagHelper,
|
34
|
+
ActionView::Helpers::JavaScriptHelper,
|
35
|
+
ActionView::Helpers::FormTagHelper].each do |m|
|
36
|
+
JsCalendarHelpers.send(:include, m)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
rake_tasks do
|
41
|
+
load 'tasks/wice_grid_tasks.rake'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
27
45
|
class WiceGrid
|
28
46
|
|
29
|
-
attr_reader :klass, :name, :resultset, :custom_order, :query_store_model
|
47
|
+
attr_reader :klass, :name, :resultset, :custom_order, :query_store_model, :options, :controller
|
30
48
|
attr_reader :ar_options, :status, :export_to_csv_enabled, :csv_file_name, :saved_query
|
31
49
|
attr_writer :renderer
|
32
50
|
attr_accessor :output_buffer, :view_helper_finished, :csv_tempfile
|
@@ -36,20 +54,9 @@ module Wice
|
|
36
54
|
def initialize(klass, controller, opts = {}) #:nodoc:
|
37
55
|
@controller = controller
|
38
56
|
|
39
|
-
|
40
|
-
|
41
|
-
unless klass.respond_to?(:paginate)
|
42
|
-
raise Wice::WiceGridException.new('will_paginate not found, WiceGrid cannot proceed. Please install gem mislav-will_paginate. ' +
|
43
|
-
'You might need to add github.com as the gem source before you install the gem: ' +
|
44
|
-
'gem sources -a http://gems.github.com')
|
57
|
+
unless klass.kind_of?(Class) && klass.ancestors.index(Mongoid::Document)
|
58
|
+
raise WiceGridArgumentError.new("The model class (second argument) must be a Class derived from Mongoid::Document")
|
45
59
|
end
|
46
|
-
|
47
|
-
unless klass.kind_of?(Class) && klass.ancestors.index(ActiveRecord::Base)
|
48
|
-
raise WiceGridArgumentError.new("ActiveRecord model class (second argument) must be a Class derived from ActiveRecord::Base")
|
49
|
-
end
|
50
|
-
|
51
|
-
Wice.deprecated_call(:after, :with_resultset, opts)
|
52
|
-
|
53
60
|
# validate :with_resultset & :with_paginated_resultset
|
54
61
|
[:with_resultset, :with_paginated_resultset].each do |callback_symbol|
|
55
62
|
unless [NilClass, Symbol, Proc].index(opts[callback_symbol].class)
|
@@ -79,7 +86,6 @@ module Wice
|
|
79
86
|
:name => Defaults::GRID_NAME,
|
80
87
|
:order => nil,
|
81
88
|
:order_direction => Defaults::ORDER_DIRECTION,
|
82
|
-
:page => 1,
|
83
89
|
:per_page => Defaults::PER_PAGE,
|
84
90
|
:saved_query => nil,
|
85
91
|
:select => nil,
|
@@ -105,11 +111,8 @@ module Wice
|
|
105
111
|
raise WiceGridArgumentError.new("name of the grid can only contain alphanumeruc characters") unless @name =~ /^[a-zA-Z\d_]*$/
|
106
112
|
|
107
113
|
@klass = klass
|
108
|
-
|
109
|
-
@
|
110
|
-
@table_column_matrix.default_model_class = @klass
|
111
|
-
|
112
|
-
@ar_options = {}
|
114
|
+
@criteria = Mongoid::Criteria.new(@klass)
|
115
|
+
@has_any_filter_criteria = false
|
113
116
|
@status = HashWithIndifferentAccess.new
|
114
117
|
|
115
118
|
if @options[:order]
|
@@ -128,12 +131,16 @@ module Wice
|
|
128
131
|
|
129
132
|
process_loading_query
|
130
133
|
process_params
|
131
|
-
|
132
|
-
@ar_options_formed = false
|
133
|
-
|
134
|
-
@method_scoping = @klass.send(:scoped_methods)[-1]
|
134
|
+
@criteria_formed = false
|
135
135
|
end
|
136
136
|
|
137
|
+
def has_any_filter_criteria?
|
138
|
+
@has_any_filter_criteria
|
139
|
+
end
|
140
|
+
|
141
|
+
def has_more_to_show?
|
142
|
+
@status[:per_page].to_i < resultset.count
|
143
|
+
end
|
137
144
|
# A block executed from within the plugin to process records of the current page.
|
138
145
|
# The argument to the callback is the array of the records. See the README for more details.
|
139
146
|
def with_paginated_resultset(&callback)
|
@@ -181,40 +188,21 @@ module Wice
|
|
181
188
|
end
|
182
189
|
end
|
183
190
|
|
184
|
-
def declare_column(
|
185
|
-
|
186
|
-
|
187
|
-
raise WiceGridArgumentError.new("Column '#{column_name}' is not found in table '#{model_class.table_name}'!") if column.nil?
|
188
|
-
main_table = false
|
189
|
-
table_name = model_class.table_name
|
190
|
-
else
|
191
|
-
column = @table_column_matrix.get_column_in_default_model_class_by_column_name(column_name)
|
192
|
-
if column.nil?
|
193
|
-
raise WiceGridArgumentError.new("Column '#{column_name}' is not found in table '#{@klass.table_name}'! " +
|
194
|
-
"If '#{column_name}' belongs to another table you should declare it in :include or :join when initialising " +
|
195
|
-
"the grid, and specify :model_class in column declaration.")
|
196
|
-
end
|
197
|
-
main_table = true
|
198
|
-
table_name = @table_column_matrix.default_model_class.table_name
|
199
|
-
end
|
191
|
+
def declare_column(field_name, custom_filter_active) #:nodoc:
|
192
|
+
field = @klass.fields[field_name]
|
193
|
+
raise WiceGridArgumentError.new("Model #{@klass.name} does not have field '#{field_name}'.! ") unless field
|
200
194
|
|
201
|
-
|
202
|
-
|
203
|
-
if @status[:f] && conditions.blank?
|
204
|
-
@status[:f].delete(current_parameter_name)
|
205
|
-
end
|
195
|
+
criteria_added = field.wice_add_filter_criteria(@status[:f], @criteria, custom_filter_active)
|
196
|
+
@status[:f].delete(field_name) if @status[:f] && !criteria_added
|
206
197
|
|
207
|
-
|
208
|
-
|
209
|
-
else
|
210
|
-
nil
|
211
|
-
end
|
198
|
+
@has_any_filter_criteria ||= criteria_added
|
199
|
+
[field, nil , true]
|
212
200
|
end
|
213
201
|
|
214
|
-
def
|
202
|
+
def form_criteria(opts = {}) #:nodoc:
|
215
203
|
|
216
|
-
return if @
|
217
|
-
@
|
204
|
+
return if @criteria_formed
|
205
|
+
@criteria_formed = true unless opts[:forget_generated_options]
|
218
206
|
|
219
207
|
# validate @status[:order_direction]
|
220
208
|
@status[:order_direction] = case @status[:order_direction]
|
@@ -226,42 +214,38 @@ module Wice
|
|
226
214
|
''
|
227
215
|
end
|
228
216
|
|
229
|
-
|
230
|
-
if @table_column_matrix.generated_conditions.size == 0
|
231
|
-
@status.delete(:f)
|
232
|
-
end
|
233
|
-
|
234
|
-
@ar_options[:conditions] = klass.send(:merge_conditions, @status[:conditions], * @table_column_matrix.conditions )
|
235
|
-
# conditions processed
|
217
|
+
@status.delete(:f) if !@has_any_filter_criteria
|
236
218
|
|
237
|
-
if
|
238
|
-
@
|
239
|
-
|
240
|
-
@ar_options[:order] += ' ' + @status[:order_direction]
|
219
|
+
if !opts[:skip_ordering] && @status[:order]
|
220
|
+
order_by = @status[:order].to_sym.send( @status[:order_direction].to_sym )
|
221
|
+
@criteria.order_by(order_by)
|
241
222
|
end
|
242
223
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
224
|
+
@criteria.limit(@status[:per_page].to_i)
|
225
|
+
# #fix-this, Criteria must respect options
|
226
|
+
# if self.output_html?
|
227
|
+
# @criteria[:per_page] = if all_record_mode?
|
228
|
+
# # reset the :pp value in all records mode
|
229
|
+
# @status[:pp] = count_resultset_without_paging_without_user_filters
|
230
|
+
# else
|
231
|
+
# @status[:per_page]
|
232
|
+
# end
|
250
233
|
|
251
|
-
|
252
|
-
|
253
|
-
|
234
|
+
# @criteria[:page] = @status[:page]
|
235
|
+
# @criteria[:total_entries] = @status[:total_entries] if @status[:total_entries]
|
236
|
+
# end
|
254
237
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
238
|
+
# @criteria[:joins] = @options[:joins]
|
239
|
+
# @criteria[:include] = @options[:include]
|
240
|
+
# @criteria[:group] = @options[:group]
|
241
|
+
# @criteria[:select] = @options[:select]
|
259
242
|
end
|
260
243
|
|
261
244
|
def read #:nodoc:
|
262
|
-
|
245
|
+
form_criteria
|
263
246
|
with_exclusive_scope do
|
264
|
-
@
|
247
|
+
@criteria.options[:limit] = nil if @resultset = self.output_csv?
|
248
|
+
@resultset = @criteria
|
265
249
|
end
|
266
250
|
invoke_resultset_callbacks
|
267
251
|
end
|
@@ -272,12 +256,8 @@ module Wice
|
|
272
256
|
# Getters
|
273
257
|
|
274
258
|
def filter_params(view_column) #:nodoc:
|
275
|
-
|
276
|
-
|
277
|
-
@status[:f][column_name]
|
278
|
-
else
|
279
|
-
{}
|
280
|
-
end
|
259
|
+
return (@status[:f][view_column.attribute_name] || {}) if @status[:f]
|
260
|
+
{}
|
281
261
|
end
|
282
262
|
|
283
263
|
def resultset #:nodoc:
|
@@ -294,11 +274,7 @@ module Wice
|
|
294
274
|
|
295
275
|
def ordered_by?(column) #:nodoc:
|
296
276
|
return nil if @status[:order].blank?
|
297
|
-
|
298
|
-
@status[:order] == column.attribute_name
|
299
|
-
else
|
300
|
-
@status[:order] == column.table_alias_or_table_name + '.' + column.attribute_name
|
301
|
-
end
|
277
|
+
@status[:order] == column.attribute_name
|
302
278
|
end
|
303
279
|
|
304
280
|
def ordered_by #:nodoc:
|
@@ -319,7 +295,7 @@ module Wice
|
|
319
295
|
end
|
320
296
|
|
321
297
|
def filtered_by?(view_column) #:nodoc:
|
322
|
-
@status[:f]
|
298
|
+
@status[:f] && @status[:f].has_key?(view_column.attribute_name)
|
323
299
|
end
|
324
300
|
|
325
301
|
def get_state_as_parameter_value_pairs(including_saved_query_request = false) #:nodoc:
|
@@ -351,8 +327,8 @@ module Wice
|
|
351
327
|
end
|
352
328
|
|
353
329
|
def count #:nodoc:
|
354
|
-
|
355
|
-
@klass.count(:conditions => @
|
330
|
+
form_criteria(:skip_ordering => true, :forget_generated_options => true)
|
331
|
+
@klass.count(:conditions => @criteria[:conditions], :joins => @criteria[:joins], :include => @criteria[:include], :group => @criteria[:group])
|
356
332
|
end
|
357
333
|
|
358
334
|
alias_method :size, :count
|
@@ -402,7 +378,7 @@ module Wice
|
|
402
378
|
def dump_status #:nodoc:
|
403
379
|
" params: #{params[name].inspect}\n" +
|
404
380
|
" status: #{@status.inspect}\n" +
|
405
|
-
" ar_options #{@
|
381
|
+
" ar_options #{@criteria.inspect}\n"
|
406
382
|
end
|
407
383
|
|
408
384
|
|
@@ -443,45 +419,14 @@ module Wice
|
|
443
419
|
end
|
444
420
|
|
445
421
|
def with_exclusive_scope #:nodoc:
|
446
|
-
|
447
|
-
@klass.send(:with_exclusive_scope, @method_scoping) do
|
448
|
-
yield
|
449
|
-
end
|
450
|
-
else
|
451
|
-
yield
|
452
|
-
end
|
453
|
-
end
|
454
|
-
|
455
|
-
|
456
|
-
def add_custom_order_sql(fully_qualified_column_name) #:nodoc:
|
457
|
-
custom_order = if @options[:custom_order].has_key?(fully_qualified_column_name)
|
458
|
-
@options[:custom_order][fully_qualified_column_name]
|
459
|
-
else
|
460
|
-
if view_column = @renderer[fully_qualified_column_name]
|
461
|
-
view_column.custom_order
|
462
|
-
else
|
463
|
-
nil
|
464
|
-
end
|
465
|
-
end
|
466
|
-
|
467
|
-
if custom_order.blank?
|
468
|
-
ActiveRecord::Base.connection.quote_table_name(fully_qualified_column_name.strip)
|
469
|
-
else
|
470
|
-
if custom_order.is_a? String
|
471
|
-
custom_order.gsub(/\?/, fully_qualified_column_name)
|
472
|
-
elsif custom_order.is_a? Proc
|
473
|
-
custom_order.call(fully_qualified_column_name)
|
474
|
-
else
|
475
|
-
raise WiceGridArgumentError.new("invalid custom order #{custom_order.inspect}")
|
476
|
-
end
|
477
|
-
end
|
422
|
+
yield
|
478
423
|
end
|
479
424
|
|
480
425
|
def complete_column_name(col_name) #:nodoc:
|
481
426
|
if col_name.index('.') # already has a table name
|
482
427
|
col_name
|
483
428
|
else # add the default table
|
484
|
-
"#{@klass.
|
429
|
+
"#{@klass.collection_name}.#{col_name}"
|
485
430
|
end
|
486
431
|
end
|
487
432
|
|
@@ -495,22 +440,22 @@ module Wice
|
|
495
440
|
|
496
441
|
|
497
442
|
def resultset_without_paging_without_user_filters #:nodoc:
|
498
|
-
|
443
|
+
form_criteria
|
499
444
|
with_exclusive_scope do
|
500
|
-
@klass.find(:all, :joins => @
|
501
|
-
:include => @
|
502
|
-
:group => @
|
445
|
+
@klass.find(:all, :joins => @criteria[:joins],
|
446
|
+
:include => @criteria[:include],
|
447
|
+
:group => @criteria[:group],
|
503
448
|
:conditions => @options[:conditions])
|
504
449
|
end
|
505
450
|
end
|
506
451
|
|
507
452
|
def count_resultset_without_paging_without_user_filters #:nodoc:
|
508
|
-
|
453
|
+
form_criteria
|
509
454
|
with_exclusive_scope do
|
510
455
|
@klass.count(
|
511
|
-
:joins => @
|
512
|
-
:include => @
|
513
|
-
:group => @
|
456
|
+
:joins => @criteria[:joins],
|
457
|
+
:include => @criteria[:include],
|
458
|
+
:group => @criteria[:group],
|
514
459
|
:conditions => @options[:conditions]
|
515
460
|
)
|
516
461
|
end
|
@@ -518,13 +463,13 @@ module Wice
|
|
518
463
|
|
519
464
|
|
520
465
|
def resultset_without_paging_with_user_filters #:nodoc:
|
521
|
-
|
466
|
+
form_criteria
|
522
467
|
with_exclusive_scope do
|
523
|
-
@klass.find(:all, :joins => @
|
524
|
-
:include => @
|
525
|
-
:group => @
|
526
|
-
:conditions => @
|
527
|
-
:order => @
|
468
|
+
@klass.find(:all, :joins => @criteria[:joins],
|
469
|
+
:include => @criteria[:include],
|
470
|
+
:group => @criteria[:group],
|
471
|
+
:conditions => @criteria[:conditions],
|
472
|
+
:order => @criteria[:order])
|
528
473
|
end
|
529
474
|
end
|
530
475
|
|
@@ -571,239 +516,6 @@ module Wice
|
|
571
516
|
end
|
572
517
|
end
|
573
518
|
|
574
|
-
# to be mixed in into ActiveRecord::ConnectionAdapters::Column
|
575
|
-
module WiceGridExtentionToActiveRecordColumn #:nodoc:
|
576
|
-
|
577
|
-
attr_accessor :model_klass
|
578
|
-
|
579
|
-
def alias_or_table_name(table_alias)
|
580
|
-
table_alias || self.model_klass.table_name
|
581
|
-
end
|
582
|
-
|
583
|
-
def wg_initialize_request_parameters(all_filter_params, main_table, table_alias, custom_filter_active) #:nodoc:
|
584
|
-
@request_params = nil
|
585
|
-
return if all_filter_params.nil?
|
586
|
-
|
587
|
-
# if the parameter does not specify the table name we only allow columns in the default table to use these parameters
|
588
|
-
if main_table && @request_params = all_filter_params[self.name]
|
589
|
-
current_parameter_name = self.name
|
590
|
-
elsif @request_params = all_filter_params[alias_or_table_name(table_alias) + '.' + self.name]
|
591
|
-
current_parameter_name = alias_or_table_name(table_alias) + '.' + self.name
|
592
|
-
end
|
593
|
-
|
594
|
-
# Preprocess incoming parameters for datetime, if what's coming in is
|
595
|
-
# a datetime (with custom_filter it can be anything else, and not
|
596
|
-
# the datetime hash {:fr => ..., :to => ...})
|
597
|
-
if @request_params
|
598
|
-
if (self.type == :datetime || self.type == :timestamp) && @request_params.is_a?(Hash)
|
599
|
-
[:fr, :to].each do |sym|
|
600
|
-
if @request_params[sym]
|
601
|
-
if @request_params[sym].is_a?(String)
|
602
|
-
@request_params[sym] = Wice::Defaults::DATETIME_PARSER.call(@request_params[sym])
|
603
|
-
elsif @request_params[sym].is_a?(Hash)
|
604
|
-
@request_params[sym] = ::Wice::GridTools.params_2_datetime(@request_params[sym])
|
605
|
-
end
|
606
|
-
end
|
607
|
-
end
|
608
|
-
|
609
|
-
end
|
610
|
-
|
611
|
-
# Preprocess incoming parameters for date, if what's coming in is
|
612
|
-
# a date (with custom_filter it can be anything else, and not
|
613
|
-
# the date hash {:fr => ..., :to => ...})
|
614
|
-
if self.type == :date && @request_params.is_a?(Hash)
|
615
|
-
[:fr, :to].each do |sym|
|
616
|
-
if @request_params[sym]
|
617
|
-
if @request_params[sym].is_a?(String)
|
618
|
-
@request_params[sym] = Wice::Defaults::DATE_PARSER.call(@request_params[sym])
|
619
|
-
elsif @request_params[sym].is_a?(Hash)
|
620
|
-
@request_params[sym] = ::Wice::GridTools.params_2_date(@request_params[sym])
|
621
|
-
end
|
622
|
-
end
|
623
|
-
end
|
624
|
-
end
|
625
|
-
end
|
626
|
-
|
627
|
-
return wg_generate_conditions(table_alias, custom_filter_active), current_parameter_name
|
628
|
-
end
|
629
|
-
|
630
|
-
def wg_generate_conditions(table_alias, custom_filter_active) #:nodoc:
|
631
|
-
return nil if @request_params.nil?
|
632
|
-
|
633
|
-
if custom_filter_active
|
634
|
-
return ::Wice::FilterConditionsGeneratorCustomFilter.new(self).generate_conditions(table_alias, @request_params)
|
635
|
-
end
|
636
|
-
|
637
|
-
column_type = self.type.to_s
|
638
|
-
|
639
|
-
processor_class = ::Wice::FilterConditionsGenerator.handled_type[column_type]
|
640
|
-
|
641
|
-
if processor_class
|
642
|
-
return processor_class.new(self).generate_conditions(table_alias, @request_params)
|
643
|
-
else
|
644
|
-
Wice.log("No processor for database type #{column_type}!!!")
|
645
|
-
nil
|
646
|
-
end
|
647
|
-
end
|
648
|
-
|
649
|
-
end
|
650
|
-
|
651
|
-
class FilterConditionsGenerator #:nodoc:
|
652
|
-
|
653
|
-
cattr_accessor :handled_type
|
654
|
-
@@handled_type = HashWithIndifferentAccess.new
|
655
|
-
|
656
|
-
def initialize(column) #:nodoc:
|
657
|
-
@column = column
|
658
|
-
end
|
659
|
-
end
|
660
|
-
|
661
|
-
class FilterConditionsGeneratorCustomFilter < FilterConditionsGenerator #:nodoc:
|
662
|
-
|
663
|
-
def generate_conditions(table_alias, opts) #:nodoc:
|
664
|
-
if opts.empty?
|
665
|
-
Wice.log "empty parameters for the grid custom filter"
|
666
|
-
return false
|
667
|
-
end
|
668
|
-
opts = (opts.kind_of?(Array) && opts.size == 1) ? opts[0] : opts
|
669
|
-
|
670
|
-
if opts.kind_of?(Array)
|
671
|
-
opts_with_special_values, normal_opts = opts.partition{|v| ::Wice::GridTools.special_value(v)}
|
672
519
|
|
673
|
-
conditions_ar = if normal_opts.size > 0
|
674
|
-
[" #{@column.alias_or_table_name(table_alias)}.#{@column.name} IN ( " + (['?'] * normal_opts.size).join(', ') + ' )'] + normal_opts
|
675
|
-
else
|
676
|
-
[]
|
677
|
-
end
|
678
|
-
|
679
|
-
if opts_with_special_values.size > 0
|
680
|
-
special_conditions = opts_with_special_values.collect{|v| " #{@column.alias_or_table_name(table_alias)}.#{@column.name} is " + v}.join(' or ')
|
681
|
-
if conditions_ar.size > 0
|
682
|
-
conditions_ar[0] = " (#{conditions_ar[0]} or #{special_conditions} ) "
|
683
|
-
else
|
684
|
-
conditions_ar = " ( #{special_conditions} ) "
|
685
|
-
end
|
686
|
-
end
|
687
|
-
conditions_ar
|
688
|
-
else
|
689
|
-
if ::Wice::GridTools.special_value(opts)
|
690
|
-
" #{@column.alias_or_table_name(table_alias)}.#{@column.name} is " + opts
|
691
|
-
else
|
692
|
-
[" #{@column.alias_or_table_name(table_alias)}.#{@column.name} = ?", opts]
|
693
|
-
end
|
694
|
-
end
|
695
|
-
end
|
696
|
-
|
697
|
-
end
|
698
|
-
|
699
|
-
class FilterConditionsGeneratorBoolean < FilterConditionsGenerator #:nodoc:
|
700
|
-
@@handled_type[:boolean] = self
|
701
|
-
|
702
|
-
def generate_conditions(table_alias, opts) #:nodoc:
|
703
|
-
unless (opts.kind_of?(Array) && opts.size == 1)
|
704
|
-
Wice.log "invalid parameters for the grid boolean filter - must be an one item array: #{opts.inspect}"
|
705
|
-
return false
|
706
|
-
end
|
707
|
-
opts = opts[0]
|
708
|
-
if opts == 'f'
|
709
|
-
[" (#{@column.alias_or_table_name(table_alias)}.#{@column.name} = ? or #{@column.alias_or_table_name(table_alias)}.#{@column.name} is null) ", false]
|
710
|
-
elsif opts == 't'
|
711
|
-
[" #{@column.alias_or_table_name(table_alias)}.#{@column.name} = ?", true]
|
712
|
-
else
|
713
|
-
nil
|
714
|
-
end
|
715
|
-
end
|
716
|
-
end
|
717
|
-
|
718
|
-
class FilterConditionsGeneratorString < FilterConditionsGenerator #:nodoc:
|
719
|
-
@@handled_type[:string] = self
|
720
|
-
@@handled_type[:text] = self
|
721
|
-
|
722
|
-
def generate_conditions(table_alias, opts) #:nodoc:
|
723
|
-
if opts.kind_of? String
|
724
|
-
string_fragment = opts
|
725
|
-
negation = ''
|
726
|
-
elsif (opts.kind_of? Hash) && opts.has_key?(:v)
|
727
|
-
string_fragment = opts[:v]
|
728
|
-
negation = opts[:n] == '1' ? 'NOT' : ''
|
729
|
-
else
|
730
|
-
Wice.log "invalid parameters for the grid string filter - must be a string: #{opts.inspect} or a Hash with keys :v and :n"
|
731
|
-
return false
|
732
|
-
end
|
733
|
-
if string_fragment.empty?
|
734
|
-
Wice.log "invalid parameters for the grid string filter - empty string"
|
735
|
-
return false
|
736
|
-
end
|
737
|
-
[" #{negation} #{@column.alias_or_table_name(table_alias)}.#{@column.name} #{::Wice.get_string_matching_operators(@column.model_klass)} ?",
|
738
|
-
'%' + string_fragment + '%']
|
739
|
-
end
|
740
|
-
|
741
|
-
end
|
742
|
-
|
743
|
-
class FilterConditionsGeneratorInteger < FilterConditionsGenerator #:nodoc:
|
744
|
-
@@handled_type[:integer] = self
|
745
|
-
@@handled_type[:float] = self
|
746
|
-
@@handled_type[:decimal] = self
|
747
|
-
|
748
|
-
def generate_conditions(table_alias, opts) #:nodoc:
|
749
|
-
unless opts.kind_of? Hash
|
750
|
-
Wice.log "invalid parameters for the grid integer filter - must be a hash"
|
751
|
-
return false
|
752
|
-
end
|
753
|
-
conditions = [[]]
|
754
|
-
if opts[:fr]
|
755
|
-
if opts[:fr] =~ /\d/
|
756
|
-
conditions[0] << " #{@column.alias_or_table_name(table_alias)}.#{@column.name} >= ? "
|
757
|
-
conditions << opts[:fr]
|
758
|
-
else
|
759
|
-
opts.delete(:fr)
|
760
|
-
end
|
761
|
-
end
|
762
|
-
|
763
|
-
if opts[:to]
|
764
|
-
if opts[:to] =~ /\d/
|
765
|
-
conditions[0] << " #{@column.alias_or_table_name(table_alias)}.#{@column.name} <= ? "
|
766
|
-
conditions << opts[:to]
|
767
|
-
else
|
768
|
-
opts.delete(:to)
|
769
|
-
end
|
770
|
-
end
|
771
|
-
|
772
|
-
if conditions.size == 1
|
773
|
-
Wice.log "invalid parameters for the grid integer filter - either range limits are not supplied or they are not numeric"
|
774
|
-
return false
|
775
|
-
end
|
776
|
-
|
777
|
-
conditions[0] = conditions[0].join(' and ')
|
778
|
-
|
779
|
-
return conditions
|
780
|
-
end
|
781
|
-
end
|
782
|
-
|
783
|
-
class FilterConditionsGeneratorDate < FilterConditionsGenerator #:nodoc:
|
784
|
-
@@handled_type[:date] = self
|
785
|
-
@@handled_type[:datetime] = self
|
786
|
-
@@handled_type[:timestamp] = self
|
787
|
-
|
788
|
-
def generate_conditions(table_alias, opts) #:nodoc:
|
789
|
-
conditions = [[]]
|
790
|
-
if opts[:fr]
|
791
|
-
conditions[0] << " #{@column.alias_or_table_name(table_alias)}.#{@column.name} >= ? "
|
792
|
-
conditions << opts[:fr]
|
793
|
-
end
|
794
|
-
|
795
|
-
if opts[:to]
|
796
|
-
conditions[0] << " #{@column.alias_or_table_name(table_alias)}.#{@column.name} <= ? "
|
797
|
-
conditions << opts[:to]
|
798
|
-
end
|
799
|
-
|
800
|
-
return false if conditions.size == 1
|
801
|
-
|
802
|
-
conditions[0] = conditions[0].join(' and ')
|
803
|
-
return conditions
|
804
|
-
end
|
805
|
-
end
|
806
520
|
|
807
521
|
end
|
808
|
-
|
809
|
-
ActiveRecord::ConnectionAdapters::Column.send(:include, ::Wice::WiceGridExtentionToActiveRecordColumn)
|
data/lib/wice_grid_misc.rb
CHANGED
@@ -29,7 +29,7 @@ module Wice
|
|
29
29
|
|
30
30
|
def get_string_matching_operators(model) #:nodoc:
|
31
31
|
if defined?(Wice::Defaults::STRING_MATCHING_OPERATORS) && Wice::Defaults::STRING_MATCHING_OPERATORS.is_a?(Hash) &&
|
32
|
-
str_matching_operator =
|
32
|
+
str_matching_operator =(Wice::Defaults::STRING_MATCHING_OPERATORS[model.connection.class.to_s] rescue nil)
|
33
33
|
str_matching_operator
|
34
34
|
else
|
35
35
|
Wice::Defaults::STRING_MATCHING_OPERATOR
|
@@ -45,7 +45,7 @@ module Wice
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def log(message) #:nodoc:
|
48
|
-
|
48
|
+
ActionController::Base.logger.info('WiceGrid: ' + message)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -96,4 +96,4 @@ module Wice
|
|
96
96
|
class WiceGridException < Exception #:nodoc:
|
97
97
|
include ExceptionsMixin
|
98
98
|
end
|
99
|
-
end
|
99
|
+
end
|