Module: Notifyor::Plugin::ClassMethods

Defined in:
lib/notifyor/plugin.rb

Instance Method Summary collapse

Instance Method Details

#append_callbacks(configuration, publish_channels) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/notifyor/plugin.rb', line 22

def append_callbacks(configuration, publish_channels)
  publish_channels.each do |channel|
    configuration[:only].each do |action|
      case action
        when :create
          self.after_commit -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:create].call(self) }, on: :create, if: -> { configuration[:only].include? :create }
        when :update
          self.after_commit -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:update].call(self) }, on: :update, if: -> { configuration[:only].include? :update }
        when :destroy
          self.before_destroy -> { ::Notifyor.configuration.redis_connection.publish channel, configuration[:messages][:destroy].call(self) }, if: -> { configuration[:only].include? :destroy }
        else
          #nop
      end
    end
  end
end

#configure_plugin(options = {}) ⇒ Object



16
17
18
19
20
# File 'lib/notifyor/plugin.rb', line 16

def configure_plugin(options = {})
  configuration = default_configuration.deep_merge(options)
  publish_channels = configuration[:channels] || ['notifyor']
  append_callbacks(configuration, publish_channels)
end

#default_configurationObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/notifyor/plugin.rb', line 39

def default_configuration
  {
      only: [:create, :destroy, :update],
      messages: {
          create: -> (model) { I18n.t('notifyor.model.create') },
          update: -> (model) { I18n.t('notifyor.model.update') },
          destroy: -> (model) { I18n.t('notifyor.model.destroy') }
      }
  }
end

#notifyor(options = {}) ⇒ Object



12
13
14
# File 'lib/notifyor/plugin.rb', line 12

def notifyor(options = {})
  configure_plugin(options)
end