Class: EasyAdmin::BatchActionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/easy_admin/batch_actions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#policy_for

Instance Method Details

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/easy_admin/batch_actions_controller.rb', line 7

def execute
  action_class_name = params[:action_class]
  execution_mode = params[:execution_mode]
  
  begin
    action_class = action_class_name.constantize
    action_instance = action_class.new
    
    # Set attributes for modal actions
    if execution_mode == 'modal'
      action_instance.assign_attributes(batch_action_params(action_class))
      
      unless action_instance.valid?
        respond_to do |format|
          format.turbo_stream do
            render turbo_stream: [
              turbo_stream.update("notifications",
                EasyAdmin::NotificationComponent.new(
                  type: :error,
                  message: action_instance.errors.full_messages.join(', '),
                  title: "Validation Error"
                ).call
              ),
            ]
          end
        end
        return
      end
    end
    
    # Set up the action instance with selected records and current user
    action_instance.selected_ids = @selected_records.pluck(:id)
    action_instance.current_user = current_admin_user
    action_instance.resource_class = @resource_class
    action_instance.params = batch_action_params(action_class).to_h
    # Execute the batch action
    result = action_instance.perform
    
    respond_to do |format|
      format.turbo_stream do
        if result[:success]
          # Update each affected row individually
          streams = [
            turbo_stream.update("notifications",
              EasyAdmin::NotificationComponent.new(
                type: :success,
                message: result[:message] || 'Action completed successfully',
                title: "Success"
              ).call
            )
          ]
          
          # Add individual row updates
          @selected_records.each do |record|
            # Reload record to get updated data
            record.reload
            streams << turbo_stream.replace(
              helpers.dom_id(record),
              EasyAdmin::Resources::TableRowComponent.new(
                record: record,
                resource_class: @resource_class
              ).call
            )
          end
          
          render turbo_stream: streams
        else
          render turbo_stream: [
            turbo_stream.update("notifications",
              EasyAdmin::NotificationComponent.new(
                type: :error,
                message: result[:message] || 'Action failed',
                title: "Error"
              ).call
            ),
          ]
        end
      end
    end
    
  rescue NameError => e
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.update("notifications",
          EasyAdmin::NotificationComponent.new(
            type: :error,
            message: 'Invalid action class',
            title: "Error"
          ).call
        )
      end
    end
  end
end

#formObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/easy_admin/batch_actions_controller.rb', line 102

def form
  action_class_name = params[:action_class]
  selected_ids = params[:selected_ids]&.split(',') || []
  
  begin
    action_class = action_class_name.constantize
    
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.update(
          'modal',
          render_batch_action_form(action_class, selected_ids).call
        )
      end
    end
    
  rescue NameError
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.update("notifications",
          EasyAdmin::NotificationComponent.new(
            type: :error,
            message: 'Invalid action class',
            title: "Error"
          ).call
        )
      end
    end
  end
end