wice_grid_mongoid 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/CHANGELOG +409 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +1172 -0
- data/Rakefile +42 -0
- data/SAVED_QUERIES_HOWTO.rdoc +123 -0
- data/VERSION +1 -0
- data/generators/common_templates/icons/arrow_down.gif +0 -0
- data/generators/common_templates/icons/arrow_up.gif +0 -0
- data/generators/common_templates/icons/calendar_view_month.png +0 -0
- data/generators/common_templates/icons/delete.png +0 -0
- data/generators/common_templates/icons/expand.png +0 -0
- data/generators/common_templates/icons/page_white_excel.png +0 -0
- data/generators/common_templates/icons/page_white_find.png +0 -0
- data/generators/common_templates/icons/table.png +0 -0
- data/generators/common_templates/icons/table_refresh.png +0 -0
- data/generators/common_templates/icons/tick_all.png +0 -0
- data/generators/common_templates/icons/untick_all.png +0 -0
- data/generators/common_templates/initializers/wice_grid_config.rb +215 -0
- data/generators/common_templates/locales/wice_grid.yml +269 -0
- data/generators/common_templates/stylesheets/wice_grid.css +173 -0
- data/generators/wice_grid_assets_jquery/templates/USAGE +6 -0
- data/generators/wice_grid_assets_jquery/templates/javascripts/wice_grid_jquery.js +161 -0
- data/generators/wice_grid_assets_jquery/wice_grid_assets_jquery_generator.rb +35 -0
- data/generators/wice_grid_assets_prototype/USAGE +8 -0
- data/generators/wice_grid_assets_prototype/templates/javascripts/calendarview.js +1168 -0
- data/generators/wice_grid_assets_prototype/templates/javascripts/wice_grid_prototype.js +153 -0
- data/generators/wice_grid_assets_prototype/templates/stylesheets/calendarview.css +107 -0
- data/generators/wice_grid_assets_prototype/wice_grid_assets_prototype_generator.rb +37 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/grid_output_buffer.rb +52 -0
- data/lib/grid_renderer.rb +531 -0
- data/lib/helpers/js_calendar_helpers.rb +188 -0
- data/lib/helpers/wice_grid_misc_view_helpers.rb +113 -0
- data/lib/helpers/wice_grid_serialized_queries_view_helpers.rb +82 -0
- data/lib/helpers/wice_grid_view_helpers.rb +781 -0
- data/lib/js_adaptors/jquery_adaptor.rb +145 -0
- data/lib/js_adaptors/js_adaptor.rb +12 -0
- data/lib/js_adaptors/prototype_adaptor.rb +168 -0
- data/lib/table_column_matrix.rb +51 -0
- data/lib/view_columns.rb +469 -0
- data/lib/views/create.rjs +13 -0
- data/lib/views/delete.rjs +12 -0
- data/lib/wice_grid.rb +809 -0
- data/lib/wice_grid_controller.rb +165 -0
- data/lib/wice_grid_core_ext.rb +179 -0
- data/lib/wice_grid_misc.rb +99 -0
- data/lib/wice_grid_serialized_queries_controller.rb +77 -0
- data/lib/wice_grid_serialized_query.rb +14 -0
- data/lib/wice_grid_spreadsheet.rb +33 -0
- data/tasks/wice_grid_tasks.rake +28 -0
- data/test/.gitignore +2 -0
- data/test/database.yml +21 -0
- data/test/schema.rb +33 -0
- data/test/test_helper.rb +89 -0
- data/test/views/projects_and_people_grid.html.erb +12 -0
- data/test/views/projects_and_people_grid_invalid.html.erb +12 -0
- data/test/views/simple_projects_grid.html.erb +9 -0
- data/test/wice_grid_core_ext_test.rb +183 -0
- data/test/wice_grid_functional_test.rb +68 -0
- data/test/wice_grid_misc_test.rb +41 -0
- data/test/wice_grid_test.rb +42 -0
- data/test/wice_grid_view_helper_test.rb +12 -0
- data/uninstall.rb +1 -0
- data/wice_grid_mongoid.gemspec +111 -0
- metadata +141 -0
@@ -0,0 +1,165 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Wice
|
3
|
+
module Controller
|
4
|
+
|
5
|
+
def self.included(base) #:nodoc:
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
# Used to add query processing action methods into a controller.
|
12
|
+
# Read section "Saving Queries How-To" in README for more details.
|
13
|
+
def save_wice_grid_queries
|
14
|
+
include Wice::SerializedQueriesControllerMixin
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
attr_accessor :wice_grid_instances
|
21
|
+
|
22
|
+
# Creates a grid object to be used in the view. This is the <i>main</i> model, and the underlying table is the default table -
|
23
|
+
# if in other parameters a column name is mentioned without the name of the table, this table is implied.
|
24
|
+
# Just like in an ordinary ActiveRecord <tt>find</tt> you can use <tt>:joins</tt>, <tt>:include</tt>, and <tt>:conditions</tt>.
|
25
|
+
#
|
26
|
+
# The first parameter is an ActiveRecord class name. The generated ActiveRecord call will use it as the
|
27
|
+
# receiver of the <tt>paginate</tt> method: <tt>klass.paginate(...)</tt>
|
28
|
+
#
|
29
|
+
# The second parameters is a hash of parameters:
|
30
|
+
# * <tt>:joins</tt> - ActiveRecord <tt>:joins</tt> option.
|
31
|
+
# * <tt>:include</tt> - ActiveRecord <tt>:include</tt> option.
|
32
|
+
# * <tt>:conditions</tt> - ActiveRecord <tt>:conditions</tt> option.
|
33
|
+
# * <tt>:per_page</tt> - Number of rows per one page. The default is 10.
|
34
|
+
# * <tt>:page</tt> - The page to show when rendering the grid for the first time. The default is one, naturally.
|
35
|
+
# * <tt>:order</tt> - Name of the column to sort by. Can be of a short form (just the name of the column) if this
|
36
|
+
# is a column of the main table (the table of the main ActiveRecord model, the first parameter of <tt>initialize_grid</tt>),
|
37
|
+
# or a fully qualified name with the name of the table.
|
38
|
+
# * <tt>:order_direction</tt> - <tt>:asc</tt> for ascending or <tt>:desc</tt> for descending. The default is <tt>:asc</tt>.
|
39
|
+
# * <tt>:name</tt> - name of the grid. Only needed if there is a second grid on a page. The name serves as the base name for
|
40
|
+
# HTTP parametes, DOM IDs, etc. The shorter the name, the shorter the GET request is. The name can only contain alphanumeruc characters.
|
41
|
+
# * <tt>:enable_export_to_csv</tt> - <Enable export of the table to CSV. Read the How-To to learn what else is needed to enable CSV export.
|
42
|
+
# * <tt>:csv_file_name</tt> - Name of the exported CSV file. If the parameter is missing, the name of the grid will be used instead.
|
43
|
+
# * <tt>:custom_order</tt> - used for overriding the ORDER BY clause with custom sql code (for example, including a function).
|
44
|
+
# The value of the parameter is a hash where keys are fully qualified names
|
45
|
+
# of database columns, and values the required chunks of SQL to use in the ORDER BY clause, either as strings or Proc object
|
46
|
+
# evaluating to string. See section 'Custom Ordering' in the README.
|
47
|
+
# * <tt>:saved_query</tt> - id of the saved query or the query object itself to load initially.
|
48
|
+
# Read section "Saving Queries How-To" in README for more details.
|
49
|
+
# * <tt>:after</tt> - defined a name of a controller method which would be called by the grid after all user input has been processed,
|
50
|
+
# with a single parameter which is a Proc object. Once called, the object returns a list of all records of the current selection
|
51
|
+
# throughout all pages. See section "Integration With The Application" in the README.
|
52
|
+
# * <tt>:total_entries</tt> - If not specified, <tt>will_paginate</tt> will run a <tt>select count</tt>
|
53
|
+
# * <tt>:select</tt> - ActiveRecord <tt>:select</tt> option. Please do not forget that <tt>:select</tt> is ignored
|
54
|
+
# when <tt>:include</tt> is present. It is unlikely you would need <tt>:select</tt> with WiceGrid, but if you do,
|
55
|
+
# use it with care :)
|
56
|
+
# * <tt>:group</tt> - ActiveRecord <tt>:group</tt> option. Use it if you are sure you know what you are doing :)
|
57
|
+
# * <tt>:with_paginated_resultset</tt> - a callback executed from within the plugin to process records of the current page.
|
58
|
+
# Can be a lambda object or a controller method name (symbol). The argument to the callback is the array of the records.
|
59
|
+
# * <tt>:with_resultset</tt> - a callback executed from within the plugin to process all records browsable through
|
60
|
+
# all pages with the current filters. Can be a lambda object or a controller method name (symbol). The argument to
|
61
|
+
# the callback is a lambda object which returns the list of records when called. See the README for the explanation.
|
62
|
+
#
|
63
|
+
# Defaults for parameters <tt>:per_page</tt>, <tt>:order_direction</tt>, <tt>:name</tt>, and <tt>:erb_mode</tt>
|
64
|
+
# can be changed in <tt>lib/wice_grid_config.rb</tt>, this is convenient if you want to set a project wide setting
|
65
|
+
# without having to repeat it for every grid instance.
|
66
|
+
|
67
|
+
|
68
|
+
def initialize_grid(klass, opts = {})
|
69
|
+
Wice::JsAdaptor.init
|
70
|
+
@__wice_grid_on_page = true
|
71
|
+
wg = WiceGrid.new(klass, self, opts)
|
72
|
+
self.wice_grid_instances = [] if self.wice_grid_instances.nil?
|
73
|
+
self.wice_grid_instances << wg
|
74
|
+
wg
|
75
|
+
end
|
76
|
+
|
77
|
+
# +export_grid_if_requested+ is a controller method which should be called at the end of each action containing grids with enabled
|
78
|
+
# CSV export.
|
79
|
+
#
|
80
|
+
# CSV export will only work if each WiceGrid helper is placed in a partial of its own (requiring it from the master template
|
81
|
+
# of course for the usual flow).
|
82
|
+
# +export_grid_if_requested+ intercepts CSV export requests and evaluates the corresponding partial with the required grid helper.
|
83
|
+
# By default for each grid +export_grid_if_requested+ will look for a partial
|
84
|
+
# whose name follows the following pattern:
|
85
|
+
#
|
86
|
+
# _GRID_NAME_grid.html.erb
|
87
|
+
#
|
88
|
+
# For example, a grid named +orders+ is supposed to be found in a template called <tt>_orders_grid.html.erb</tt>,
|
89
|
+
# Remember that the default name of grids is +grid+.
|
90
|
+
#
|
91
|
+
# This convention can be easily overridden by supplying a hash parameter to +export_grid_if_requested+ where each key is the name of
|
92
|
+
# a grid, and the value is the name of the template (like it is specified for +render+, i.e. without '_' and extensions):
|
93
|
+
#
|
94
|
+
# export_grid_if_requested(:grid => 'orders', 'grid2' => 'invoices')
|
95
|
+
#
|
96
|
+
# If the request is not a CSV export request, the method does nothing and returns +false+, if it is a CSV export request,
|
97
|
+
# the method returns +true+.
|
98
|
+
#
|
99
|
+
# If the action has no explicit +render+ call, it's OK to just place +export_grid_if_requested+ as the last line of the action. Otherwise,
|
100
|
+
# to avoid double rendering, use the return value of the method to conditionally call your +render+ :
|
101
|
+
#
|
102
|
+
# export_grid_if_requested || render(:action => 'index')
|
103
|
+
#
|
104
|
+
# It's also possible to supply a block which will be called if no CSV export is requested:
|
105
|
+
#
|
106
|
+
# export_grid_if_requested do
|
107
|
+
# render(:action => 'index')
|
108
|
+
# end
|
109
|
+
|
110
|
+
def export_grid_if_requested(opts = {})
|
111
|
+
grid = self.wice_grid_instances.detect(&:output_csv?)
|
112
|
+
|
113
|
+
if grid
|
114
|
+
template_name = opts[grid.name] || opts[grid.name.intern]
|
115
|
+
template_name ||= grid.name + '_grid'
|
116
|
+
temp_filename = render_to_string(:partial => template_name)
|
117
|
+
temp_filename.strip!
|
118
|
+
filename = (grid.csv_file_name || grid.name ) + '.csv'
|
119
|
+
grid.csv_tempfile.close
|
120
|
+
send_file temp_filename, :filename => filename, :type => 'text/csv'
|
121
|
+
grid.csv_tempfile = nil
|
122
|
+
true
|
123
|
+
else
|
124
|
+
yield if block_given?
|
125
|
+
false
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# +wice_grid_custom_filter_params+ generates HTTP parameters understood by WiceGrid custom filters.
|
130
|
+
# Combined with Rails route helpers it allows to generate links leading to
|
131
|
+
# grids with pre-selected custom filters.
|
132
|
+
#
|
133
|
+
# Parameters:
|
134
|
+
# * <tt>:grid_name</tt> - The name of the grid. Just like parameter <tt>:name</tt> of
|
135
|
+
# <tt>initialize_grid</tt>, the parameter is optional, and when absent, the name
|
136
|
+
# <tt>'grid'</tt> is assumed
|
137
|
+
# * <tt>:attribute_name</tt> and <tt>:model_class</tt> - should be the same as <tt>:attribute_name</tt> and
|
138
|
+
# <tt>:model_class</tt> of the column declaration with the target custom filter.
|
139
|
+
# * <tt>:value</tt> - the value of the column filter.
|
140
|
+
def wice_grid_custom_filter_params(opts = {})
|
141
|
+
options = {:grid_name => 'grid',
|
142
|
+
:attribute_name => nil,
|
143
|
+
:model_class => nil,
|
144
|
+
:value => nil}
|
145
|
+
options.merge!(opts)
|
146
|
+
|
147
|
+
[:attribute_name, :value].each do |key|
|
148
|
+
raise ::Wice::WiceGridArgumentError.new("wice_grid_custom_filter_params: :#{key} is a mandatory argument") unless options[key]
|
149
|
+
end
|
150
|
+
|
151
|
+
attr_name = if options[:model_class]
|
152
|
+
unless options[:model_class].nil?
|
153
|
+
options[:model_class] = options[:model_class].constantize if options[:model_class].is_a? String
|
154
|
+
raise Wice::WiceGridArgumentError.new("Option :model_class can be either a class or a string instance") unless options[:model_class].is_a? Class
|
155
|
+
end
|
156
|
+
options[:model_class].table_name + '.' + options[:attribute_name]
|
157
|
+
else
|
158
|
+
options[:attribute_name]
|
159
|
+
end
|
160
|
+
|
161
|
+
{"#{options[:grid_name]}[f][#{attr_name}][]" => options[:value]}
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module WGHashExtensions #:nodoc:
|
3
|
+
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
# if there's a hash of hashes, the original structure and the
|
10
|
+
# returned structure should not contain any shared deep hashes
|
11
|
+
def deep_clone_yl #:nodoc:
|
12
|
+
cloned = self.clone
|
13
|
+
cloned.keys.each do |k|
|
14
|
+
if cloned[k].kind_of?(Hash)
|
15
|
+
cloned[k] = cloned[k].deep_clone_yl
|
16
|
+
end
|
17
|
+
end
|
18
|
+
cloned
|
19
|
+
end
|
20
|
+
|
21
|
+
# Used to modify options submitted to view helpers. If there is no :klass option,
|
22
|
+
# it will be added, if there is, the css class name will be appended to the existing
|
23
|
+
# class name(s)
|
24
|
+
def add_or_append_class_value!(klass_value, prepend = false) #:nodoc:
|
25
|
+
if self.has_key?('class')
|
26
|
+
self[:class] = self['class']
|
27
|
+
self.delete('class')
|
28
|
+
end
|
29
|
+
|
30
|
+
self[:class] = if self.has_key?(:class)
|
31
|
+
if prepend
|
32
|
+
"#{klass_value} #{self[:class]}"
|
33
|
+
else
|
34
|
+
"#{self[:class]} #{klass_value}"
|
35
|
+
end
|
36
|
+
else
|
37
|
+
klass_value
|
38
|
+
end
|
39
|
+
|
40
|
+
return self
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# Used to transform a traditional params hash
|
45
|
+
# into an array of two element arrays where element zero is a parameter name as it appears in HTTP requests,
|
46
|
+
# and the first element is the value:
|
47
|
+
# { :a => { :b => 3, :c => 4, :d => { :e => 5 }} }.parameter_names_and_values #=> [["a[d][e]", 5], ["a[b]", 3], ["a[c]", 4]]
|
48
|
+
# The parameter is an optional array of parameter names to prepend:
|
49
|
+
# { :a => { :b => 3, :c => 4, :d => { :e => 5 }} }.parameter_names_and_values(['foo', 'baz']) #=>
|
50
|
+
# [["foo[baz][a][d][e]", 5], ["foo[baz][a][b]", 3], ["foo[baz][a][c]", 4]]
|
51
|
+
def parameter_names_and_values(initial = []) #:nodoc:
|
52
|
+
res = []
|
53
|
+
recursively_gather_finite_non_hash_values_with_key_path(res, [])
|
54
|
+
res.collect do |parameter_struct|
|
55
|
+
parameter_struct[0] = initial + parameter_struct[0]
|
56
|
+
[parameter_struct[0].to_parameter_name, parameter_struct[1]]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# A deep merge of two hashes.
|
62
|
+
# That is, if both hashes have the same key and the values are hashes, these two hashes should also be merged.
|
63
|
+
# Used for merging two sets of params.
|
64
|
+
def rec_merge(other) #:nodoc:
|
65
|
+
res = self.clone
|
66
|
+
other.each do |key, other_value|
|
67
|
+
value = res[key]
|
68
|
+
if value.is_a?(Hash) && other_value.is_a?(Hash)
|
69
|
+
res[key] = value.rec_merge other_value
|
70
|
+
else
|
71
|
+
res[key] = other_value
|
72
|
+
end
|
73
|
+
end
|
74
|
+
res
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
module ClassMethods #:nodoc:
|
79
|
+
|
80
|
+
# Used mostly for submitting options to view helpers, that is, like this:
|
81
|
+
# content_tag(:th, col_link, Hash.make_hash(:class, css_class))
|
82
|
+
# In some it is important that if the value is empty, no option
|
83
|
+
# is submitted at all. Thus, there's a check for an empty value
|
84
|
+
def make_hash(key, value) #:nodoc:
|
85
|
+
value.blank? ? {} : {key => value}
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
protected
|
92
|
+
|
93
|
+
def recursively_gather_finite_non_hash_values_with_key_path(res, stack = []) #:nodoc:
|
94
|
+
self.each do |key, value|
|
95
|
+
if value.kind_of?(Hash)
|
96
|
+
value.recursively_gather_finite_non_hash_values_with_key_path(res, stack + [key])
|
97
|
+
else
|
98
|
+
res << [stack + [key], value]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
class Hash #:nodoc:
|
107
|
+
include WGHashExtensions
|
108
|
+
end
|
109
|
+
|
110
|
+
# tag_options is a Rails views private method that takes a hash op options for
|
111
|
+
# an HTM hash and produces a string ready to be added to the tag.
|
112
|
+
# Here we are changing its visibility in order to be able to use it.
|
113
|
+
module ActionView #:nodoc:
|
114
|
+
module Helpers #:nodoc:
|
115
|
+
module TagHelper #:nodoc:
|
116
|
+
public :tag_options
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
module Enumerable #:nodoc:
|
124
|
+
|
125
|
+
# Used to check the validity of :custom_filter parameter of column
|
126
|
+
def all_items_are_of_class(klass) #:nodoc:
|
127
|
+
return false if self.empty?
|
128
|
+
self.inject(true){|memo, o| (o.is_a? klass) && memo}
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
module WGObjectExtensions #:nodoc:
|
134
|
+
|
135
|
+
# takes a list of messages, sends message 1 to self, then message 2 is sent to the result of the first message, ans so on
|
136
|
+
# returns nil as soon as the current receiver does not respond to such a message
|
137
|
+
def deep_send(*messages) #:nodoc:
|
138
|
+
obj = self
|
139
|
+
messages.each do |message|
|
140
|
+
if obj.respond_to? message
|
141
|
+
obj = obj.send(message)
|
142
|
+
else
|
143
|
+
return nil
|
144
|
+
end
|
145
|
+
# return obj if obj.nil?
|
146
|
+
end
|
147
|
+
return obj
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class Object #:nodoc:
|
152
|
+
include WGObjectExtensions
|
153
|
+
end
|
154
|
+
|
155
|
+
module WGArrayExtensions #:nodoc:
|
156
|
+
# Only used by Hash#parameter_names_and_values
|
157
|
+
# Transforms ['foo', 'bar', 'baz'] to 'foo[bar][baz]'
|
158
|
+
def to_parameter_name #:nodoc:
|
159
|
+
self[0].to_s + (self[1..-1] || []).collect{|k| '[' + k.to_s + ']'}.join('')
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
class Array #:nodoc:
|
164
|
+
include WGArrayExtensions
|
165
|
+
end
|
166
|
+
|
167
|
+
module StringExt #:nodoc:
|
168
|
+
def html_safe_if_necessary #:nodoc:
|
169
|
+
if respond_to?(:html_safe)
|
170
|
+
html_safe
|
171
|
+
else
|
172
|
+
self
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
class String #:nodoc:
|
178
|
+
include StringExt
|
179
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Wice
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
@@model_validated = false
|
7
|
+
|
8
|
+
# checks whether the class is a valid storage for saved queries
|
9
|
+
def validate_query_model(query_store_model) #:nodoc:
|
10
|
+
unless query_store_model.respond_to?(:list)
|
11
|
+
raise ::Wice::WiceGridArgumentError.new("Model for saving queries #{query_store_model.class.name} is invalid - there is no class method #list defined")
|
12
|
+
end
|
13
|
+
arit = query_store_model.method(:list).arity
|
14
|
+
unless arit == 2
|
15
|
+
raise ::Wice::WiceGridArgumentError.new("Method list in the model for saving queries #{query_store_model.class.name} has wrong arity - it should be 2 instead of #{arit}")
|
16
|
+
end
|
17
|
+
@@model_validated = true
|
18
|
+
end
|
19
|
+
|
20
|
+
# Retrieves and constantizes (if needed ) the Query Store model
|
21
|
+
def get_query_store_model #:nodoc:
|
22
|
+
|
23
|
+
query_store_model = ::Wice::Defaults::QUERY_STORE_MODEL
|
24
|
+
query_store_model = query_store_model.constantize if query_store_model.is_a? String
|
25
|
+
raise ::Wice::WiceGridArgumentError.new("Defaults::QUERY_STORE_MODEL must be an ActiveRecord class or a string which can be constantized to an ActiveRecord class") unless query_store_model.kind_of? Class
|
26
|
+
validate_query_model(query_store_model) unless @@model_validated
|
27
|
+
query_store_model
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_string_matching_operators(model) #:nodoc:
|
31
|
+
if defined?(Wice::Defaults::STRING_MATCHING_OPERATORS) && Wice::Defaults::STRING_MATCHING_OPERATORS.is_a?(Hash) &&
|
32
|
+
str_matching_operator = Wice::Defaults::STRING_MATCHING_OPERATORS[model.connection.class.to_s]
|
33
|
+
str_matching_operator
|
34
|
+
else
|
35
|
+
Wice::Defaults::STRING_MATCHING_OPERATOR
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def deprecated_call(old_name, new_name, opts) #:nodoc:
|
40
|
+
if opts[old_name] && ! opts[new_name]
|
41
|
+
opts[new_name] = opts[old_name]
|
42
|
+
opts.delete(old_name)
|
43
|
+
STDERR.puts "WiceGrid: Parameter :#{old_name} is deprecated, use :#{new_name} instead!"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def log(message) #:nodoc:
|
48
|
+
ActiveRecord::Base.logger.info('WiceGrid: ' + message)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# The point of this module is to provide a thin layer between
|
54
|
+
# Rails Internationalization API and WiceGrid, enabling a fallback
|
55
|
+
# to the old hardcoded messages if no translations are available
|
56
|
+
# or I18n is not present (Rails 2.1.0 and older).
|
57
|
+
module WiceGridNlMessageProvider #:nodoc:
|
58
|
+
class << self
|
59
|
+
|
60
|
+
def get_from_hardcoded_constants(key) #:nodoc:
|
61
|
+
if Wice::Defaults.const_defined?(key)
|
62
|
+
Wice::Defaults.const_get(key)
|
63
|
+
else
|
64
|
+
return "message for key #{key} not found!"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if Object.const_defined?(:I18n) # Rails with :I18n
|
69
|
+
|
70
|
+
def get_message(key) #:nodoc:
|
71
|
+
translated = I18n.t(key.to_s.downcase, :scope => 'wice_grid', :default => '_')
|
72
|
+
if translated == '_'
|
73
|
+
get_from_hardcoded_constants(key)
|
74
|
+
else
|
75
|
+
translated
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
else # Rails without :I18n
|
80
|
+
alias_method :get_message, :get_from_hardcoded_constants
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module Defaults #:nodoc:
|
86
|
+
end
|
87
|
+
|
88
|
+
module ExceptionsMixin #:nodoc:
|
89
|
+
def initialize(str) #:nodoc:
|
90
|
+
super("WiceGrid: " + str)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
class WiceGridArgumentError < ArgumentError #:nodoc:
|
94
|
+
include ExceptionsMixin
|
95
|
+
end
|
96
|
+
class WiceGridException < Exception #:nodoc:
|
97
|
+
include ExceptionsMixin
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Wice
|
3
|
+
class <<self
|
4
|
+
# Used in routes.rb to define routes to the query processing controller.
|
5
|
+
# Parameters:
|
6
|
+
# * map - the mapper object used in routes.rb to defined routes (instance of <tt>ActionController::Routing::RouteSet::Mapper</tt>)
|
7
|
+
# * controller - name of the query processing controller, i.e. <tt>'queries'</tt> if the controller is +QueriesController+ .
|
8
|
+
# Read section "Saving Queries How-To" in README for more details.
|
9
|
+
def define_routes(map, controller)
|
10
|
+
controller = controller.to_s
|
11
|
+
map.create_serialized_query '/wice_grid_serialized_queries/:grid_name',
|
12
|
+
:controller => controller,
|
13
|
+
:action => 'create',
|
14
|
+
:conditions => {:method => :post}
|
15
|
+
map.delete_serialized_query '/wice_grid_serialized_query/:grid_name/:id',
|
16
|
+
:controller => controller,
|
17
|
+
:action => 'delete',
|
18
|
+
:conditions => {:method => :post}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module SerializedQueriesControllerMixin #:nodoc:
|
23
|
+
|
24
|
+
def delete #:nodoc:
|
25
|
+
init
|
26
|
+
if sq = @query_store_model.find_by_id_and_grid_name(params[:id], @grid_name)
|
27
|
+
if sq.destroy
|
28
|
+
if params[:current]
|
29
|
+
@current = @query_store_model.find_by_id_and_grid_name(params[:current], @grid_name)
|
30
|
+
end
|
31
|
+
@notification_messages = WiceGridNlMessageProvider.get_message(:QUERY_DELETED_MESSAGE)
|
32
|
+
else
|
33
|
+
@error_messages = sq.errors.full_raw_messages.join(' ')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
render :file => "#{RAILS_ROOT}/vendor/plugins/wice_grid/lib/views/delete.rjs"
|
37
|
+
end
|
38
|
+
|
39
|
+
def create #:nodoc:
|
40
|
+
init
|
41
|
+
query_params = if params[@grid_name]
|
42
|
+
params[@grid_name]
|
43
|
+
else
|
44
|
+
{}
|
45
|
+
end
|
46
|
+
query_params.delete(:page)
|
47
|
+
|
48
|
+
@saved_query = @query_store_model.new(:grid_name => @grid_name, :name => params[:query_name], :query => query_params)
|
49
|
+
|
50
|
+
@saved_query.attributes = params[:extra] unless params[:extra].blank?
|
51
|
+
|
52
|
+
if @saved_query.save
|
53
|
+
@grid_title_id = "#{@grid_name}_title"
|
54
|
+
@notification_messages = WiceGridNlMessageProvider.get_message(:QUERY_SAVED_MESSAGE)
|
55
|
+
else
|
56
|
+
@error_messages = @saved_query.errors.map{ |_, msg| msg }.join(' ')
|
57
|
+
end
|
58
|
+
|
59
|
+
render :file => "#{RAILS_ROOT}/vendor/plugins/wice_grid/lib/views/create.rjs"
|
60
|
+
end
|
61
|
+
|
62
|
+
def extra
|
63
|
+
params[:extra]
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def init #:nodoc:
|
69
|
+
@query_store_model = ::Wice::get_query_store_model
|
70
|
+
|
71
|
+
@grid_name = params[:grid_name]
|
72
|
+
@notification_messages_dom_id = "#{@grid_name}_notification_messages"
|
73
|
+
@query_list_dom_id = "#{@grid_name}_query_list"
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|