Class: Avo::BaseAction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::HasFields

#fields, #get_field, #get_field_definitions, #get_fields, #get_items, #get_tabs, #hydrate_fields, #tab_groups, #tools

Constructor Details

#initialize(model: nil, resource: nil, user: nil, view: nil) ⇒ BaseAction

Returns a new instance of BaseAction.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/avo/base_action.rb', line 59

def initialize(model: nil, resource: nil, user: nil, view: nil)
  self.class.model = model if model.present?
  self.class.resource = resource if resource.present?
  self.class.user = user if user.present?
  self.class.view = view if view.present?

  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")

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

Instance Attribute Details

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#resourceObject

Returns the value of attribute resource.



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

def resource
  @resource
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.form_data_attributesObject



34
35
36
37
38
39
40
41
# File 'lib/avo/base_action.rb', line 34

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: false, remote: false}
  else
    {"turbo-frame": "_top", "action-target": "form"}
  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)



44
45
46
47
48
49
50
# File 'lib/avo/base_action.rb', line 44

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

Instance Method Details

#action_nameObject



53
54
55
56
57
# File 'lib/avo/base_action.rb', line 53

def action_name
  return name if name.present?

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

#download(path, filename) ⇒ Object



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

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

  self
end

#fail(text) ⇒ Object



128
129
130
131
132
# File 'lib/avo/base_action.rb', line 128

def fail(text)
  add_message text, :error

  self
end

#get_attributes_for_actionObject



73
74
75
76
77
78
# File 'lib/avo/base_action.rb', line 73

def get_attributes_for_action
  get_fields.map do |field|
    [field.id, field.value]
  end
    .to_h
end

#handle_action(**args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/avo/base_action.rb', line 80

def handle_action(**args)
  models, fields, current_user, resource = args.values_at(:models, :fields, :current_user, :resource)
  avo_fields = get_fields.map { |field| [field.id, field] }.to_h

  if fields.present?
    processed_fields = fields.to_unsafe_h.map do |name, value|
      [name, avo_fields[name.to_sym].resolve_attribute(value)]
    end

    processed_fields = processed_fields.to_h
  else
    processed_fields = {}
  end

  args = {
    fields: processed_fields,
    current_user: current_user,
    resource: resource
  }

  args[:models] = models unless standalone

  handle(**args)

  self
end

#inform(text) ⇒ Object



134
135
136
137
138
# File 'lib/avo/base_action.rb', line 134

def inform(text)
  add_message text, :info

  self
end

#param_idObject



118
119
120
# File 'lib/avo/base_action.rb', line 118

def param_id
  self.class.to_s.demodulize.underscore.tr "/", "_"
end

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/avo/base_action.rb', line 153

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

  self
end

#reloadObject



164
165
166
167
168
# File 'lib/avo/base_action.rb', line 164

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



147
148
149
150
151
# File 'lib/avo/base_action.rb', line 147

def silent
  add_message nil, :silent

  self
end

#succeed(text) ⇒ Object



122
123
124
125
126
# File 'lib/avo/base_action.rb', line 122

def succeed(text)
  add_message text, :success

  self
end

#visible_in_viewObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/avo/base_action.rb', line 107

def visible_in_view
  # Run the visible block if available
  return instance_exec(resource: self.class.resource, view: view, &visible) if visible.present?

  # Hide on the :new view by default
  return false if view == :new

  # Show on all other views
  true
end

#warn(text) ⇒ Object



140
141
142
143
144
# File 'lib/avo/base_action.rb', line 140

def warn(text)
  add_message text, :warning

  self
end