Sha256: c4f2673c3807527d6e991de73917a3b201eca3b55321749a1bc942b7651a1a50

Contents?: true

Size: 1.95 KB

Versions: 62

Compression:

Stored size: 1.95 KB

Contents

module Workarea
  class BulkAction
    include ApplicationDocument

    field :query_id, type: String
    field :ids, type: Array, default: []
    field :exclude_ids, type: Array, default: []
    field :completed_at, type: Time

    before_validation :clean_blank_ids

    validate :something_to_act_on

    before_save :convert_query_to_ids

    delegate :params, :count, to: :admin_query

    index({ _type: 1 })
    index(
      { created_at: 1 },
      { expire_after_seconds: Workarea.config.bulk_action_expiration.to_i }
    )

    def perform!
      admin_query.each { |m| act_on!(m) }
    end

    def act_on!(model)
      raise NotImplementedError
    end

    def completed!
      update_attribute(:completed_at, Time.current)
    end

    def completed?
      !!completed_at
    end

    def reset_to_default!
      attributes
        .except(*BulkAction.fields.keys)
        .each { |a, _| send("reset_#{a}_to_default!") }

      self
    end

    # For reasonably sized queries, we want to store the ids of the results
    # to maintain the original result set acted on in the case that an
    # edit removes a result from the query during the bulk action.
    def convert_query_to_ids
      return if ids.present? || admin_query.results.total_pages > 10

      self.ids =
        Array.new(admin_query.results.total_pages) do |page|
          page_params = admin_query.params.merge(page: page + 1)
          page_search = admin_query.query.class.new(page_params)

          page_search.results.map do |model|
            id = model.to_global_id.to_param
            id unless id.in?(exclude_ids)
          end
        end.flatten.compact
    end

    def admin_query
      @admin_query ||= AdminQueryOperation.new(attributes)
    end

    private

    def something_to_act_on
      if query_id.blank? && ids.blank?
        errors.add(:base, I18n.t('workarea.errors.messages.nothing_to_act_on'))
      end
    end

    def clean_blank_ids
      ids.reject!(&:blank?)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.27 app/models/workarea/bulk_action.rb
workarea-core-3.5.4 app/models/workarea/bulk_action.rb
workarea-core-3.4.26 app/models/workarea/bulk_action.rb
workarea-core-3.5.3 app/models/workarea/bulk_action.rb
workarea-core-3.4.25 app/models/workarea/bulk_action.rb
workarea-core-3.5.2 app/models/workarea/bulk_action.rb
workarea-core-3.4.24 app/models/workarea/bulk_action.rb
workarea-core-3.5.1 app/models/workarea/bulk_action.rb
workarea-core-3.4.23 app/models/workarea/bulk_action.rb
workarea-core-3.4.22 app/models/workarea/bulk_action.rb
workarea-core-3.5.0 app/models/workarea/bulk_action.rb
workarea-core-3.4.21 app/models/workarea/bulk_action.rb
workarea-core-3.5.0.beta.1 app/models/workarea/bulk_action.rb
workarea-core-3.4.20 app/models/workarea/bulk_action.rb
workarea-core-3.4.19 app/models/workarea/bulk_action.rb
workarea-core-3.4.18 app/models/workarea/bulk_action.rb
workarea-core-3.4.17 app/models/workarea/bulk_action.rb
workarea-core-3.4.16 app/models/workarea/bulk_action.rb
workarea-core-3.4.15 app/models/workarea/bulk_action.rb
workarea-core-3.4.14 app/models/workarea/bulk_action.rb