Class: Workarea::BulkAction

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/bulk_action.rb,
app/models/workarea/bulk_action/deletion.rb,
app/models/workarea/bulk_action/product_edit.rb,
app/models/workarea/bulk_action/sequential_product_edit.rb

Direct Known Subclasses

Deletion, ProductEdit, SequentialProductEdit

Defined Under Namespace

Classes: Deletion, ProductEdit, SequentialProductEdit

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Method Details

#act_on!(model) ⇒ Object

Raises:

  • (NotImplementedError)


28
29
30
# File 'app/models/workarea/bulk_action.rb', line 28

def act_on!(model)
  raise NotImplementedError
end

#admin_queryObject



66
67
68
# File 'app/models/workarea/bulk_action.rb', line 66

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

#completed!Object



32
33
34
# File 'app/models/workarea/bulk_action.rb', line 32

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

#completed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/workarea/bulk_action.rb', line 36

def completed?
  !!completed_at
end

#convert_query_to_idsObject

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/workarea/bulk_action.rb', line 51

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

#perform!Object



24
25
26
# File 'app/models/workarea/bulk_action.rb', line 24

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

#reset_to_default!Object



40
41
42
43
44
45
46
# File 'app/models/workarea/bulk_action.rb', line 40

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

  self
end