class Avo::ActionsController

def action_params

def action_params
  params.permit(:resource_name, :action_id, fields: {})
end

def handle

def handle
  resource_ids = action_params[:fields][:resource_ids].split(",")
  models = @resource.class.find_scope.find resource_ids
  fields = action_params[:fields].select do |key, value|
    key != "resource_ids"
  end
  args = {
    fields: fields
  }
  args[:models] = models unless @action.standalone
  performed_action = @action.handle_action(**args)
  respond performed_action.response
end

def respond(response)

def respond(response)
  response[:type] ||= :reload
  response[:message_type] ||= :notice
  response[:message] ||= I18n.t("avo.action_ran_successfully")
  if response[:type] == :download
    return send_data response[:path], filename: response[:filename]
  end
  respond_to do |format|
    format.html do
      if response[:type] == :redirect
        path = response[:path]
        if path.respond_to? :call
          path = instance_eval(&path)
        end
        redirect_to path, "#{response[:message_type]}": response[:message]
      elsif response[:type] == :reload
        redirect_back fallback_location: resources_path(@resource.model_class), "#{response[:message_type]}": response[:message]
      end
    end
  end
end

def set_action

def set_action
  action_class = params[:action_id].gsub("avo_actions_", "").camelize.safe_constantize
  if params[:id].present?
    model = @resource.class.find_scope.find params[:id]
  end
  @action = action_class.new(model: model, resource: resource, user: _current_user)
end

def show

def show
  @model = ActionModel.new @action.get_attributes_for_action
end