Module: ActiveEvents::InstanceMethods

Includes:
ActiveBunnyEvents
Defined in:
lib/active_events.rb

Instance Method Summary collapse

Instance Method Details

#active_event_createObject



107
108
109
110
111
# 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_destroyObject



121
122
123
124
125
126
127
# 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_updateObject



113
114
115
116
117
118
119
# 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_changesObject



103
104
105
# File 'lib/active_events.rb', line 103

def entity_changes
  respond_to?(:changes_to_save) ? changes_to_save : changes
end

#event_attributesObject



71
72
73
74
# 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_commentObject



92
93
94
# File 'lib/active_events.rb', line 92

def event_comment
  ""
end

#init_bunnyObject



96
97
98
99
100
101
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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