Class: EasyAdmin::BatchAction
- Inherits:
-
Object
- Object
- EasyAdmin::BatchAction
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
- Defined in:
- lib/easy_admin/batch_action.rb
Constant Summary collapse
- INSTANT =
Execution modes
:instant- MODAL =
Execute immediately (toggle, delete, etc.)
:modal
Instance Attribute Summary collapse
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#params ⇒ Object
Returns the value of attribute params.
-
#resource_class ⇒ Object
Returns the value of attribute resource_class.
-
#selected_ids ⇒ Object
Returns the value of attribute selected_ids.
Class Method Summary collapse
-
.field_config(field_name) ⇒ Object
Helper method to get field configuration.
- .form_field(name, type, options = {}) ⇒ Object
- .instant(label:, icon: nil, style: :primary, confirm: nil) ⇒ Object
-
.instant? ⇒ Boolean
Helper method to check if action is instant or modal.
- .modal(label:, icon: nil, style: :primary, title: nil, description: nil) ⇒ Object
- .modal? ⇒ Boolean
Instance Method Summary collapse
-
#authorized? ⇒ Boolean
Override for custom authorization.
-
#form_param(key) ⇒ Object
Helper method to access form parameters safely.
-
#initialize(attributes = {}) ⇒ BatchAction
constructor
Show form first, then execute.
-
#perform ⇒ Object
Main execution method - must be implemented by subclasses.
-
#records ⇒ Object
Get the selected records.
-
#success_message(count = nil) ⇒ Object
Default success message.
-
#valid_for_execution? ⇒ Boolean
Validation before execution.
Constructor Details
#initialize(attributes = {}) ⇒ BatchAction
Show form first, then execute
17 18 19 20 21 22 |
# File 'lib/easy_admin/batch_action.rb', line 17 def initialize(attributes = {}) super(attributes) @records = nil @selected_ids ||= [] @params ||= {} end |
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
7 8 9 |
# File 'lib/easy_admin/batch_action.rb', line 7 def current_user @current_user end |
#params ⇒ Object
Returns the value of attribute params.
7 8 9 |
# File 'lib/easy_admin/batch_action.rb', line 7 def params @params end |
#resource_class ⇒ Object
Returns the value of attribute resource_class.
7 8 9 |
# File 'lib/easy_admin/batch_action.rb', line 7 def resource_class @resource_class end |
#selected_ids ⇒ Object
Returns the value of attribute selected_ids.
7 8 9 |
# File 'lib/easy_admin/batch_action.rb', line 7 def selected_ids @selected_ids end |
Class Method Details
.field_config(field_name) ⇒ Object
Helper method to get field configuration
110 111 112 |
# File 'lib/easy_admin/batch_action.rb', line 110 def self.field_config(field_name) modal_form_fields&.find { |field| field[:name] == field_name } || {} end |
.form_field(name, type, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/easy_admin/batch_action.rb', line 64 def form_field(name, type, = {}) self.modal_form_fields ||= [] self.modal_form_fields << { name: name, type: type, options: } # Add attribute and validation dynamically attribute name, get_attribute_type(type), **.slice(:default) # Add validations if specified if [:required] validates name, presence: true end if [:format] validates name, format: [:format] end end |
.instant(label:, icon: nil, style: :primary, confirm: nil) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/easy_admin/batch_action.rb', line 46 def instant(label:, icon: nil, style: :primary, confirm: nil) self.execution_mode = INSTANT self.label = label self.icon = icon self.style = style self. = confirm end |
.instant? ⇒ Boolean
Helper method to check if action is instant or modal
115 116 117 |
# File 'lib/easy_admin/batch_action.rb', line 115 def self.instant? execution_mode == INSTANT end |
.modal(label:, icon: nil, style: :primary, title: nil, description: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/easy_admin/batch_action.rb', line 54 def modal(label:, icon: nil, style: :primary, title: nil, description: nil) self.execution_mode = MODAL self.label = label self.icon = icon self.style = style self.modal_title = title || label self.modal_description = description self.modal_form_fields = [] end |
.modal? ⇒ Boolean
119 120 121 |
# File 'lib/easy_admin/batch_action.rb', line 119 def self.modal? execution_mode == MODAL end |
Instance Method Details
#authorized? ⇒ Boolean
Override for custom authorization
40 41 42 |
# File 'lib/easy_admin/batch_action.rb', line 40 def true # Override in subclasses end |
#form_param(key) ⇒ Object
Helper method to access form parameters safely
130 131 132 |
# File 'lib/easy_admin/batch_action.rb', line 130 def form_param(key) params[key.to_s] || params[key.to_sym] end |
#perform ⇒ Object
Main execution method - must be implemented by subclasses
25 26 27 |
# File 'lib/easy_admin/batch_action.rb', line 25 def perform raise NotImplementedError, "Subclasses must implement #perform" end |
#records ⇒ Object
Get the selected records
35 36 37 |
# File 'lib/easy_admin/batch_action.rb', line 35 def records @records ||= resource_class.model_class.where(id: selected_ids) end |
#success_message(count = nil) ⇒ Object
Default success message
124 125 126 127 |
# File 'lib/easy_admin/batch_action.rb', line 124 def (count = nil) count ||= records.count "#{self.class.label} completed for #{count} item#{'s' if count != 1}" end |
#valid_for_execution? ⇒ Boolean
Validation before execution
30 31 32 |
# File 'lib/easy_admin/batch_action.rb', line 30 def valid_for_execution? selected_ids.present? && records.exists? end |