active_scaffold_batch 3.7.3 → 3.9.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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README +101 -5
  3. data/app/assets/javascripts/active_scaffold_batch.js +10 -10
  4. data/app/assets/stylesshets/active_scaffold_batch.scss +4 -13
  5. data/app/views/active_scaffold_overrides/_batch_create_form.html.erb +8 -8
  6. data/app/views/active_scaffold_overrides/_batch_create_form_attribute.html.erb +7 -19
  7. data/app/views/active_scaffold_overrides/_batch_create_form_body.html.erb +24 -23
  8. data/app/views/active_scaffold_overrides/_batch_create_form_footer.html.erb +11 -2
  9. data/app/views/active_scaffold_overrides/_batch_create_form_horizontal.html.erb +24 -9
  10. data/app/views/active_scaffold_overrides/_batch_create_form_horizontal_record.html.erb +11 -26
  11. data/app/views/active_scaffold_overrides/_batch_create_form_multiple.html.erb +1 -1
  12. data/app/views/active_scaffold_overrides/_batch_create_form_vertical.html.erb +12 -3
  13. data/app/views/active_scaffold_overrides/_batch_create_form_vertical_record.html.erb +5 -5
  14. data/app/views/active_scaffold_overrides/_batch_update_form.html.erb +6 -6
  15. data/app/views/active_scaffold_overrides/_batch_update_form_attribute.html.erb +1 -11
  16. data/app/views/active_scaffold_overrides/_batch_update_form_attribute_scope.html.erb +7 -12
  17. data/app/views/active_scaffold_overrides/_batch_update_form_body.html.erb +24 -23
  18. data/app/views/active_scaffold_overrides/_form_messages.html.erb +16 -4
  19. data/app/views/active_scaffold_overrides/batch_add.js.erb +16 -3
  20. data/app/views/active_scaffold_overrides/batch_create.html.erb +1 -1
  21. data/app/views/active_scaffold_overrides/batch_update.html.erb +1 -1
  22. data/app/views/active_scaffold_overrides/on_batch_base.js.erb +14 -4
  23. data/app/views/active_scaffold_overrides/on_batch_create.js.erb +10 -5
  24. data/app/views/active_scaffold_overrides/on_batch_update.js.erb +6 -4
  25. data/config/locales/de.yml +2 -1
  26. data/config/locales/en.yml +2 -1
  27. data/config/locales/es.yml +2 -1
  28. data/lib/active_scaffold/actions/batch_base.rb +23 -28
  29. data/lib/active_scaffold/actions/batch_create.rb +78 -44
  30. data/lib/active_scaffold/actions/batch_destroy.rb +16 -15
  31. data/lib/active_scaffold/actions/batch_update.rb +129 -104
  32. data/lib/active_scaffold/config/batch_create.rb +17 -19
  33. data/lib/active_scaffold/config/batch_destroy.rb +24 -14
  34. data/lib/active_scaffold/config/batch_update.rb +21 -20
  35. data/lib/active_scaffold/helpers/batch_create_column_helpers.rb +17 -18
  36. data/lib/active_scaffold/helpers/datepicker_update_column_helpers.rb +48 -17
  37. data/lib/active_scaffold/helpers/update_column_helpers.rb +91 -33
  38. data/lib/active_scaffold_batch/engine.rb +10 -12
  39. data/lib/active_scaffold_batch/extensions.rb +16 -0
  40. data/lib/active_scaffold_batch/version.rb +4 -2
  41. data/lib/active_scaffold_batch.rb +5 -15
  42. metadata +7 -5
@@ -1,29 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffold::Actions
2
4
  module BatchUpdate
5
+ GENERIC_OPERATORS = %w[
6
+ NO_UPDATE
7
+ REPLACE
8
+ ].freeze
9
+ NUMERIC_OPERATORS = %w[
10
+ PLUS
11
+ MINUS
12
+ TIMES
13
+ DIVISION
14
+ ].freeze
15
+ NUMERIC_OPTIONS = %w[
16
+ ABSOLUTE
17
+ PERCENT
18
+ ].freeze
19
+
20
+ DATE_OPERATORS = %w[
21
+ PLUS
22
+ MINUS
23
+ ].freeze
3
24
 
