Class: RailsPipeline::SubscriberHandler::ActiveRecordCRUD

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/rails-pipeline/handlers/activerecord_crud.rb

Instance Attribute Summary

Attributes inherited from BaseHandler

#envelope, #event_type, #payload, #target_class

Instance Method Summary collapse

Methods inherited from BaseHandler

#initialize

Constructor Details

This class inherits a constructor from RailsPipeline::SubscriberHandler::BaseHandler

Instance Method Details

#_attributes(payload) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rails-pipeline/handlers/activerecord_crud.rb', line 24

def _attributes(payload)
  attributes_hash = payload.to_hash
  attributes_hash.each do |attribute_name, value|
    if attribute_name.match /_at$/
      attributes_hash[attribute_name] = Time.at(value).to_datetime
    end
  end
  return attributes_hash
end

#handle_payloadObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails-pipeline/handlers/activerecord_crud.rb', line 4

def handle_payload
  begin
    case event_type
    when RailsPipeline::EncryptedMessage::EventType::CREATED
      return target_class.create!(_attributes(payload), without_protection: true)
    when RailsPipeline::EncryptedMessage::EventType::UPDATED
      # We might want to allow confiugration of the primary key field
      object = target_class.find(payload.id)
      object.update_attributes!(_attributes(payload), without_protection: true)
      return object
    when RailsPipeline::EncryptedMessage::EventType::DELETED
      object = target_class.find(payload.id)
      object.destroy
      return object
    end
  rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound => e
    RailsPipeline.logger.error "Could not handle payload: #{payload.inspect}, event_type: #{event_type}"
  end
end