cm-admin 2.4.4 → 2.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/controllers/cm_admin/resource_controller.rb +26 -11
- data/app/models/concerns/cm_admin/bulk_action_processor.rb +38 -17
- data/lib/cm_admin/models/action.rb +1 -1
- data/lib/cm_admin/models/dsl_method.rb +4 -2
- data/lib/cm_admin/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eca7a51066874791c62583654eea82878e1b84fefaca0ac6893972573fa21bdc
|
4
|
+
data.tar.gz: eb36d11d0a1d47f8c854fd79b19621148ef84ed45d2afcb72609441761c47f0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c0596af7a585db67aa2f16a902f7765f94b6419fdff631efff2cdad9f6f9d885d61fe2ce55c783704c91b575919253b6cc3b504bfc4cba02fe945dbec139030
|
7
|
+
data.tar.gz: b1228ab04a4664b6b0452b83667e5bef30591e5787775002a66897db3b1a1145ebc00360abbffa9bd659f508b6fc99c1f4a96b364601564f038afcf7ad3cb0e7
|
data/Gemfile.lock
CHANGED
@@ -111,22 +111,37 @@ module CmAdmin
|
|
111
111
|
|
112
112
|
def cm_bulk_action(params)
|
113
113
|
@model = Model.find_by({ name: controller_name.classify })
|
114
|
-
@bulk_action_processor = CmAdmin::BulkActionProcessor.new(@action, @model, params)
|
114
|
+
@bulk_action_processor = CmAdmin::BulkActionProcessor.new(@action, @model, params)
|
115
|
+
if @action.execution_mode.to_sym == :bulk
|
116
|
+
@bulk_action_processor.perform_bulk_action
|
117
|
+
else
|
118
|
+
@bulk_action_processor.perform_individual_action
|
119
|
+
end
|
115
120
|
respond_to do |format|
|
121
|
+
action_name = @action.display_name || @action.name.humanize
|
116
122
|
if @bulk_action_processor.invalid_records.empty?
|
117
|
-
flash[:
|
118
|
-
|
119
|
-
format.html { redirect_to request.referrer }
|
123
|
+
flash[:bulk_action_success] = "#{action_name} was successful"
|
124
|
+
format.html { redirect_to request.referrer, notice: "#{action_name} was successful" }
|
120
125
|
else
|
121
|
-
error_messages = @bulk_action_processor.invalid_records.map
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
126
|
+
error_messages = @bulk_action_processor.invalid_records.map{|record| "<li>#{record.error_message}</li>"}.join
|
127
|
+
|
128
|
+
if @action.execution_mode.to_sym == :individual
|
129
|
+
flash[:bulk_action_success] = "#{@bulk_action_processor.success_records.size} rows were successful for #{action_name}" if @bulk_action_processor.success_records.present?
|
130
|
+
flash[:alert] = "<b>#{@bulk_action_processor.invalid_records.size} rows were unsuccessful for #{action_name}</b>"
|
131
|
+
flash[:bulk_action_error] = "<div>
|
132
|
+
<div>
|
133
|
+
<div><b>#{@bulk_action_processor.invalid_records.size} rows were unsuccessful for #{action_name}</b></div>
|
134
|
+
<ul>#{error_messages}</ul>
|
135
|
+
</div>
|
136
|
+
</div>"
|
137
|
+
else
|
138
|
+
flash[:alert] = "<b>#{action_name} was unsuccessful</b>"
|
139
|
+
flash[:bulk_action_error] = "<div>
|
140
|
+
<div><b>#{action_name} encountered the following errors:</b></div>
|
127
141
|
<ul>#{error_messages}</ul>
|
128
142
|
</div>"
|
129
|
-
|
143
|
+
end
|
144
|
+
format.html { redirect_to request.referrer}
|
130
145
|
end
|
131
146
|
end
|
132
147
|
end
|
@@ -1,30 +1,51 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module CmAdmin
|
2
|
+
class BulkActionProcessor
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
attr_accessor :invalid_records, :success_records
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
def initialize(current_action, model, params)
|
7
|
+
@invalid_records = []
|
8
|
+
@success_records = []
|
9
|
+
@current_action = current_action
|
10
|
+
@model = model
|
11
|
+
@params = params
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform_individual_action
|
15
|
+
@params[:selected_ids].split(',').each do |id|
|
16
|
+
ar_object = @model.ar_model.find(id)
|
17
|
+
column_name = @model.available_fields[:index].first.field_name
|
18
|
+
begin
|
19
|
+
@current_action.code_block.call(id)
|
20
|
+
@error_message = nil
|
21
|
+
rescue NoMethodError, NameError => e
|
22
|
+
@error_message = "#{e.message.slice(0..(e.message.index(' for')))} at #{ar_object.send(column_name)}"
|
23
|
+
rescue ActiveRecord::RecordInvalid => e
|
24
|
+
@error_message = "#{e.message} at #{ar_object.send(column_name)}"
|
25
|
+
rescue StandardError => e
|
26
|
+
@error_message = "#{ar_object.send(column_name)} - #{e.message}"
|
27
|
+
end
|
28
|
+
@invalid_records << OpenStruct.new({ row_identifier: ar_object.send(column_name), error_message: @error_message }) if @error_message
|
29
|
+
@success_records << OpenStruct.new({ row_identifier: ar_object.send(column_name) }) unless @error_message
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
11
33
|
|
12
|
-
|
13
|
-
|
14
|
-
ar_object = @model.ar_model.find(id)
|
34
|
+
def perform_bulk_action
|
35
|
+
ar_object = @model.ar_model.where(id: @params[:selected_ids]).first
|
15
36
|
column_name = @model.available_fields[:index].first.field_name
|
16
37
|
begin
|
17
|
-
@current_action.code_block.call(
|
38
|
+
@current_action.code_block.call(@params[:selected_ids])
|
18
39
|
@error_message = nil
|
19
40
|
rescue NoMethodError, NameError => e
|
20
|
-
@error_message =
|
41
|
+
@error_message = e.message.to_s
|
21
42
|
rescue ActiveRecord::RecordInvalid => e
|
22
|
-
@error_message =
|
43
|
+
@error_message = e.message.to_s
|
23
44
|
rescue StandardError => e
|
24
|
-
@error_message =
|
45
|
+
@error_message = e.message.to_s
|
25
46
|
end
|
26
47
|
@invalid_records << OpenStruct.new({ row_identifier: ar_object.send(column_name), error_message: @error_message }) if @error_message
|
48
|
+
self
|
27
49
|
end
|
28
|
-
self
|
29
50
|
end
|
30
51
|
end
|
@@ -7,7 +7,7 @@ module CmAdmin
|
|
7
7
|
attr_accessor :name, :display_name, :verb, :layout_type, :layout, :partial, :path, :page_title, :page_description,
|
8
8
|
:child_records, :is_nested_field, :nested_table_name, :parent, :display_if, :route_type, :code_block,
|
9
9
|
:display_type, :action_type, :redirection_url, :sort_direction, :sort_column, :icon_name, :scopes, :view_type,
|
10
|
-
:kanban_attr, :model_name, :redirect_to
|
10
|
+
:kanban_attr, :model_name, :redirect_to, :execution_mode
|
11
11
|
|
12
12
|
VALID_SORT_DIRECTION = Set[:asc, :desc].freeze
|
13
13
|
|
@@ -286,16 +286,18 @@ module CmAdmin
|
|
286
286
|
# @param modal_configuration [Hash] the configuration of modal
|
287
287
|
# @param route_type [String] the route type of action, +member+, +collection+
|
288
288
|
# @param partial [String] the partial path of action
|
289
|
+
# @param execution_mode [Symbol] the types of execution mode are +:bulk+, +:individual+ [default +:individual+]
|
289
290
|
# @example Creating a bulk action
|
290
|
-
# bulk_action name: 'approve', display_name: 'Approve', display_if: lambda { |arg| arg.draft? }, redirection_url: '/posts', icon_name: 'fa-regular fa-circle-check', verb: :patch, display_type: :modal, modal_configuration: { title: 'Approve Post', description: 'Are you sure you want approve this post', confirmation_text: 'Approve' } do
|
291
|
+
# bulk_action name: 'approve', display_name: 'Approve', display_if: lambda { |arg| arg.draft? }, redirection_url: '/posts', icon_name: 'fa-regular fa-circle-check', verb: :patch, display_type: :modal, modal_configuration: { title: 'Approve Post', description: 'Are you sure you want approve this post', confirmation_text: 'Approve' }, execution_mode: :individual do
|
291
292
|
# posts = ::Post.where(id: params[:ids])
|
292
293
|
# posts.each(&:approved!)
|
293
294
|
# posts
|
294
295
|
# end
|
295
|
-
def bulk_action(name: nil, display_name: nil, display_if: ->(_arg) { true }, redirection_url: nil, icon_name: nil, verb: nil, display_type: nil, modal_configuration: {}, route_type: nil, partial: nil, &block)
|
296
|
+
def bulk_action(name: nil, display_name: nil, display_if: ->(_arg) { true }, redirection_url: nil, icon_name: nil, verb: nil, display_type: nil, modal_configuration: {}, route_type: nil, partial: nil, execution_mode: :individual, &block)
|
296
297
|
bulk_action = CmAdmin::Models::BulkAction.new(
|
297
298
|
name:, display_name:, display_if:, modal_configuration:,
|
298
299
|
redirection_url:, icon_name:, action_type: :bulk_action,
|
300
|
+
execution_mode:,
|
299
301
|
verb:, display_type:, route_type:, partial:, &block
|
300
302
|
)
|
301
303
|
@available_actions << bulk_action
|
data/lib/cm_admin/version.rb
CHANGED