4
- GenericOperators = [
5
- 'NO_UPDATE',
6
- 'REPLACE'
7
- ]
8
- NumericOperators = [
9
- 'PLUS',
10
- 'MINUS',
11
- 'TIMES',
12
- 'DIVISION'
13
- ]
14
- NumericOptions = [
15
- 'ABSOLUTE',
16
- 'PERCENT'
17
- ]
18
-
19
- DateOperators = [
20
- 'PLUS',
21
- 'MINUS'
22
- ]
23
-
24
25
  def self.included(base)
25
26
  base.send :include, ActiveScaffold::Actions::BatchBase unless base < ActiveScaffold::Actions::BatchBase
26
- base.before_action :batch_update_authorized_filter, :only => [:batch_edit, :batch_update]
27
+ base.before_action :batch_update_authorized_filter, only: %i[batch_edit batch_update]
27
28
  base.helper_method :batch_update_values
28
29
  end
29
30
 
@@ -36,18 +37,18 @@ module ActiveScaffold::Actions
36
37
  batch_action
37
38
  end
38
39
 
39
-
40
40
  protected
41
+
41
42
  def batch_edit_respond_to_html
42
43
  if batch_successful?
43
- render(:action => 'batch_update')
44
+ render(action: 'batch_update')
44
45
  else
45
46
  return_to_main
46
47
  end
47
48
  end
48
49
 
49
50
  def batch_edit_respond_to_js
50
- render(:partial => 'batch_update_form')
51
+ render(partial: 'batch_update_form')
51
52
  end
52
53
 
53
54
  def batch_update_values
@@ -55,18 +56,18 @@ module ActiveScaffold::Actions
55
56
  end
56
57
 
57
58
  def batch_update_respond_to_html
58
- if params[:iframe]=='true' # was this an iframe post ?
59
+ if params[:iframe] == 'true' # was this an iframe post ?
59
60
  flash[:info] = as_(:batch_processing_successful) if batch_successful?
60
61
  do_refresh_list
61
62
  responds_to_parent do
62
- render :action => 'on_batch_update.js', :layout => false
63
+ render action: 'on_batch_update.js', layout: false
63
64
  end
64
65
  else # just a regular post
65
66
  if batch_successful?
66
- flash[:info] = as_(:updated_model, :model => @record.to_label)
67
+ flash[:info] = as_(:updated_model, model: @record.to_label)
67
68
  return_to_main
68
69
  else
69
- render(:action => 'batch_update')
70
+ render(action: 'batch_update')
70
71
  end
71
72
  end
72
73
  end
@@ -74,7 +75,7 @@ module ActiveScaffold::Actions
74
75
  def batch_update_respond_to_js
75
76
  flash[:info] = as_(:batch_processing_successful) if batch_successful?
76
77
  do_refresh_list
77
- render :action => 'on_batch_update'
78
+ render action: 'on_batch_update'
78
79
  end
79
80
 
80
81
  def do_batch_edit
@@ -83,42 +84,49 @@ module ActiveScaffold::Actions
83
84
  end
84
85
 
85
86
  def before_do_batch_update
86
- @batch_update_values = update_attribute_values_from_params(active_scaffold_config.batch_update.columns, params[:record])
87
+ @batch_update_values = update_attribute_values_from_params(
88
+ active_scaffold_config.batch_update.columns,
89
+ params[:record]
90
+ )
87
91
  end
88
92
 
89
93
  def batch_update_listed
90
94
  case active_scaffold_config.batch_update.process_mode
91
- when :update then
92
- each_record_in_scope {|record| update_record_in_batch(record) if authorized_for_job?(record)}
93
- when :update_all then
95
+ when :update
96
+ each_record_in_scope { |record| update_record_in_batch(record) if authorized_for_job?(record) }
97
+ when :update_all
94
98
  updates = updates_for_update_all
95
99
  unless updates.first.empty?
96
100
  do_search if respond_to? :do_search, true
97
101
  # all_conditions might fail cause joins are not working in update_all
98
- active_scaffold_config.model.update_all(updates, all_conditions)
102
+ # update_all is the explicitly configured batch processing mode.
103
+ active_scaffold_config.model.update_all(updates, all_conditions) # rubocop:disable Rails/SkipsModelValidations
99
104
  end
100
105
  else
