Sha256: 6437f72a092ede54f8341fcfe78a0862e29743d8a12dba895d18a2b9d778736e

Contents?: true

Size: 1.76 KB

Versions: 24

Compression:

Stored size: 1.76 KB

Contents

module Actions

  class BulkAction < Actions::ActionWithSubPlans
    # == Parameters:
    # actions_class::
    #   Class of action to trigger on targets
    # targets::
    #   Array of objects on which the action_class should be triggered
    # *args::
    #   Arguments that all the targets share
    def plan(action_class, targets, *args)
      check_targets!(targets)
      plan_self(:action_class => action_class.to_s,
                :target_ids => targets.map(&:id),
                :target_class => targets.first.class.to_s,
                :args => args)
    end

    def run(event = nil)
      super unless event == Dynflow::Action::Skip
    end

    def humanized_name
      if task.sub_tasks.first
         task.sub_tasks.first.humanized[:action]
      else
        _("Bulk action")
      end
    end

    def rescue_strategy
      Dynflow::Action::Rescue::Skip
    end

    def humanized_input
      a_sub_task = task.sub_tasks.first
      if a_sub_task
        [a_sub_task.humanized[:action].to_s.downcase] +
            Array(a_sub_task.humanized[:input]) + ['...']
      end
    end

    # @api override when the logic for the initiation of the subtasks
    #      is different from the default one
    def create_sub_plans
      action_class = input[:action_class].constantize
      target_class = input[:target_class].constantize
      targets = target_class.where(:id => input[:target_ids])

      targets.map do |target|
        trigger(action_class, target, *input[:args])
      end
    end

    def check_targets!(targets)
      if targets.empty?
        fail ::Foreman::Exception.new(N_("Empty bulk action"))
      end
      if targets.map(&:class).uniq.length > 1
        fail ::Foreman::Exception.new(N_("The targets are of different types"))
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
foreman-tasks-0.7.7 app/lib/actions/bulk_action.rb
foreman-tasks-0.7.6 app/lib/actions/bulk_action.rb
foreman-tasks-0.7.5 app/lib/actions/bulk_action.rb
foreman-tasks-0.7.4 app/lib/actions/bulk_action.rb