Module: HazWebhooks::Hook::ClassMethods

Defined in:
lib/haz_webhooks/hook.rb

Instance Method Summary collapse

Instance Method Details

#haz_webhooks(events) ⇒ Object

events is an array of strings that are the key for the event



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/haz_webhooks/hook.rb', line 10

def haz_webhooks(events)
  class_eval do
    has_many :webhooks, :as => :hook, :class_name => 'HazWebhooks::Webhook', :dependent => :delete_all
    @@events = events
    def webhook_events
      @@events
    end

    after_create :init_webhooks
    before_destroy :destroy_webhooks

    def init_webhooks
      @@events.each do |e|
        self.webhooks.create(:key => e, :active => true)
      end
    end

    def destroy_webhooks
      self.webhooks.clear
    end
  end
end