101
- Rails.logger.error("Unknown process_mode: #{active_scaffold_config.batch_update.process_mode} for action batch_update")
106
+ Rails.logger.error "Unknown process_mode: #{active_scaffold_config.batch_update.process_mode} " \
107
+ 'for action batch_update'
102
108
  end
103
-
104
109
  end
105
110
 
106
111
  def batch_update_marked
107
112
  case active_scaffold_config.batch_update.process_mode
108
- when :update then
109
- each_marked_record {|record| update_record_in_batch(record) if authorized_for_job?(record)}
110
- when :update_all then
113
+ when :update
114
+ each_marked_record { |record| update_record_in_batch(record) if authorized_for_job?(record) }
115
+ when :update_all
111
116
  updates = updates_for_update_all
112
117
  unless updates.first.empty?
113
- active_scaffold_config.model.where(active_scaffold_config.model.primary_key => marked_records.to_a).update_all(updates)
118
+ model = active_scaffold_config.model
119
+ # update_all is the explicitly configured batch processing mode.
120
+ model.where(model.primary_key => marked_records.to_a).update_all(updates) # rubocop:disable Rails/SkipsModelValidations
114
121
  do_demark_all
115
122
  end
116
123
  else
117
- Rails.logger.error("Unknown process_mode: #{active_scaffold_config.batch_update.process_mode} for action batch_update")
124
+ Rails.logger.error "Unknown process_mode: #{active_scaffold_config.batch_update.process_mode} " \
125
+ 'for action batch_update'
118
126
  end
119
127
  end
120
128
 
121
- def updates_for_update_all()
129
+ def updates_for_update_all
122
130
  update_all = [[]]
123
131
  batch_update_values.each do |attribute, value|
124
132
  sql_set, value = get_update_all_attribute(value[:column], attribute, value[:value])
@@ -127,10 +135,24 @@ module ActiveScaffold::Actions
127
135
  update_all << value if value.present?
128
136
  end
129
137
  end
138
+ add_timestamps_to_update_all(update_all) if update_all.first.any?
130
139
  update_all[0] = update_all.first.join(',')
131
140
  update_all
132
141
  end
133
142
 
143
+ def add_timestamps_to_update_all(update_all)
144
+ return unless active_scaffold_config.batch_update.update_timestamps
145
+
146
+ model = active_scaffold_config.model
147
+ model.touch_attributes_with_time.each do |attribute, time|
148
+ next if batch_update_values.key?(attribute) || batch_update_values.key?(attribute.to_sym)
149
+
150
+ quoted_attribute = model.connection.quote_column_name(attribute)
151
+ update_all.first << "#{quoted_attribute} = ?"
152
+ update_all << time
153
+ end
154
+ end
155
+
134
156
  def update_record_in_batch(record)
135
157
  @successful = nil
136
158
  @record = record
@@ -138,8 +160,8 @@ module ActiveScaffold::Actions
138
160
  batch_update_values.each do |attribute, value|
139
161
  set_record_attribute(value[:column], attribute, value[:value])
140
162
  end
141
-
142
- update_save(:no_record_param_update => true)
163
+
164
+ update_save(no_record_param_update: true)
143
165
  if successful?
144
166
  @record.as_marked = false if batch_scope == 'MARKED'
145
167
  else
@@ -149,7 +171,7 @@ module ActiveScaffold::Actions
149
171
 
150
172
  def get_update_all_attribute(column, attribute, value)
151
173
  form_ui = column_form_ui(column)
152
-
174
+
153
175
  if form_ui && (method = override_batch_update_all_value(form_ui))
154
176
  update_value = send(method, column, value)
155
177
  if update_value.nil?
@@ -162,8 +184,8 @@ module ActiveScaffold::Actions
162
184
  sql_set = "#{attribute} = ?"
163
185
  update_value = value[:value]
164
186
  end
165
-
166
- return sql_set, update_value
187
+
188
+ [sql_set, update_value]
167
189
  end
168
190
 
169
191
  # TODO: delete when ActiveScaffold 3.5 is released and required
@@ -174,19 +196,21 @@ module ActiveScaffold::Actions
174
196
  def update_attribute_values_from_params(columns, attributes)
175
197
  values = {}
176
198
  return values unless params_hash? attributes
