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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0637794147d28cbeb2b09c9162104aabb3c5b7f52e3fb9e42c6a291e89b8e5e
4
- data.tar.gz: 7971b87140570d9a486c65522fa762a960b5d5bc1e2cb392abcb36beec867003
3
+ metadata.gz: eca7a51066874791c62583654eea82878e1b84fefaca0ac6893972573fa21bdc
4
+ data.tar.gz: eb36d11d0a1d47f8c854fd79b19621148ef84ed45d2afcb72609441761c47f0b
5
5
  SHA512:
6
- metadata.gz: 5e61028998a91e7279541570fd4e881cdc9263f3bd89f03db97e0b057c6667e32f69481164cf294bd6c517a55ec34de5c744b50e98340192c56861325eb9a7bb
7
- data.tar.gz: 2cacfa59737f253733fe9b362464515f944e876e4e67b5e21f0895bd5691c6888c7a9d4013432d80b0ad24b04b4e9f52712786e445826ab539aef4d490a6a1cc
6
+ metadata.gz: 9c0596af7a585db67aa2f16a902f7765f94b6419fdff631efff2cdad9f6f9d885d61fe2ce55c783704c91b575919253b6cc3b504bfc4cba02fe945dbec139030
7
+ data.tar.gz: b1228ab04a4664b6b0452b83667e5bef30591e5787775002a66897db3b1a1145ebc00360abbffa9bd659f508b6fc99c1f4a96b364601564f038afcf7ad3cb0e7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cm-admin (2.4.4)
4
+ cm-admin (2.4.5)
5
5
  caxlsx_rails
6
6
  cocoon (~> 1.2.15)
7
7
  csv-importer (~> 0.8.2)
@@ -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).perform_bulk_action
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[:notice] = "#{@action.formatted_name} was successful"
118
- flash[:bulk_action_success] = "<b>#{@action.formatted_name} was successful</b>"
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 do |invalid_record|
122
- "<li>#{invalid_record.error_message}</li>"
123
- end.join
124
- flash[:alert] = "#{@action.formatted_name} was unsuccessful"
125
- flash[:bulk_action_error] = "<div>
126
- <div><b>#{@action.formatted_name} encountered the following errors:</b></div>
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
- format.html { redirect_to request.referrer }
143
+ end
144
+ format.html { redirect_to request.referrer}
130
145
  end
131
146
  end
132
147
  end
@@ -1,30 +1,51 @@
1
- class CmAdmin::BulkActionProcessor
2
- extend ActiveSupport::Concern
3
- attr_accessor :invalid_records
1
+ module CmAdmin
2
+ class BulkActionProcessor
3
+ extend ActiveSupport::Concern
4
+ attr_accessor :invalid_records, :success_records
4
5
 
5
- def initialize(current_action, model, params)
6
- @invalid_records = []
7
- @current_action = current_action
8
- @model = model
9
- @params = params
10
- end
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
- def perform_bulk_action
13
- @params[:selected_ids].split(',').each do |id|
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(id)
38
+ @current_action.code_block.call(@params[:selected_ids])
18
39
  @error_message = nil
19
40
  rescue NoMethodError, NameError => e
20
- @error_message = "#{e.message.slice(0..(e.message.index(' for')))} at #{ar_object.send(column_name)}"
41
+ @error_message = e.message.to_s
21
42
  rescue ActiveRecord::RecordInvalid => e
22
- @error_message = "#{e.message} at #{ar_object.send(column_name)}"
43
+ @error_message = e.message.to_s
23
44
  rescue StandardError => e
24
- @error_message = "#{ar_object.send(column_name)} - #{e.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
@@ -1,3 +1,3 @@
1
1
  module CmAdmin
2
- VERSION = '2.4.4'
2
+ VERSION = '2.4.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cm-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael