Class: Triggerable::Rules::Automation

Inherits:
Rule
  • Object
show all
Defined in:
lib/triggerable/rules/automation.rb

Instance Attribute Summary collapse

Attributes inherited from Rule

#actions, #condition, #model, #name

Instance Method Summary collapse

Constructor Details

#initialize(model, options, block) ⇒ Automation

Returns a new instance of Automation.



6
7
8
9
10
# File 'lib/triggerable/rules/automation.rb', line 6

def initialize model, options, block
  super(model, options, block)
  @pass_relation = options[:pass_relation]
  @unscoped = options[:unscoped]
end

Instance Attribute Details

#pass_relationObject

Returns the value of attribute pass_relation.



4
5
6
# File 'lib/triggerable/rules/automation.rb', line 4

def pass_relation
  @pass_relation
end

Instance Method Details

#execute!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/triggerable/rules/automation.rb', line 12

def execute!
  ids = ActiveRecord::Base.connection.execute(build_query).map { |r| r['id'] }
  models = @unscoped ? model.unscoped : model
  models = models.where(id: ids)

  Triggerable::Engine.log(:debug, "#{desc}: processing #{models.count} object(s)")

  if @pass_relation
    execute_on!(models)
  else
    models.each { |object| execute_on!(object) }
  end
end