199
+
177
200
  columns.each_column(for: new_model, crud_type: :update, flatten: true) do |column|
178
201
  next unless params_hash?(attributes[column.name]) && attributes[column.name][:operator] != 'NO_UPDATE'
202
+
179
203
  value = attributes[column.name]
180
204
  value[:value] = value[:operator] == 'NULL' ? nil : column_value_from_param_value(nil, column, value[:value])
181
- values[column.name] = {:column => column, :value => value}
205
+ values[column.name] = {column: column, value: value}
182
206
  end
183
207
  values
184
208
  end
185
-
209
+
186
210
  # The default security delegates to ActiveRecordPermissions.
187
211
  # You may override the method to customize.
188
212
  def batch_update_authorized?(record = nil)
189
- authorized_for?(:crud_type => :update)
213
+ authorized_for?(crud_type: :update)
190
214
  end
191
215
 
192
216
  def batch_update_ignore?(record = nil)
@@ -195,72 +219,72 @@ module ActiveScaffold::Actions
195
219
 
196
220
  def batch_update_value_for_numeric(column, record, calculation_info)
197
221
  current_value = record.send(column.name)
198
- if ActiveScaffold::Actions::BatchUpdate::GenericOperators.include?(calculation_info[:operator]) || ActiveScaffold::Actions::BatchUpdate::NumericOperators.include?(calculation_info[:operator])
199
- operand = self.class.condition_value_for_numeric(column, calculation_info[:value])
200
- operand = current_value / 100 * operand if calculation_info[:opt] == 'PERCENT'
201
- case calculation_info[:operator]
202
- when 'REPLACE' then operand
203
- when 'NULL' then nil
204
- when 'PLUS' then current_value.present? ? current_value + operand : nil
205
- when 'MINUS' then current_value.present? ? current_value - operand : nil
206
- when 'TIMES' then current_value.present? ? current_value * operand : nil
207
- when 'DIVISION' then current_value.present? ? current_value / operand : nil
208
- else
209
- current_value
210
- end
222
+ operator = calculation_info[:operator]
223
+ return current_value unless batch_update_operator?(operator, NUMERIC_OPERATORS)
224
+
225
+ operand = self.class.condition_value_for_numeric(column, calculation_info[:value])
226
+ operand = current_value / 100 * operand if calculation_info[:opt] == 'PERCENT'
227
+ return operand if operator == 'REPLACE'
228
+ return if operator == 'NULL' || current_value.blank?
229
+
230
+ apply_numeric_operator(operator, current_value, operand)
231
+ end
232
+
233
+ def apply_numeric_operator(operator, current_value, operand)
234
+ case operator
235
+ when 'PLUS' then current_value + operand
236
+ when 'MINUS' then current_value - operand
237
+ when 'TIMES' then current_value * operand
238
+ when 'DIVISION' then current_value / operand
211
239
  else
212
240
  current_value
213
241
  end
214
242
  end
215
- alias_method :batch_update_value_for_integer, :batch_update_value_for_numeric
216
- alias_method :batch_update_value_for_decimal, :batch_update_value_for_numeric
217
- alias_method :batch_update_value_for_float, :batch_update_value_for_numeric
243
+ alias batch_update_value_for_integer batch_update_value_for_numeric
244
+ alias batch_update_value_for_decimal batch_update_value_for_numeric
245
+ alias batch_update_value_for_float batch_update_value_for_numeric
218
246
 
219
247
  def batch_update_all_value_for_numeric(column, calculation_info)
220
248
  operator = calculation_info[:operator]
