Last active
May 30, 2016 08:38
-
-
Save pete2786/80f3eb56e6168eafd949 to your computer and use it in GitHub Desktop.
Slack Notifiable and Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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