Class: ActiveRecord::Base::PromiscousOperation

Inherits:
Promiscuous::Publisher::Operation::NonPersistent show all
Defined in:
lib/promiscuous/publisher/operation/active_record.rb

Instance Attribute Summary

Attributes inherited from Promiscuous::Publisher::Operation::NonPersistent

#instances

Attributes inherited from Promiscuous::Publisher::Operation::Base

#operation

Instance Method Summary collapse

Methods inherited from Promiscuous::Publisher::Operation::NonPersistent

#execute_instrumented, #query_dependencies

Methods inherited from Promiscuous::Publisher::Operation::Base

_acquire_lock, #acquire_op_lock, #dependencies_for, #dependency_for_op_lock, #ensure_op_still_locked, #execute_instrumented, #explain_operation, #generate_payload, #get_new_op_lock, #increment_dependencies, lock_options, #on_rabbitmq_confirm, #payload_for, #publish_payload_in_rabbitmq_async, #publish_payload_in_redis, #query_dependencies, rabbitmq_staging_set_key, #record_timestamp, #recover_db_operation, recover_locks, recover_operation, recover_operation_from_lock, recover_payloads_for_rabbitmq, #recovering?, #recovery_payload, register_recovery_mechanism, #release_op_lock, run_recovery_mechanisms, #should_instrument_query?, #trace_operation, #write_dependencies

Constructor Details

#initialize(arel, name, binds, options = {}) ⇒ PromiscousOperation

Returns a new instance of PromiscousOperation.



136
137
138
139
140
141
142
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 136

def initialize(arel, name, binds, options={})
  super(options)
  @arel = arel
  @operation_name = name
  @binds = binds
  @connection = options[:connection]
end

Instance Method Details

#db_operation_and_selectObject



174
175
176
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 174

def db_operation_and_select
  raise
end

#ensure_transaction!Object



148
149
150
151
152
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 148

def ensure_transaction!
  if !transaction_context.in_transaction?
    raise "You need to write to the database within an ActiveRecord transaction"
  end
end

#execute(&db_operation) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 160

def execute(&db_operation)
  return db_operation.call unless model
  ensure_transaction!

  super do |query|
    query.non_instrumented { db_operation.call }
    query.instrumented do
      db_operation_and_select.tap do
        transaction_context.add_write_operation(self) if !@instances.empty?
      end
    end
  end
end

#modelObject



154
155
156
157
158
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 154

def model
  @model ||= @arel.ast.relation.engine
  @model = nil unless @model < Promiscuous::Publisher::Model::ActiveRecord
  @model
end

#operation_payloadsObject



178
179
180
181
182
183
184
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 178

def operation_payloads
  @instances.map do |instance|
    instance.promiscuous.payload(:with_attributes => self.operation.in?([:create, :update])).tap do |payload|
      payload[:operation] = self.operation
    end
  end
end

#transaction_contextObject



144
145
146
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 144

def transaction_context
  Promiscuous::Publisher::Context.current.transaction_context_of(:active_record)
end