wice_grid_ms 3.6.0
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.inch.yml +3 -0
- data/.rspec +3 -0
- data/.rubocop.yml +181 -0
- data/.travis.yml +22 -0
- data/CHANGELOG.md +714 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +1518 -0
- data/Rakefile +59 -0
- data/SAVED_QUERIES_HOWTO.md +113 -0
- data/TODO.md +16 -0
- data/app/views/kaminari/wice_grid/_gap.html.erb +1 -0
- data/app/views/kaminari/wice_grid/_next_page.html.erb +1 -0
- data/app/views/kaminari/wice_grid/_page.html.erb +1 -0
- data/app/views/kaminari/wice_grid/_paginator.html.erb +19 -0
- data/app/views/kaminari/wice_grid/_prev_page.html.erb +1 -0
- data/config/locales/cz.yml +45 -0
- data/config/locales/de.yml +47 -0
- data/config/locales/en.yml +47 -0
- data/config/locales/es.yml +47 -0
- data/config/locales/fr.yml +45 -0
- data/config/locales/is.yml +46 -0
- data/config/locales/it.yml +38 -0
- data/config/locales/ja.yml +47 -0
- data/config/locales/nl.yml +45 -0
- data/config/locales/pt-BR.yml +36 -0
- data/config/locales/pt.yml +45 -0
- data/config/locales/ru.yml +45 -0
- data/config/locales/sk.yml +45 -0
- data/config/locales/uk.yml +45 -0
- data/config/locales/zh.yml +45 -0
- data/lib/generators/wice_grid/add_migration_for_serialized_queries_generator.rb +20 -0
- data/lib/generators/wice_grid/install_generator.rb +14 -0
- data/lib/generators/wice_grid/templates/create_wice_grid_serialized_queries.rb +14 -0
- data/lib/generators/wice_grid/templates/wice_grid_config.rb +192 -0
- data/lib/wice/active_record_column_wrapper.rb +123 -0
- data/lib/wice/columns.rb +276 -0
- data/lib/wice/columns/column_action.rb +52 -0
- data/lib/wice/columns/column_boolean.rb +40 -0
- data/lib/wice/columns/column_bootstrap_datepicker.rb +48 -0
- data/lib/wice/columns/column_custom_dropdown.rb +115 -0
- data/lib/wice/columns/column_float.rb +9 -0
- data/lib/wice/columns/column_html5_datepicker.rb +31 -0
- data/lib/wice/columns/column_integer.rb +78 -0
- data/lib/wice/columns/column_jquery_datepicker.rb +49 -0
- data/lib/wice/columns/column_processor_index.rb +23 -0
- data/lib/wice/columns/column_rails_date_helper.rb +41 -0
- data/lib/wice/columns/column_rails_datetime_helper.rb +40 -0
- data/lib/wice/columns/column_range.rb +72 -0
- data/lib/wice/columns/column_string.rb +92 -0
- data/lib/wice/columns/common_date_datetime_mixin.rb +20 -0
- data/lib/wice/columns/common_js_date_datetime_conditions_generator_mixin.rb +42 -0
- data/lib/wice/columns/common_js_date_datetime_mixin.rb +15 -0
- data/lib/wice/columns/common_rails_date_datetime_conditions_generator_mixin.rb +26 -0
- data/lib/wice/columns/common_standard_helper_date_datetime_mixin.rb +22 -0
- data/lib/wice/grid_output_buffer.rb +49 -0
- data/lib/wice/grid_renderer.rb +609 -0
- data/lib/wice/helpers/bs_calendar_helpers.rb +66 -0
- data/lib/wice/helpers/js_calendar_helpers.rb +83 -0
- data/lib/wice/helpers/wice_grid_misc_view_helpers.rb +75 -0
- data/lib/wice/helpers/wice_grid_serialized_queries_view_helpers.rb +95 -0
- data/lib/wice/helpers/wice_grid_view_helpers.rb +718 -0
- data/lib/wice/kaminari_monkey_patching.rb +14 -0
- data/lib/wice/table_column_matrix.rb +65 -0
- data/lib/wice/wice_grid_controller.rb +223 -0
- data/lib/wice/wice_grid_core_ext.rb +142 -0
- data/lib/wice/wice_grid_misc.rb +209 -0
- data/lib/wice/wice_grid_serialized_queries_controller.rb +87 -0
- data/lib/wice/wice_grid_serialized_query.rb +14 -0
- data/lib/wice/wice_grid_spreadsheet.rb +20 -0
- data/lib/wice_grid.rb +676 -0
- data/release_notes/RELEASE_NOTES_3.2.pre1.rdoc +81 -0
- data/release_notes/RELEASE_NOTES_3.2.pre2.rdoc +6 -0
- data/release_notes/RELEASE_NOTES_3.3.0.rdoc +21 -0
- data/spec/schema.rb +9 -0
- data/spec/spec_helper.rb +75 -0
- data/spec/support/active_record.rb +11 -0
- data/spec/support/wice_grid_test_config.rb +175 -0
- data/spec/wice/grid_output_buffer_spec.rb +41 -0
- data/spec/wice/table_column_matrix_spec.rb +38 -0
- data/spec/wice/wice_grid_misc_spec.rb +159 -0
- data/spec/wice/wice_grid_spreadsheet_spec.rb +14 -0
- data/test/readme.txt +1 -0
- data/vendor/assets/javascripts/wice_grid.js +3 -0
- data/vendor/assets/javascripts/wice_grid_init.js.coffee +339 -0
- data/vendor/assets/javascripts/wice_grid_processor.js.coffee +133 -0
- data/vendor/assets/javascripts/wice_grid_saved_queries_init.js.coffee +103 -0
- data/vendor/assets/stylesheets/wice_grid.scss +81 -0
- data/wice_grid.gemspec +36 -0
- metadata +335 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Ugly monkey-patching Kaminari (https://github.com/amatsuda/kaminari/pull/267)
|
4
|
+
module Kaminari #:nodoc:
|
5
|
+
module Helpers #:nodoc:
|
6
|
+
class Tag #:nodoc:
|
7
|
+
def page_url_for(page) #:nodoc:
|
8
|
+
current_page_params_as_query_string = @param_name.to_s + '=' + (page <= 1 ? nil : page).to_s
|
9
|
+
current_page_params_as_hash = Rack::Utils.parse_nested_query(current_page_params_as_query_string)
|
10
|
+
@template.url_for Wice::WgHash.rec_merge(@params, current_page_params_as_hash).symbolize_keys
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Wice
|
3
|
+
|
4
|
+
# a matrix for all declared columns
|
5
|
+
class TableColumnMatrix < Hash #:nodoc:
|
6
|
+
|
7
|
+
# a structure to hold generates Arels for all column filters
|
8
|
+
attr_reader :generated_conditions
|
9
|
+
|
10
|
+
# init a matrix of all columns
|
11
|
+
def initialize #:nodoc:
|
12
|
+
super
|
13
|
+
@generated_conditions = []
|
14
|
+
@by_table_names = HashWithIndifferentAccess.new
|
15
|
+
end
|
16
|
+
|
17
|
+
# add an Arel for a column
|
18
|
+
def add_condition(column, conditions)
|
19
|
+
@generated_conditions << [column, conditions] unless conditions.blank?
|
20
|
+
end
|
21
|
+
|
22
|
+
# returns a list of all Arels
|
23
|
+
def conditions
|
24
|
+
@generated_conditions.collect { |_, cond| cond }
|
25
|
+
end
|
26
|
+
|
27
|
+
# returns Arels for one model
|
28
|
+
alias_method :get, :[]
|
29
|
+
|
30
|
+
# returns the main ActiveRecord model class
|
31
|
+
attr_reader :default_model_class
|
32
|
+
|
33
|
+
# sets the main ActiveRecord model class
|
34
|
+
def default_model_class=(model) #:nodoc:
|
35
|
+
init_columns_of_table(model) unless key?(model)
|
36
|
+
@default_model_class = model
|
37
|
+
end
|
38
|
+
|
39
|
+
# returns Arels for one model
|
40
|
+
def [](model) #:nodoc:
|
41
|
+
init_columns_of_table(model) unless key?(model)
|
42
|
+
get(model)
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_column_by_model_class_and_column_name(model_class, column_name) #:nodoc:
|
46
|
+
self[model_class][column_name]
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_column_in_default_model_class_by_column_name(column_name) #:nodoc:
|
50
|
+
if @default_model_class.nil?
|
51
|
+
fail WiceGridException.new("Cannot search for this column(#{column_name}) in a default model(#{@default_model_class}) as the default model is not set")
|
52
|
+
end
|
53
|
+
|
54
|
+
self[@default_model_class][column_name]
|
55
|
+
end
|
56
|
+
|
57
|
+
def init_columns_of_table(model) #:nodoc:
|
58
|
+
self[model] = HashWithIndifferentAccess.new(model.columns.index_by(&:name))
|
59
|
+
@by_table_names[model.table_name] = self[model]
|
60
|
+
self[model].each_value { |c| c.model = model }
|
61
|
+
end
|
62
|
+
|
63
|
+
alias_method :<<, :init_columns_of_table
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Wice
|
3
|
+
module Controller #:nodoc:
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods #:nodoc:
|
9
|
+
# Used to add query processing action methods into a controller.
|
10
|
+
# Read section "Saving Queries How-To" in README for more details.
|
11
|
+
def save_wice_grid_queries
|
12
|
+
include Wice::SerializedQueriesControllerMixin
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
attr_accessor :wice_grid_instances #:nodoc:
|
19
|
+
|
20
|
+
# 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 -
|
21
|
+
# if in other parameters a column name is mentioned without the name of the table, this table is implied.
|
22
|
+
# Just like in an ordinary ActiveRecord <tt>find</tt> you can use <tt>:joins</tt>, <tt>:include</tt>, and <tt>:conditions</tt>.
|
23
|
+
#
|
24
|
+
# The first parameter is an ActiveRecord class name or an ActiveRecord::Relation instance. The generated ActiveRecord call will use it as the
|
25
|
+
# receiver of the <tt>paginate</tt> method: <tt>klass.paginate(...)</tt>
|
26
|
+
#
|
27
|
+
# The second parameters is a hash of parameters:
|
28
|
+
# * <tt>:joins</tt> - ActiveRecord <tt>:joins</tt> option.
|
29
|
+
# * <tt>:include</tt> - ActiveRecord <tt>:include</tt> option.
|
30
|
+
# The value of `:include` can be an array of association names `include: [:category, :users, :status]`,
|
31
|
+
# If you need to join tables to joined tables, use hashes: `include: [:category, {users: :group}, :status]`
|
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>:csv_field_separator</tt> - field separator for CSV files. The default is defined in +CSV_FIELD_SEPARATOR+ in the config file.
|
44
|
+
# * <tt>:custom_order</tt> - used for overriding the ORDER BY clause with custom sql code (for example, including a function).
|
45
|
+
# The value of the parameter is a hash where keys are fully qualified names
|
46
|
+
# of database columns, and values the required chunks of SQL to use in the ORDER BY clause, either as strings or Proc object
|
47
|
+
# evaluating to string. See section 'Custom Ordering' in the README.
|
48
|
+
# * <tt>:saved_query</tt> - id of the saved query or the query object itself to load initially.
|
49
|
+
# Read section "Saving Queries How-To" in README for more details.
|
50
|
+
# * <tt>:after</tt> - defined a name of a controller method which would be called by the grid after all user input has been processed,
|
51
|
+
# with a single parameter which is a Proc object. Once called, the object returns a list of all records of the current selection
|
52
|
+
# throughout all pages. See section "Integration With The Application" in the README.
|
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>, and <tt>:name</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
|
+
def initialize_grid(klass, opts = {})
|
68
|
+
wg = WiceGrid.new(klass, self, opts)
|
69
|
+
self.wice_grid_instances = [] if self.wice_grid_instances.nil?
|
70
|
+
self.wice_grid_instances << wg
|
71
|
+
wg
|
72
|
+
end
|
73
|
+
|
74
|
+
# +export_grid_if_requested+ is a controller method which should be called at the end of each action containing grids with enabled
|
75
|
+
# CSV export.
|
76
|
+
#
|
77
|
+
# CSV export will only work if each WiceGrid helper is placed in a partial of its own (requiring it from the master template
|
78
|
+
# of course for the usual flow).
|
79
|
+
# +export_grid_if_requested+ intercepts CSV export requests and evaluates the corresponding partial with the required grid helper.
|
80
|
+
# By default for each grid +export_grid_if_requested+ will look for a partial
|
81
|
+
# whose name follows the following pattern:
|
82
|
+
#
|
83
|
+
# _GRID_NAME_grid.html.erb
|
84
|
+
#
|
85
|
+
# For example, a grid named +orders+ is supposed to be found in a template called <tt>_orders_grid.html.erb</tt>,
|
86
|
+
# Remember that the default name of grids is +grid+.
|
87
|
+
#
|
88
|
+
# This convention can be easily overridden by supplying a hash parameter to +export_grid_if_requested+ where each key is the name of
|
89
|
+
# a grid, and the value is the name of the template (like it is specified for +render+, i.e. without '_' and extensions):
|
90
|
+
#
|
91
|
+
# export_grid_if_requested('grid' => 'orders', 'grid2' => 'invoices')
|
92
|
+
#
|
93
|
+
# If the request is not a CSV export request, the method does nothing and returns +false+, if it is a CSV export request,
|
94
|
+
# the method returns +true+.
|
95
|
+
#
|
96
|
+
# 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,
|
97
|
+
# to avoid double rendering, use the return value of the method to conditionally call your +render+ :
|
98
|
+
#
|
99
|
+
# export_grid_if_requested || render(action: 'index')
|
100
|
+
#
|
101
|
+
# It's also possible to supply a block which will be called if no CSV export is requested:
|
102
|
+
#
|
103
|
+
# export_grid_if_requested do
|
104
|
+
# render(action: 'index')
|
105
|
+
# end
|
106
|
+
|
107
|
+
def export_grid_if_requested(opts = {})
|
108
|
+
grid = self.wice_grid_instances.detect(&:output_csv?)
|
109
|
+
|
110
|
+
if grid
|
111
|
+
template_name = opts[grid.name] || opts[grid.name.intern]
|
112
|
+
template_name ||= grid.name + '_grid'
|
113
|
+
temp_filename = render_to_string(partial: template_name)
|
114
|
+
temp_filename = temp_filename.strip
|
115
|
+
filename = (grid.csv_file_name || grid.name) + '.csv'
|
116
|
+
grid.csv_tempfile.close
|
117
|
+
send_file_rails2 temp_filename, filename: filename, type: "text/csv; charset=#{get_output_encoding grid.csv_encoding}"
|
118
|
+
grid.csv_tempfile = nil
|
119
|
+
true
|
120
|
+
else
|
121
|
+
yield if block_given?
|
122
|
+
false
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# +wice_grid_custom_filter_params+ generates HTTP parameters understood by WiceGrid custom filters.
|
127
|
+
# Combined with Rails route helpers it allows to generate links leading to
|
128
|
+
# grids with pre-selected custom filters.
|
129
|
+
#
|
130
|
+
# Parameters:
|
131
|
+
# * <tt>:grid_name</tt> - The name of the grid. Just like parameter <tt>:name</tt> of
|
132
|
+
# <tt>initialize_grid</tt>, the parameter is optional, and when absent, the name
|
133
|
+
# <tt>'grid'</tt> is assumed
|
134
|
+
# * <tt>:attribute</tt> and <tt>:model</tt> - should be the same as <tt>:attribute</tt> and
|
135
|
+
# <tt>:model</tt> of the column declaration with the target custom filter.
|
136
|
+
# * <tt>:value</tt> - the value of the column filter.
|
137
|
+
def wice_grid_custom_filter_params(opts = {})
|
138
|
+
options = {
|
139
|
+
grid_name: 'grid',
|
140
|
+
attribute: nil,
|
141
|
+
model: nil,
|
142
|
+
value: nil
|
143
|
+
}
|
144
|
+
options.merge!(opts)
|
145
|
+
|
146
|
+
[:attribute, :value].each do |key|
|
147
|
+
fail ::Wice::WiceGridArgumentError.new("wice_grid_custom_filter_params: :#{key} is a mandatory argument") unless options[key]
|
148
|
+
end
|
149
|
+
|
150
|
+
attr_name = if options[:model]
|
151
|
+
unless options[:model].nil?
|
152
|
+
options[:model] = options[:model].constantize if options[:model].is_a? String
|
153
|
+
fail Wice::WiceGridArgumentError.new('Option :model can be either a class or a string instance') unless options[:model].is_a? Class
|
154
|
+
end
|
155
|
+
options[:model].table_name + '.' + options[:attribute]
|
156
|
+
else
|
157
|
+
options[:attribute]
|
158
|
+
end
|
159
|
+
|
160
|
+
{ "#{options[:grid_name]}[f][#{attr_name}][]" => options[:value] }
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def get_output_encoding(csv_encoding)
|
166
|
+
if csv_encoding.blank?
|
167
|
+
'utf-8'
|
168
|
+
else
|
169
|
+
csv_encoding.split(':').first
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def send_file_rails2(path, options = {}) #:nodoc:
|
174
|
+
fail "Cannot read file #{path}" unless File.file?(path) && File.readable?(path)
|
175
|
+
|
176
|
+
options[:length] ||= File.size(path)
|
177
|
+
options[:filename] ||= File.basename(path) unless options[:url_based_filename]
|
178
|
+
send_file_headers_rails2! options
|
179
|
+
|
180
|
+
@performed_render = false
|
181
|
+
|
182
|
+
logger.info "Sending file #{path}" unless logger.nil?
|
183
|
+
File.open(path, 'rb') { |file| render status: options[:status], text: file.read }
|
184
|
+
end
|
185
|
+
|
186
|
+
DEFAULT_SEND_FILE_OPTIONS_RAILS2 = { #:nodoc:
|
187
|
+
type: 'application/octet-stream'.freeze,
|
188
|
+
disposition: 'attachment'.freeze,
|
189
|
+
stream: true,
|
190
|
+
buffer_size: 4096,
|
191
|
+
x_sendfile: false
|
192
|
+
}.freeze
|
193
|
+
|
194
|
+
def send_file_headers_rails2!(options) #:nodoc:
|
195
|
+
options.update(DEFAULT_SEND_FILE_OPTIONS_RAILS2.merge(options))
|
196
|
+
[:length, :type, :disposition].each do |arg|
|
197
|
+
fail ArgumentError, ":#{arg} option required" if options[arg].nil?
|
198
|
+
end
|
199
|
+
|
200
|
+
disposition = options[:disposition].dup || 'attachment'
|
201
|
+
|
202
|
+
disposition <<= %(; filename="#{options[:filename]}") if options[:filename]
|
203
|
+
|
204
|
+
content_type = options[:type]
|
205
|
+
content_type = content_type.to_s.strip # fixes a problem with extra '\r' with some browsers
|
206
|
+
|
207
|
+
headers.merge!(
|
208
|
+
'Content-Length' => options[:length].to_s,
|
209
|
+
'Content-Type' => content_type,
|
210
|
+
'Content-Disposition' => disposition,
|
211
|
+
'Content-Transfer-Encoding' => 'binary'
|
212
|
+
)
|
213
|
+
|
214
|
+
# Fix a problem with IE 6.0 on opening downloaded files:
|
215
|
+
# If Cache-Control: no-cache is set (which Rails does by default),
|
216
|
+
# IE removes the file it just downloaded from its cache immediately
|
217
|
+
# after it displays the "open/save" dialog, which means that if you
|
218
|
+
# hit "open" the file isn't there anymore when the application that
|
219
|
+
# is called for handling the download is run, so let's workaround that
|
220
|
+
headers['Cache-Control'] = 'private' if headers['Cache-Control'] == 'no-cache'
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Wice
|
3
|
+
module WgHash #:nodoc:
|
4
|
+
class << self #:nodoc:
|
5
|
+
# if there's a hash of hashes, the original structure and the
|
6
|
+
# returned structure should not contain any shared deep hashes
|
7
|
+
def deep_clone(hash) #:nodoc:
|
8
|
+
cloned = hash.clone
|
9
|
+
cloned.keys.each do |k|
|
10
|
+
if cloned[k].is_a?(Hash)
|
11
|
+
cloned[k] = Wice::WgHash.deep_clone cloned[k]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
cloned
|
15
|
+
end
|
16
|
+
|
17
|
+
# Used to modify options submitted to view helpers. If there is no :klass option,
|
18
|
+
# it will be added, if there is, the css class name will be appended to the existing
|
19
|
+
# class name(s)
|
20
|
+
def add_or_append_class_value!(hash, klass_value, prepend = false) #:nodoc:
|
21
|
+
if hash.key?('class')
|
22
|
+
hash[:class] = hash['class']
|
23
|
+
hash.delete('class')
|
24
|
+
end
|
25
|
+
|
26
|
+
hash[:class] = if hash.key?(:class)
|
27
|
+
if prepend
|
28
|
+
"#{klass_value} #{hash[:class]}"
|
29
|
+
else
|
30
|
+
"#{hash[:class]} #{klass_value}"
|
31
|
+
end
|
32
|
+
else
|
33
|
+
klass_value
|
34
|
+
end
|
35
|
+
|
36
|
+
hash
|
37
|
+
end
|
38
|
+
|
39
|
+
# Used mostly for submitting options to view helpers, that is, like this:
|
40
|
+
# content_tag(:th, col_link, Wice::WgHash.make_hash(:class, css_class))
|
41
|
+
# In some it is important that if the value is empty, no option
|
42
|
+
# is submitted at all. Thus, there's a check for an empty value
|
43
|
+
def make_hash(key, value) #:nodoc:
|
44
|
+
value.blank? ? {} : { key => value }
|
45
|
+
end
|
46
|
+
|
47
|
+
# A deep merge of two hashes.
|
48
|
+
# That is, if both hashes have the same key and the values are hashes, these two hashes should also be merged.
|
49
|
+
# Used for merging two sets of params.
|
50
|
+
def rec_merge(hash, other) #:nodoc:
|
51
|
+
res = hash.clone
|
52
|
+
other.each do |key, other_value|
|
53
|
+
value = res[key]
|
54
|
+
if value.is_a?(Hash) && other_value.is_a?(Hash)
|
55
|
+
res[key] = rec_merge value, other_value
|
56
|
+
else
|
57
|
+
res[key] = other_value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
res
|
61
|
+
end
|
62
|
+
|
63
|
+
# Used to transform a traditional params hash
|
64
|
+
# into an array of two element arrays where element zero is a parameter name as it appears in HTTP requests,
|
65
|
+
# and the first element is the value:
|
66
|
+
# { a: { b: 3, c: 4, d: { e: 5 }} }.parameter_names_and_values #=> [["a[d][e]", 5], ["a[b]", 3], ["a[c]", 4]]
|
67
|
+
# The parameter is an optional array of parameter names to prepend:
|
68
|
+
# { a: { b: 3, c: 4, d: { e: 5 }} }.parameter_names_and_values(['foo', 'baz']) #=>
|
69
|
+
# [["foo[baz][a][d][e]", 5], ["foo[baz][a][b]", 3], ["foo[baz][a][c]", 4]]
|
70
|
+
def parameter_names_and_values(hash, initial = []) #:nodoc:
|
71
|
+
res = []
|
72
|
+
recursively_gather_finite_non_hash_values_with_key_path(hash, res, [])
|
73
|
+
res.collect do |parameter_struct|
|
74
|
+
parameter_struct[0] = initial + parameter_struct[0]
|
75
|
+
[Wice::WgArray.to_parameter_name(parameter_struct[0]), parameter_struct[1]]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def recursively_gather_finite_non_hash_values_with_key_path(hash, res, stack = []) #:nodoc:
|
82
|
+
hash.each do |key, value|
|
83
|
+
if value.is_a?(Hash)
|
84
|
+
recursively_gather_finite_non_hash_values_with_key_path(value, res, stack + [key])
|
85
|
+
else
|
86
|
+
res << [stack + [key], value]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
module WgEnumerable #:nodoc:
|
94
|
+
# Used to check the validity of :custom_filter parameter of column
|
95
|
+
def self.all_items_are_of_class(enumerable, klass) #:nodoc:
|
96
|
+
return false if enumerable.empty?
|
97
|
+
enumerable.inject(true) { |memo, o| (o.is_a? klass) && memo }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
module WgArray #:nodoc:
|
102
|
+
# Only used by Hash#parameter_names_and_values
|
103
|
+
# Transforms ['foo', 'bar', 'baz'] to 'foo[bar][baz]'
|
104
|
+
def self.to_parameter_name(array) #:nodoc:
|
105
|
+
array[0].to_s + (array[1..-1] || []).collect { |k| '[' + k.to_s + ']' }.join('')
|
106
|
+
end
|
107
|
+
end
|
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
|
+
def public_tag_options(options, escape = true) #:nodoc:
|
117
|
+
tag_options(options, escape)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
module WGObjectExtensions #:nodoc:
|
124
|
+
# 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
|
125
|
+
# returns nil as soon as the current receiver does not respond to such a message
|
126
|
+
def deep_send(*messages) #:nodoc:
|
127
|
+
obj = self
|
128
|
+
messages.each do |message|
|
129
|
+
if obj.respond_to? message
|
130
|
+
obj = obj.send(message)
|
131
|
+
else
|
132
|
+
return nil
|
133
|
+
end
|
134
|
+
# return obj if obj.nil?
|
135
|
+
end
|
136
|
+
obj
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
class Object #:nodoc:
|
141
|
+
include WGObjectExtensions
|
142
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Wice
|
3
|
+
class << self
|
4
|
+
|
5
|
+
# a flag storing whether the saved query class is a valid storage for saved queries
|
6
|
+
@@model_validated = false
|
7
|
+
|
8
|
+
def assoc_list_to_hash(assocs) #:nodoc:
|
9
|
+
head = assocs[0]
|
10
|
+
tail = assocs[1..-1]
|
11
|
+
|
12
|
+
if tail.blank?
|
13
|
+
head
|
14
|
+
elsif tail.size == 1
|
15
|
+
{head => tail[0]}
|
16
|
+
else
|
17
|
+
{head => assoc_list_to_hash(tail)}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_includes(existing_includes, new_assocs) #:nodoc:
|
22
|
+
new_includes = if new_assocs.blank?
|
23
|
+
existing_includes
|
24
|
+
else
|
25
|
+
existing_includes = if existing_includes.is_a?(Symbol)
|
26
|
+
[existing_includes]
|
27
|
+
elsif existing_includes.nil?
|
28
|
+
[]
|
29
|
+
else
|
30
|
+
existing_includes
|
31
|
+
end
|
32
|
+
|
33
|
+
assocs_as_hash = assoc_list_to_hash(new_assocs)
|
34
|
+
build_includes_int(existing_includes, assocs_as_hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
if new_includes.is_a?(Array) && new_includes.size == 1
|
38
|
+
new_includes[0]
|
39
|
+
else
|
40
|
+
new_includes
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def build_includes_int(includes, assocs) #:nodoc:
|
45
|
+
if includes.is_a?(Array)
|
46
|
+
build_includes_includes_is_array(includes, assocs)
|
47
|
+
elsif includes.is_a?(Hash)
|
48
|
+
build_includes_includes_is_hash(includes, assocs)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# TODO: refactor
|
53
|
+
def build_includes_includes_is_hash(includes, assocs) #:nodoc:
|
54
|
+
|
55
|
+
includes_key = includes.keys[0]
|
56
|
+
includes_value = includes.values[0]
|
57
|
+
|
58
|
+
if assocs.is_a?(Hash)
|
59
|
+
assocs_key = assocs.keys[0]
|
60
|
+
assocs_value = assocs.values[0]
|
61
|
+
|
62
|
+
if includes_value.is_a?(Symbol) && includes_value == assocs_key
|
63
|
+
{includes_key => assocs}
|
64
|
+
elsif includes_value.is_a?(Hash)
|
65
|
+
if includes_value.keys[0] == assocs_key
|
66
|
+
if includes_value.values[0] == assocs_value
|
67
|
+
{includes_key => assocs}
|
68
|
+
else
|
69
|
+
{includes_key => [includes_value.values[0], assocs_value]}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
elsif includes_value == assocs
|
74
|
+
{includes_key => assocs}
|
75
|
+
else
|
76
|
+
includes
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def build_includes_includes_is_array(includes, assocs) #:nodoc:
|
81
|
+
|
82
|
+
hash_keys = Hash[
|
83
|
+
*(
|
84
|
+
includes
|
85
|
+
.each_with_index
|
86
|
+
.to_a
|
87
|
+
.select{ |e, _idx| e.is_a?(Hash)}
|
88
|
+
.map{ |hash, idx| [ hash.keys[0], idx ] }
|
89
|
+
.flatten
|
90
|
+
)
|
91
|
+
]
|
92
|
+
|
93
|
+
key_to_search, finished = if assocs.is_a?(Hash)
|
94
|
+
[assocs.keys[0], false]
|
95
|
+
else
|
96
|
+
[assocs, true]
|
97
|
+
end
|
98
|
+
|
99
|
+
if idx = includes.index(key_to_search)
|
100
|
+
if finished # [:a, :b, :c] vs :a
|
101
|
+
includes
|
102
|
+
else # [:a, :b, :c] vs {:a => x}
|
103
|
+
includes[idx] = assocs
|
104
|
+
includes
|
105
|
+
end
|
106
|
+
|
107
|
+
elsif hash_keys.key?(key_to_search)
|
108
|
+
if finished # [{a: :x}, :b, :c, :d, :e] vs :a
|
109
|
+
includes
|
110
|
+
else
|
111
|
+
hash_idx = hash_keys[key_to_search]
|
112
|
+
assocs_value = assocs[key_to_search]
|
113
|
+
includes[hash_idx] = build_includes_int(includes[hash_idx], assocs_value)
|
114
|
+
includes
|
115
|
+
end
|
116
|
+
|
117
|
+
else # [:a, :b, :c] vs :x
|
118
|
+
# [:a, :b, :c] vs {:x => y}
|
119
|
+
includes + [assocs]
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
# checks whether the class is a valid storage for saved queries
|
125
|
+
def validate_query_model(query_store_model) #:nodoc:
|
126
|
+
unless query_store_model.respond_to?(:list)
|
127
|
+
fail ::Wice::WiceGridArgumentError.new("Model for saving queries #{query_store_model.class.name} is invalid - there is no class method #list defined")
|
128
|
+
end
|
129
|
+
arit = query_store_model.method(:list).arity
|
130
|
+
unless arit == 2
|
131
|
+
fail ::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}")
|
132
|
+
end
|
133
|
+
@@model_validated = true
|
134
|
+
end
|
135
|
+
|
136
|
+
# Retrieves and constantizes (if needed ) the Query Store model
|
137
|
+
def get_query_store_model #:nodoc:
|
138
|
+
query_store_model = Wice::ConfigurationProvider.value_for(:QUERY_STORE_MODEL)
|
139
|
+
query_store_model = query_store_model.constantize if query_store_model.is_a? String
|
140
|
+
fail ::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.is_a? Class
|
141
|
+
validate_query_model(query_store_model) unless @@model_validated
|
142
|
+
query_store_model
|
143
|
+
end
|
144
|
+
|
145
|
+
def get_string_matching_operators(model) #:nodoc:
|
146
|
+
if defined?(Wice::Defaults::STRING_MATCHING_OPERATORS) && (ops = Wice::ConfigurationProvider.value_for(:STRING_MATCHING_OPERATORS)) &&
|
147
|
+
ops.is_a?(Hash) && (str_matching_operator = ops[model.connection.class.to_s])
|
148
|
+
str_matching_operator
|
149
|
+
else
|
150
|
+
Wice::ConfigurationProvider.value_for(:STRING_MATCHING_OPERATOR)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def deprecated_call(old_name, new_name, opts) #:nodoc:
|
155
|
+
if opts[old_name] && !opts[new_name]
|
156
|
+
opts[new_name] = opts[old_name]
|
157
|
+
opts.delete(old_name)
|
158
|
+
STDERR.puts "WiceGrid: Parameter :#{old_name} is deprecated, use :#{new_name} instead!"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def log(message) #:nodoc:
|
163
|
+
ActiveRecord::Base.logger.info('WiceGrid: ' + message)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
module NlMessage #:nodoc:
|
168
|
+
class << self
|
169
|
+
def [](key) #:nodoc:
|
170
|
+
I18n.t(key, scope: 'wice_grid')
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
module ConfigurationProvider #:nodoc:
|
176
|
+
class << self
|
177
|
+
def value_for(key, strict: true) #:nodoc:
|
178
|
+
if Wice::Defaults.const_defined?(key)
|
179
|
+
Wice::Defaults.const_get(key)
|
180
|
+
else
|
181
|
+
if strict
|
182
|
+
fail WiceGridMissingConfigurationConstantException.new("Could not find constant #{key} in the configuration file!" \
|
183
|
+
' It is possible that the version of WiceGrid you are using is newer than the installed configuration file in config/initializers. ' \
|
184
|
+
"Constant Wice::Defaults::#{key} is missing and you need to add it manually to wice_grid_config.rb or run the generator task=:\n" \
|
185
|
+
' rails g wice_grid:install')
|
186
|
+
end # else nil
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
module Defaults #:nodoc:
|
193
|
+
end
|
194
|
+
|
195
|
+
module ExceptionsMixin #:nodoc:
|
196
|
+
def initialize(str) #:nodoc:
|
197
|
+
super('WiceGrid: ' + str)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
class WiceGridArgumentError < ArgumentError #:nodoc:
|
201
|
+
include ExceptionsMixin
|
202
|
+
end
|
203
|
+
class WiceGridException < Exception #:nodoc:
|
204
|
+
include ExceptionsMixin
|
205
|
+
end
|
206
|
+
class WiceGridMissingConfigurationConstantException < Exception #:nodoc:
|
207
|
+
include ExceptionsMixin
|
208
|
+
end
|
209
|
+
end
|