221
- if operator == 'NULL' || ActiveScaffold::Actions::BatchUpdate::GenericOperators.include?(operator) || ActiveScaffold::Actions::BatchUpdate::NumericOperators.include?(operator)
222
- operand = active_scaffold_config.model.quote_value(self.class.condition_value_for_numeric(column, calculation_info[:value]))
223
- if calculation_info[:opt] == 'PERCENT'
224
- operand = "#{active_scaffold_config.model.connection.quote_column_name(column.name)} / 100.0 * #{operand}"
225
- end
226
- case calculation_info[:operator]
227
- when 'REPLACE' then operand
228
- when 'NULL' then active_scaffold_config.model.quote_value(nil)
229
- when 'PLUS' then "#{active_scaffold_config.model.connection.quote_column_name(column.name)} + #{operand}"
230
- when 'MINUS' then "#{active_scaffold_config.model.connection.quote_column_name(column.name)} - #{operand}"
231
- when 'TIMES' then "#{active_scaffold_config.model.connection.quote_column_name(column.name)} * #{operand}"
232
- when 'DIVISION' then "#{active_scaffold_config.model.connection.quote_column_name(column.name)} / #{operand}"
233
- else
234
- nil
235
- end
236
- else
237
- nil
249
+ return unless batch_update_operator?(operator, NUMERIC_OPERATORS)
250
+
251
+ model = active_scaffold_config.model
252
+ connection = model.connection
253
+ operand = model.quote_value(self.class.condition_value_for_numeric(column, calculation_info[:value]))
254
+ if calculation_info[:opt] == 'PERCENT'
255
+ operand = "#{connection.quote_column_name(column.name)} / 100.0 * #{operand}"
256
+ end
257
+ case calculation_info[:operator]
258
+ when 'REPLACE' then operand
259
+ when 'NULL' then model.quote_value(nil)
260
+ when 'PLUS' then "#{connection.quote_column_name(column.name)} + #{operand}"
261
+ when 'MINUS' then "#{connection.quote_column_name(column.name)} - #{operand}"
262
+ when 'TIMES' then "#{connection.quote_column_name(column.name)} * #{operand}"
263
+ when 'DIVISION' then "#{connection.quote_column_name(column.name)} / #{operand}"
238
264
  end
239
265
  end
240
- alias_method :batch_update_all_value_for_integer, :batch_update_all_value_for_numeric
241
- alias_method :batch_update_all_value_for_decimal, :batch_update_all_value_for_numeric
242
- alias_method :batch_update_all_value_for_float, :batch_update_all_value_for_numeric
266
+ alias batch_update_all_value_for_integer batch_update_all_value_for_numeric
267
+ alias batch_update_all_value_for_decimal batch_update_all_value_for_numeric
268
+ alias batch_update_all_value_for_float batch_update_all_value_for_numeric
243
269
 
244
270
  def batch_update_value_for_date_picker(column, record, calculation_info)
245
271
  current_value = record.send(column.name)
246
272
  operator = calculation_info[:operator]
247
- if operator == 'NULL' || ActiveScaffold::Actions::BatchUpdate::GenericOperators.include?(operator) || ActiveScaffold::Actions::BatchUpdate::DateOperators.include?(operator)
248
- operand = self.class.condition_value_for_datetime(column, calculation_info[:value], column.column.type == :date ? :to_date : :to_time)
249
- case calculation_info[:operator]
250
- when 'REPLACE' then operand
251
- when 'NULL' then nil
252
- when 'PLUS' then
253
- trend_number = [calculation_info['number'].to_i, 1].max
254
- current_value&.in((trend_number).send(calculation_info['unit'].downcase.singularize.to_sym))
255
- when 'MINUS' then
256
- trend_number = [calculation_info['number'].to_i, 1].max
257
- current_value&.ago((trend_number).send(calculation_info['unit'].downcase.singularize.to_sym))
258
- else
259
- current_value
260
- end
261
- else
262
- current_value
263
- end
273
+ return current_value unless batch_update_operator?(operator, DATE_OPERATORS)
274
+
275
+ conversion = column.column.type == :date ? :to_date : :to_time
276
+ operand = self.class.condition_value_for_datetime(column, calculation_info[:value], conversion)
277
+ return operand if operator == 'REPLACE'
278
+ return if operator == 'NULL'
279
+ return current_value unless DATE_OPERATORS.include?(operator)
280
+
281
+ trend_number = [calculation_info['number'].to_i, 1].max
282
+ duration = trend_number.send(calculation_info['unit'].downcase.singularize.to_sym)
283
+ operator == 'PLUS' ? current_value&.in(duration) : current_value&.ago(duration)
284
+ end
285
+
286
+ def batch_update_operator?(operator, type_operators)
287
+ operator == 'NULL' || GENERIC_OPERATORS.include?(operator) || type_operators.include?(operator)
264
288
  end
265
289
 
266
290
  def override_batch_update_value(form_ui)
@@ -277,8 +301,9 @@ module ActiveScaffold::Actions
277
301
 
