lib/avo/base_action.rb in avo-0.5.0.beta7 vs lib/avo/base_action.rb in avo-0.5.0.beta8
- old
+ new
@@ -1,79 +1,82 @@
module Avo
class BaseAction
+ extend FieldsCollector
+ extend HasContext
+
class_attribute :name, default: self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
class_attribute :message
- class_attribute :confirm_text
- class_attribute :cancel_text
+ class_attribute :confirm_button_label
+ class_attribute :cancel_button_label
class_attribute :no_confirmation, default: false
- class_attribute :fields_loader
+ class_attribute :model
+ class_attribute :view
+ class_attribute :user
+ class_attribute :resource
+ class_attribute :fields
attr_accessor :response
attr_accessor :model
attr_accessor :resource
attr_accessor :user
+ attr_accessor :fields_loader
- class << self
- def fields(&block)
- self.fields_loader ||= Avo::Loaders::FieldsLoader.new
+ def initialize(model: nil, resource: nil, user: nil)
+ self.class.model = model if model.present?
+ self.class.resource = resource if resource.present?
+ self.class.user = user if user.present?
- yield(fields_loader)
- end
-
- def context
- App.context
- end
- end
-
- def initialize
self.class.message ||= I18n.t('avo.are_you_sure_you_want_to_run_this_option')
- self.class.confirm_text ||= I18n.t('avo.run')
- self.class.cancel_text ||= I18n.t('avo.cancel')
+ self.class.confirm_button_label ||= I18n.t('avo.run')
+ self.class.cancel_button_label ||= I18n.t('avo.cancel')
@response ||= {}
@response[:message_type] ||= :notice
@response[:message] ||= I18n.t('avo.action_ran_successfully')
end
- def get_fields(view_type: :table)
- get_field_definitions.map do |field|
- field.hydrate(action: self, model: @model)
- end
- .select do |field|
- field.can_see.present? ? field.can_see.call : true
- end
+ def context
+ self.class.context
end
def get_field_definitions
- return [] if self.class.fields_loader.blank?
+ return [] if self.class.fields.blank?
- self.class.fields_loader.bag.map do |field|
+ self.class.fields.map do |field|
field.hydrate(action: self)
end
end
- def hydrate(model: nil, resource: nil, user: nil)
- @model = model if model.present?
- @resource = resource if resource.present?
- @user = user if user.present?
+ def get_fields
+ get_field_definitions.map do |field|
+ field.hydrate(action: self, model: @model)
+ end
+ .select do |field|
+ field.visible?
+ end
+ end
- self
+ def get_attributes_for_action
+ get_fields.map do |field|
+ [field.id, field.value]
+ end
+ .to_h
end
- def handle_action(request, models, raw_fields)
+ def handle_action(models:, fields:)
avo_fields = get_fields.map { |field| [field.id, field] }.to_h
- if raw_fields.present?
- fields = raw_fields.to_unsafe_h.map do |name, value|
+ if fields.present?
+ processed_fields = fields.to_unsafe_h.map do |name, value|
[name, avo_fields[name.to_sym].resolve_attribute(value)]
end
- fields = fields.to_h
+ processed_fields = processed_fields.to_h
else
- fields = {}
+ processed_fields = {}
end
- result = self.handle request, models, fields
+ result = self.handle models: models, fields: processed_fields
self
end
def param_id