Class: EasyAdmin::BatchAction

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/easy_admin/batch_action.rb

Constant Summary collapse

INSTANT =

Execution modes

:instant
:modal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  self.modal_form_fields ||= []
  self.modal_form_fields << { 
    name: name, 
    type: type, 
    options: options 
  }
  
  # Add attribute and validation dynamically
  attribute name, get_attribute_type(type), **options.slice(:default)
  
  # Add validations if specified
  if options[:required]
    validates name, presence: true
  end
  
  if options[:format]
    validates name, format: options[: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_message = confirm
end

.instant? ⇒ Boolean

Helper method to check if action is instant or modal

Returns:

  • (Boolean)


115
116
117
# File 'lib/easy_admin/batch_action.rb', line 115

def self.instant?
  execution_mode == INSTANT
end


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

Returns:

  • (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

Returns:

  • (Boolean)


40
41
42
# File 'lib/easy_admin/batch_action.rb', line 40

def authorized?
  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

Raises:

  • (NotImplementedError)


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 success_message(count = nil)
  count ||= records.count
  "#{self.class.label} completed for #{count} item#{'s' if count != 1}"
end

#valid_for_execution? ⇒ Boolean

Validation before execution

Returns:

  • (Boolean)


30
31
32
# File 'lib/easy_admin/batch_action.rb', line 30

def valid_for_execution?
  selected_ids.present? && records.exists?
end