278
302
  def batch_update_authorized_filter
279
303
  link = active_scaffold_config.batch_update.link || active_scaffold_config.batch_update.class.link
280
- raise ActiveScaffold::ActionNotAllowed unless self.send(link.security_method)
304
+ raise ActiveScaffold::ActionNotAllowed unless send(link.security_method)
281
305
  end
306
+
282
307
  def batch_edit_formats
283
308
  (default_formats + active_scaffold_config.formats).uniq
284
309
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffold::Config
2
4
  class BatchCreate < ActiveScaffold::Config::Form
3
5
  self.crud_type = :create
@@ -13,37 +15,33 @@ module ActiveScaffold::Config
13
15
  # global level configuration
14
16
  # --------------------------
15
17
  # the ActionLink for this action
16
- def self.link
17
- @@link
18
- end
19
- def self.link=(val)
20
- @@link = val
21
- end
22
- @@link = ActiveScaffold::DataStructures::ActionLink.new('batch_new', :label => :create, :type => :collection, :security_method => :batch_create_authorized?, :ignore_method => :batch_create_ignore?)
18
+ cattr_accessor :link,
19
+ instance_accessor: false,
20
+ default: ActiveScaffold::DataStructures::ActionLink.new(
21
+ 'batch_new', label: :create, type: :collection,
22
+ security_method: :batch_create_authorized?, ignore_method: :batch_create_ignore?
23
+ )
23
24
 
24
25
  # configures where the plugin itself is located. there is no instance version of this.
25
- cattr_accessor :plugin_directory
26
- @@plugin_directory = File.expand_path(__FILE__).match(%{(^.*)/lib/active_scaffold/config/batch_create.rb})[1]
26
+ cattr_accessor :plugin_directory,
27
+ instance_accessor: false,
28
+ default: File.expand_path(__FILE__).match(%r{(^.*)/lib/active_scaffold/config/batch_create.rb})[1]
27
29
 
28
30
  # configures how batch create should be processed
29
31
  # :create => standard activerecord create including validations
30
- cattr_accessor :process_mode
31
- @@process_mode = :create
32
+ cattr_accessor :process_mode, instance_accessor: false, default: :create
32
33
 
33
34
  # you may update all records in list view or all marked records
34
35
  # you might disable list mode with this switch if you think it is
35
36
  # too "dangerous"
36
- cattr_accessor :list_mode_enabled
37
- @@list_mode_enabled = true
37
+ cattr_accessor :list_mode_enabled, instance_accessor: false, default: true
38
38
 
39
39
  # run all create statements in a transaction, so no record is created
40
40
  # if someone fails
41
- cattr_accessor :run_in_transaction
42
- @@run_in_transaction = true
41
+ cattr_accessor :run_in_transaction, instance_accessor: false, default: true
43
42
 
44
43
  # layout for create multiple records
45
- cattr_accessor :layout
46
- @@layout = :vertical
44
+ cattr_accessor :layout, instance_accessor: false, default: :vertical
47
45
 
48
46
  # instance-level configuration
49
47
  # ----------------------------
@@ -72,8 +70,8 @@ module ActiveScaffold::Config
72
70
 
73
71
  # the label= method already exists in the Form base class
74
72
  def label(model = nil, core: @core)
75
- model ||= core.label(:count => 2)
76
- @label ? as_(@label) : as_(:create_model, :model => model)
73
+ model ||= core.label(count: 2)
74
+ @label ? as_(@label) : as_(:create_model, model: model)
77
75
  end
78
76
  end
79
77
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffold::Config
2
4
  class BatchDestroy < ActiveScaffold::Config::Base
3
5
  self.crud_type = :delete
4
-
6
+
5
7
  def initialize(core_config)
6
8
  super
7
9
  @action_group ||= 'collection.batch.destroy'
@@ -11,25 +13,33 @@ module ActiveScaffold::Config
11
13
  # global level configuration
12
14
  # --------------------------
13
15
  # the ActionLink for this action
