Module: ActiveEvents::InstanceMethods

Includes:
ActiveBunnyEvents
Defined in:
lib/active_events.rb

Instance Method Summary collapse

Instance Method Details

#active_event_createObject



104
105
106
107
108
# File 'lib/active_events.rb', line 104

def active_event_create
  init_bunny
  event = {event: 'create', changes: event_attributes, comment: event_comment}
  @bunny.publish CreateEvent.new event
end

#active_event_destroyObject



118
119
120
121
122
123
124
# File 'lib/active_events.rb', line 118

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



110
111
112
113
114
115
116
# File 'lib/active_events.rb', line 110

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



100
101
102
# File 'lib/active_events.rb', line 100

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

#event_attributesObject



68
69
70
71
# File 'lib/active_events.rb', line 68

def event_attributes
  event_attributes = attributes.except(*self.class.skipped_columns)
  normalize_enum_changes(event_attributes)
end

#event_commentObject



89
90
91
# File 'lib/active_events.rb', line 89

def event_comment
  ""
end

#init_bunnyObject



93
94
95
96
97
98
# File 'lib/active_events.rb', line 93

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_events.rb', line 73

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