lib/avo/base_action.rb in avo-3.0.0.pre19 vs lib/avo/base_action.rb in avo-3.0.1.beta1

- old
+ new

@@ -23,11 +23,11 @@ attr_reader :arguments delegate :view, to: :class # TODO: find a differnet way to delegate this to the uninitialized Current variable delegate :context, to: Avo::Current - def current_user + def curent_user Avo::Current.user end delegate :params, to: Avo::Current delegate :view_context, to: Avo::Current delegate :avo, to: :view_context @@ -63,11 +63,11 @@ 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) + self.class.view = 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") @@ -99,45 +99,50 @@ [field.id, value] end.to_h end 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 + records, fields, current_user, resource = args.values_at(:records, :fields, :current_user, :resource) + # 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 + # 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| + if fields.present? + processed_fields = 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 + end + + processed_fields = processed_fields.reject(&:blank?).to_h else - {} + processed_fields = {} end - handle( + args = { fields: processed_fields.with_indifferent_access, - current_user: args[:current_user], - resource: args[:resource], - records: args[:query], - query: args[:query] - ) + current_user: current_user, + resource: resource + } + args[:records] = records + + handle(**args) + self end def visible_in_view(parent_resource: nil) if visible.blank? # Hide on the :new view by default - return false if view.new? + return false if view == :new # Show on all other views return true end