14
- def self.link
15
- @@link
16
- end
17
- def self.link=(val)
18
- @@link = val
19
- end
20
- @@link = [ ActiveScaffold::DataStructures::ActionLink.new('batch_destroy', :label => :listed, :type => :collection, :method => :delete, :position => false, :crud_type => :delete, :confirm => :are_you_sure_to_delete, :parameters => {:batch_scope => 'LISTED'},:security_method => :batch_destroy_authorized?),
21
- ActiveScaffold::DataStructures::ActionLink.new('batch_destroy', :label => :marked, :type => :collection, :method => :delete, :position => false, :crud_type => :delete, :confirm => :are_you_sure_to_delete, :parameters => {:batch_scope => 'MARKED'}, :security_method => :batch_destroy_authorized?, :ignore_method => :batch_destroy_marked_ignore?)]
16
+ cattr_accessor :link,
17
+ instance_accessor: false,
18
+ default: [
19
+ ActiveScaffold::DataStructures::ActionLink.new(
20
+ 'batch_destroy', label: :listed, type: :collection, method: :delete, position: false,
21
+ crud_type: :delete, confirm: :are_you_sure_to_delete,
22
+ parameters: {batch_scope: 'LISTED'},
23
+ security_method: :batch_destroy_authorized?
24
+ ),
25
+ ActiveScaffold::DataStructures::ActionLink.new(
26
+ 'batch_destroy', label: :marked, type: :collection, method: :delete, position: false,
27
+ crud_type: :delete, confirm: :are_you_sure_to_delete,
28
+ parameters: {batch_scope: 'MARKED'},
29
+ security_method: :batch_destroy_authorized?,
30
+ ignore_method: :batch_destroy_marked_ignore?
31
+ )
32
+ ]
22
33
 
23
34
  # configures where the plugin itself is located. there is no instance version of this.
24
- cattr_accessor :plugin_directory
25
- @@plugin_directory = File.expand_path(__FILE__).match(%{(^.*)/lib/active_scaffold/config/batch_destroy.rb})[1]
35
+ cattr_accessor :plugin_directory,
36
+ instance_accessor: false,
37
+ default: File.expand_path(__FILE__).match(%r{(^.*)/lib/active_scaffold/config/batch_destroy.rb})[1]
26
38
 
27
39
  # configures how batch updates should be processed
28
40
  # :delete => standard activerecord delete including validations
29
41
  # :delete_all => updating in one sql call without activerecord instantiation and validation
30
- cattr_accessor :process_mode
31
- @@process_mode = :delete
32
-
42
+ cattr_accessor :process_mode, instance_accessor: false, default: :delete
33
43
 
34
44
  # instance-level configuration
35
45
  # ----------------------------
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffold::Config
2
4
  class BatchUpdate < ActiveScaffold::Config::Form
3
5
  self.crud_type = :update
@@ -6,47 +8,46 @@ module ActiveScaffold::Config
6
8
  @process_mode = self.class.process_mode
7
9
  @action_group ||= 'collection.batch'
8
10
  @list_mode_enabled = self.class.list_mode_enabled
11
+ @update_timestamps = self.class.update_timestamps
9
12
  end
10
13
 
11
14
  # global level configuration
12
15
  # --------------------------
13
16
  # the ActionLink for this action
14
- def self.link
15
- @@link
16
- end
17
- def self.link=(val)
18
- @@link = val
19
- end
20
- @@link = ActiveScaffold::DataStructures::ActionLink.new('batch_edit', :label => :edit, :type => :collection, :security_method => :batch_update_authorized?, :ignore_method => :batch_update_ignore?)
17
+ cattr_accessor :link,
18
+ instance_accessor: false,
19
+ default: ActiveScaffold::DataStructures::ActionLink.new(
20
+ 'batch_edit', label: :edit, type: :collection,
21
+ security_method: :batch_update_authorized?, ignore_method: :batch_update_ignore?
22
+ )
21
23
 
22
24
  # configures where the plugin itself is located. there is no instance version of this.
23
- cattr_accessor :plugin_directory
24
- @@plugin_directory = File.expand_path(__FILE__).match(%{(^.*)/lib/active_scaffold/config/batch_update.rb})[1]
25
+ cattr_accessor :plugin_directory,
26
+ instance_accessor: false,
27
+ default: File.expand_path(__FILE__).match(%r{(^.*)/lib/active_scaffold/config/batch_update.rb})[1]
25
28
 
26
29
  # configures how batch updates should be processed
27
30
  # :update => standard activerecord update including validations
28
31
  # :update_all => updating in one sql call without activerecord instantiation and validation
