Sha256: 578736273717dacedbce69811fce1ded9499d6a09ddafc8f6f147d172900e112

Contents?: true

Size: 1.52 KB

Versions: 13

Compression:

Stored size: 1.52 KB

Contents

module Actions
  module Middleware
    class KeepCurrentTaxonomies < Dynflow::Middleware
      def delay(*args)
        pass(*args).tap { store_current_taxonomies }
      end

      def plan(*args)
        with_current_taxonomies do
          pass(*args).tap { store_current_taxonomies }
        end
      end

      def run(*args)
        restore_current_taxonomies { pass(*args) }
      end

      def finalize
        restore_current_taxonomies { pass }
      end

      def hook(*args)
        restore_current_taxonomies { pass(*args) }
      end

      private

      def with_current_taxonomies
        if current_taxonomies?
          yield
        else
          restore_current_taxonomies { yield }
        end
      end

      def store_current_taxonomies
        action.input[:current_organization_id] = Organization.current.try(:id)
        action.input[:current_location_id] = Location.current.try(:id)
      end

      def restore_current_taxonomies
        Organization.current = Organization.unscoped.find(action.input[:current_organization_id]) if action.input[:current_organization_id].present?
        Location.current = Location.unscoped.find(action.input[:current_location_id]) if action.input[:current_location_id].present?
        yield
      ensure
        Organization.current = nil
        Location.current = nil
      end

      def current_taxonomies?
        (Organization.current || action.input[:current_organization_id].nil?) &&
          (Location.current || action.input[:current_location_id].nil?)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
foreman-tasks-0.14.6 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.15.2 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.15.1 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.15.0 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.5 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.4 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.3 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.2 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.1 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.14.0 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.13.4 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.13.3 app/lib/actions/middleware/keep_current_taxonomies.rb
foreman-tasks-0.13.2 app/lib/actions/middleware/keep_current_taxonomies.rb