Class: Avo::BaseAction
- Inherits:
-
Object
- Object
- Avo::BaseAction
- Extended by:
- FieldsCollector, HasContext
- Defined in:
- lib/avo/base_action.rb
Instance Attribute Summary collapse
-
#fields_loader ⇒ Object
Returns the value of attribute fields_loader.
-
#model ⇒ Object
Returns the value of attribute model.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#response ⇒ Object
Returns the value of attribute response.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #action_name ⇒ Object
- #context ⇒ Object
- #download(path, filename) ⇒ Object
- #fail(text) ⇒ Object
- #get_attributes_for_action ⇒ Object
- #get_field_definitions ⇒ Object
- #get_fields ⇒ Object
- #handle_action(**args) ⇒ Object
-
#initialize(model: nil, resource: nil, user: nil, view: nil) ⇒ BaseAction
constructor
A new instance of BaseAction.
- #param_id ⇒ Object
- #redirect_to(path = nil, &block) ⇒ Object
- #reload ⇒ Object
- #succeed(text) ⇒ Object
- #visible_in_view ⇒ Object
Methods included from FieldsCollector
Constructor Details
#initialize(model: nil, resource: nil, user: nil, view: nil) ⇒ BaseAction
Returns a new instance of BaseAction.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/avo/base_action.rb', line 31 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. ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option") self.class. ||= I18n.t("avo.run") self.class. ||= I18n.t("avo.cancel") @response ||= {} @response[:message_type] ||= :notice @response[:message] ||= I18n.t("avo.action_ran_successfully") end |
Instance Attribute Details
#fields_loader ⇒ Object
Returns the value of attribute fields_loader.
23 24 25 |
# File 'lib/avo/base_action.rb', line 23 def fields_loader @fields_loader end |
#model ⇒ Object
Returns the value of attribute model.
20 21 22 |
# File 'lib/avo/base_action.rb', line 20 def model @model end |
#resource ⇒ Object
Returns the value of attribute resource.
21 22 23 |
# File 'lib/avo/base_action.rb', line 21 def resource @resource end |
#response ⇒ Object
Returns the value of attribute response.
19 20 21 |
# File 'lib/avo/base_action.rb', line 19 def response @response end |
#user ⇒ Object
Returns the value of attribute user.
22 23 24 |
# File 'lib/avo/base_action.rb', line 22 def user @user end |
Instance Method Details
#action_name ⇒ Object
25 26 27 28 29 |
# File 'lib/avo/base_action.rb', line 25 def action_name return name if name.present? self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true) end |
#context ⇒ Object
46 47 48 |
# File 'lib/avo/base_action.rb', line 46 def context self.class.context end |
#download(path, filename) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/avo/base_action.rb', line 142 def download(path, filename) response[:type] = :download response[:path] = path response[:filename] = filename self end |
#fail(text) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/avo/base_action.rb', line 118 def fail(text) response[:message_type] = :alert response[:message] = text self end |
#get_attributes_for_action ⇒ Object
67 68 69 70 71 72 |
# File 'lib/avo/base_action.rb', line 67 def get_attributes_for_action get_fields.map do |field| [field.id, field.value] end .to_h end |
#get_field_definitions ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/avo/base_action.rb', line 50 def get_field_definitions return [] if self.class.fields.blank? self.class.fields.map do |field| field.hydrate(action: self) end end |
#get_fields ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/avo/base_action.rb', line 58 def get_fields get_field_definitions.map do |field| field.hydrate(action: self, model: @model) end .select do |field| field.visible? end end |
#handle_action(**args) ⇒ Object
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 |
# File 'lib/avo/base_action.rb', line 74 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 |
#param_id ⇒ Object
107 108 109 |
# File 'lib/avo/base_action.rb', line 107 def param_id self.class.to_s.demodulize.underscore.tr "/", "_" end |
#redirect_to(path = nil, &block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/avo/base_action.rb', line 125 def redirect_to(path = nil, &block) response[:type] = :redirect response[:path] = if block.present? block else path end self end |
#reload ⇒ Object
136 137 138 139 140 |
# File 'lib/avo/base_action.rb', line 136 def reload response[:type] = :reload self end |
#succeed(text) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/avo/base_action.rb', line 111 def succeed(text) response[:message_type] = :notice response[:message] = text self end |
#visible_in_view ⇒ Object
101 102 103 104 105 |
# File 'lib/avo/base_action.rb', line 101 def visible_in_view return true unless visible.present? instance_exec(resource: self.class.resource, view: view, &visible) end |