29
- cattr_accessor :process_mode
30
- @@process_mode = :update
32
+ cattr_accessor :process_mode, instance_accessor: false, default: :update
31
33
 
32
34
  # you may update all records in list view or all marked records
33
35
  # you might disable list mode with this switch if you think it is
34
36
  # too "dangerous"
35
- cattr_accessor :list_mode_enabled
36
- @@list_mode_enabled = true
37
+ cattr_accessor :list_mode_enabled, instance_accessor: false, default: true
38
+
39
+ # update the model's update timestamp when process_mode is :update_all
40
+ cattr_accessor :update_timestamps, instance_accessor: false, default: true
37
41
  # instance-level configuration
38
42
  # ----------------------------
39
43
 
40
- # see class accessor
41
- attr_accessor :process_mode
42
-
43
- attr_accessor :list_mode_enabled
44
-
44
+ # see class accessors
45
+ attr_accessor :process_mode, :list_mode_enabled, :update_timestamps
45
46
 
46
47
  # the label= method already exists in the Form base class
47
48
  def label(model = nil, core: @core)
48
- model ||= core.label(:count => 2)
49
- @label ? as_(@label) : as_(:update_model, :model => model)
49
+ model ||= core.label(count: 2)
50
+ @label ? as_(@label) : as_(:update_model, model: model)
50
51
  end
51
52
  end
52
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveScaffold
2
4
  module Helpers
3
5
  # Helpers that assist with the rendering of a Form Column
@@ -7,30 +9,26 @@ module ActiveScaffold
7
9
  def active_scaffold_batch_create_by_column(column, scope = nil, options = {})
8
10
  options = active_scaffold_input_options(column, scope, options)
9
11
 
12
+ ui_options = column.form_ui_options || column.options
10
13
  if column.form_ui == :record_select
11
- ui_options = column.form_ui_options || column.options
12
- active_scaffold_record_select(options[:object], column, options, batch_create_by_records, true, ui_options: ui_options)
14
+ active_scaffold_record_select(options[:object], column, options, batch_create_by_records, true,
15
+ ui_options: ui_options)
13
16
  elsif column.association
14
- active_scaffold_batch_create_singular_association(column, options)
17
+ active_scaffold_batch_create_singular_association(column, options, ui_options: ui_options)
15
18
  else
16
- text_area_tag(column.name, params[:record] ? params[:record][column.name] : '', options.merge(column.options[:html_options] || {}))
19
+ text_area_tag(column.name, params[:record] ? params[:record][column.name] : '',
20
+ options.merge(column.options[:html_options] || {}))
17
21
  end
18
-
19
22
  end
20
23
 
21
- def active_scaffold_batch_create_singular_association(column, html_options)
22
- associated_options = batch_create_by_records.collect {|r| r.id}
23
- select_options = sorted_association_options_find(column.association, nil, html_options.delete(:record))
24
- html_options.update(column.options[:html_options] || {})
25
- options = {}
26
- options.update(column.options)
27
- html_options[:name] = "#{html_options[:name]}[]"
28
- html_options[:multiple] = true
29
- select_tag(column.name, options_from_collection_for_select(select_options.uniq, :id, column.options[:label_method] || :to_label, associated_options), html_options)
30
- end
31
-
32
- def batch_create_multiple_remove_link
33
- link_to as_(:remove), '#', :class => 'remove'
24
+ def active_scaffold_batch_create_singular_association(column, html_options, ui_options: column.options)
25
+ associated_options = batch_create_by_records.collect(&:id)
26
+ active_scaffold_input_singular_association(
27
+ column,
28
+ html_options.merge(multiple: true, associated: associated_options),
29
+ {include_blank: false},
30
+ ui_options: ui_options
31
+ )
34
32
  end
35
33
 
36
34
  def batch_create_multiple_layout
@@ -40,6 +38,7 @@ module ActiveScaffold
40
38
  def current_form_columns(record, scope, subform_controller = nil)
41
39
  columns = super
42
40
  return columns if columns
41
+
43
42
  if %i[batch_new batch_create].include? action_name.to_sym
44
43
  active_scaffold_config.batch_create.columns.visible_columns_names
45
44
  elsif %i[batch_edit batch_update].include? action_name.to_sym