module ActiveEvents::InstanceMethods

Public Instance Methods

active_event_create() click to toggle source
# File lib/active_events.rb, line 107
def active_event_create
  init_bunny
  event = {event: 'create', changes: event_attributes, comment: event_comment}
  @bunny.publish CreateEvent.new event
end
active_event_destroy() click to toggle source
# File lib/active_events.rb, line 121
def active_event_destroy
  unless new_record?
    init_bunny
    event = {event: 'destroy', changes: event_attributes, comment: event_comment}
    @bunny.publish DestroyEvent.new event
  end
end
active_event_update() click to toggle source
# File lib/active_events.rb, line 113
def active_event_update
  unless (changes = entity_changes).empty?
    init_bunny
    event = {event: 'update', changes: changes, comment: event_comment}
    @bunny.publish UpdateEvent.new event
  end
end
entity_changes() click to toggle source
# File lib/active_events.rb, line 103
def entity_changes
  respond_to?(:changes_to_save) ? changes_to_save : changes
end
event_attributes() click to toggle source
# File lib/active_events.rb, line 71
def event_attributes
  event_attributes = attributes.except(*self.class.skipped_columns)
  normalize_enum_changes(event_attributes)
end
event_comment() click to toggle source
# File lib/active_events.rb, line 92
def event_comment
  ""
end
init_bunny() click to toggle source
# File lib/active_events.rb, line 96
def init_bunny
  return if @bunny.instance_of? BunnyEvents

  @bunny = BunnyEvents.new
  @bunny.init Bunny.new(ENV['AMQP_CONNECTION']).start
end
normalize_enum_changes(changes) click to toggle source
# File lib/active_events.rb, line 76
def normalize_enum_changes(changes)
  self.class.defined_enums.each do |name, values|
    if changes.has_key?(name)
      changes[name] = \
          if changes[name].is_a?(Array)
                                      changes[name].map { |v| values[v] }
          elsif rails_below?('5.0')
            changes[name]
          else
            values[changes[name]]
          end
    end
  end
  changes
end