Class: Avo::BaseAction

Inherits:
Object
  • Object
show all
Includes:
Concerns::HasItems
Defined in:
lib/avo/base_action.rb

Instance Attribute Summary collapse

Attributes included from Concerns::HasItems

#items_holder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasItems

#get_field, #get_field_definitions, #get_fields, #get_items, #get_preview_fields, #invalid_fields, #is_empty?, #items, #only_fields, #tab_groups, #tools, #visible_items

Constructor Details

#initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}) ⇒ BaseAction

Returns a new instance of BaseAction.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/avo/base_action.rb', line 64

def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {})
  self.class.record = record
  self.class.resource = resource
  self.class.user = user
  self.class.view = Avo::ViewInquirer.new(view)
  @arguments = arguments

  self.class.message ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option")
  self.class.confirm_button_label ||= I18n.t("avo.run")
  self.class.cancel_button_label ||= I18n.t("avo.cancel")

  self.items_holder = Avo::Resources::Items::Holder.new
  fields

  @response ||= {}
  @response[:messages] = []
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



23
24
25
# File 'lib/avo/base_action.rb', line 23

def arguments
  @arguments
end

#recordObject

Returns the value of attribute record.



20
21
22
# File 'lib/avo/base_action.rb', line 20

def record
  @record
end

#resourceObject

Returns the value of attribute resource.



21
22
23
# File 'lib/avo/base_action.rb', line 21

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



19
20
21
# File 'lib/avo/base_action.rb', line 19

def response
  @response
end

#userObject

Returns the value of attribute user.



22
23
24
# File 'lib/avo/base_action.rb', line 22

def user
  @user
end

Class Method Details

.form_data_attributesObject



39
40
41
42
43
44
45
46
# File 'lib/avo/base_action.rb', line 39

def form_data_attributes
  # We can't respond with a file download from Turbo se we disable it on the form
  if may_download_file
    {turbo: turbo || false, remote: false, action_target: :form}
  else
    {turbo: turbo, turbo_frame: :_top, action_target: :form}.compact
  end
end

.submit_button_data_attributesObject

We can’t respond with a file download from Turbo se we disable close the modal manually after a while (it’s a hack, we know)



49
50
51
52
53
54
55
# File 'lib/avo/base_action.rb', line 49

def submit_button_data_attributes
  if may_download_file
    {action: "click->modal#delayedClose"}
  else
    {}
  end
end

Instance Method Details

#action_nameObject



58
59
60
61
62
# File 'lib/avo/base_action.rb', line 58

def action_name
  return name if name.present?

  self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
end

#current_userObject



28
29
30
# File 'lib/avo/base_action.rb', line 28

def current_user
  Avo::Current.user
end

#download(path, filename) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/avo/base_action.rb', line 216

def download(path, filename)
  response[:type] = :download
  response[:path] = path
  response[:filename] = filename

  self
end

#error(text) ⇒ Object



166
167
168
169
170
# File 'lib/avo/base_action.rb', line 166

def error(text)
  add_message text, :error

  self
end

#fieldsObject

Blank method



83
84
# File 'lib/avo/base_action.rb', line 83

def fields
end

#get_attributes_for_actionObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/avo/base_action.rb', line 90

def get_attributes_for_action
  get_fields.map do |field|
    value = field.value || Avo::ExecutionContext.new(
      target: field.default,
      record: self.class.record,
      resource: self.class.resource,
      view: view
    ).handle

    [field.id, value]
  end.to_h
end

#get_messageObject



86
87
88
# File 'lib/avo/base_action.rb', line 86

def get_message
  Avo::ExecutionContext.new(target: self.class.message, record: self.class.record, resource: self.class.resource).handle
end

#handle_action(**args) ⇒ Object



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
132
133
134
# File 'lib/avo/base_action.rb', line 103

def handle_action(**args)
  processed_fields = if args[:fields].present?
    # Fetching the field definitions and not the actual fields (get_fields) because they will break if the user uses a `visible` block and adds a condition using the `params` variable. The params are different in the show method and the handle method.
    action_fields = get_field_definitions.map { |field| [field.id, field] }.to_h

    # For some fields, like belongs_to, the id and database_id differ (user vs user_id).
    # That's why we need to fetch the database_id for when we process the action.
    action_fields_by_database_id = action_fields.map do |id, value|
      [value.database_id.to_sym, value]
    end.to_h

    args[:fields].to_unsafe_h.map do |name, value|
      field = action_fields_by_database_id[name.to_sym]

      next if field.blank?

      [name, field.resolve_attribute(value)]
    end.reject(&:blank?).to_h
  else
    {}
  end

  handle(
    fields: processed_fields.with_indifferent_access,
    current_user: args[:current_user],
    resource: args[:resource],
    records: args[:query],
    query: args[:query]
  )

  self
end

#hydrate_fieldsObject

We’re overriding this method to hydrate with the proper resource attribute.



225
226
227
228
229
230
231
# File 'lib/avo/base_action.rb', line 225

def hydrate_fields
  fields.map do |field|
    field.hydrate(record: @record, view: @view, resource: resource)
  end

  self
end

#inform(text) ⇒ Object



172
173
174
175
176
# File 'lib/avo/base_action.rb', line 172

def inform(text)
  add_message text, :info

  self
end

#keep_modal_openObject



184
185
186
187
188
# File 'lib/avo/base_action.rb', line 184

def keep_modal_open
  response[:keep_modal_open] = true

  self
end

#param_idObject



156
157
158
# File 'lib/avo/base_action.rb', line 156

def param_id
  self.class.to_s
end

#redirect_to(path = nil, allow_other_host: nil, status: nil, &block) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/avo/base_action.rb', line 197

def redirect_to(path = nil, allow_other_host: nil, status: nil, &block)
  response[:type] = :redirect
  response[:allow_other_host] = allow_other_host
  response[:status] = status
  response[:path] = if block.present?
    block
  else
    path
  end

  self
end

#reloadObject



210
211
212
213
214
# File 'lib/avo/base_action.rb', line 210

def reload
  response[:type] = :reload

  self
end

#silentObject

Add a placeholder silent message from when a user wants to do a redirect action or something similar



191
192
193
194
195
# File 'lib/avo/base_action.rb', line 191

def silent
  add_message nil, :silent

  self
end

#succeed(text) ⇒ Object



160
161
162
163
164
# File 'lib/avo/base_action.rb', line 160

def succeed(text)
  add_message text, :success

  self
end

#visible_in_view(parent_resource: nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/avo/base_action.rb', line 136

def visible_in_view(parent_resource: nil)
  if visible.blank?
    # Hide on the :new view by default
    return false if view.new?

    # Show on all other views
    return true
  end

  # Run the visible block if available
  Avo::ExecutionContext.new(
    target: visible,
    params: params,
    parent_resource: parent_resource,
    resource: self.class.resource,
    view: self.class.view,
    arguments: arguments
  ).handle
end

#warn(text) ⇒ Object



178
179
180
181
182
# File 'lib/avo/base_action.rb', line 178

def warn(text)
  add_message text, :warning

  self
end