Skip to content

Instantly share code, notes, and snippets.

@pete2786
Last active May 30, 2016 08:38
Show Gist options
  • Save pete2786/80f3eb56e6168eafd949 to your computer and use it in GitHub Desktop.
Save pete2786/80f3eb56e6168eafd949 to your computer and use it in GitHub Desktop.
Slack Notifiable and Example
class ProjectComment < ActiveRecord::Base
include SlackNotifiable
belongs_to :user
belongs_to :project
has_one :organization, through: :project
def slack_message
"#{user.name} has commented on #{project.title}: #{description}"
end
end
require 'active_support/concern'
require 'slack-notifier'
module SlackNotifiable
extend ActiveSupport::Concern
included do
after_create :ping_slack
end
def ping_slack
return unless Rails.env.production? && respond_to?(:organization) && organization.slack_webhook_url
begin
Slack::Notifier.new(organization.slack_webhook_url, username: 'Notifier').ping(slack_message)
rescue Exception => e
# non-essential so do nothing
end
end
def slack_message
self.inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment