Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack notify integration #605

Open
wants to merge 12 commits into
base: v3.1
Choose a base branch
from
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ PATH
sass (>= 3.4.0)
sass-rails (>= 5.0.7)
simple_form (<= 5.1)
slack-notifier
slim
uglifier

Expand Down Expand Up @@ -426,6 +427,7 @@ GEM
simple_form (5.1.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
slack-notifier (2.4.0)
slim (4.1.0)
temple (>= 0.7.6, < 0.9)
tilt (>= 2.0.6, < 2.1)
Expand Down
30 changes: 30 additions & 0 deletions app/models/concerns/fae/base_model_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ module BaseModelConcern
included do
include Fae::Trackable if Fae.track_changes
include Fae::Sortable
after_create :notify_initiation
before_save :notify_changes
end

def notify_changes
return unless notifiable_attributes.present?
notifiable_attributes.each do |field_name_symbol|
if self.send("#{field_name_symbol}_changed?") && self.send(field_name_symbol).present?
format_and_send_slack(field_name_symbol)
end
end
end

def notify_initiation
return unless notifiable_attributes.present?
notifiable_attributes.each do |field_name_symbol|
if self.send(field_name_symbol).present?
format_and_send_slack(field_name_symbol)
end
end
end

def notifiable_attributes
# override this method in your model
# array of attributes to notify if changed
end

def fae_display_field
Expand Down Expand Up @@ -36,6 +61,11 @@ def fae_form_manager_model_id
self.id
end

def format_and_send_slack(field_name_symbol)
message = "#{Rails.application.class.module_parent_name} - #{name} (#{self.class.name.constantize}) - #{field_name_symbol.to_s} set to '#{self.send(field_name_symbol)}'"
Fae::SlackNotification.new().send_slack(message: message)
end

module ClassMethods
def for_fae_index
order(order_method)
Expand Down
17 changes: 17 additions & 0 deletions app/services/fae/slack_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'slack-notifier'

module Fae
class SlackNotification

def send_slack(webhook: ENV["SLACK_WEBHOOK_URL"], message: nil)
if webhook.is_a?(String)
webhook = webhook.split(',')
end
webhook.each do |wh|
notifier = Slack::Notifier.new wh
notifier.ping message
end
end

end
end
1 change: 1 addition & 0 deletions fae.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Gem::Specification.new do |s|
s.add_dependency 'slim'
s.add_dependency 'devise-two-factor'
s.add_dependency 'rqrcode'
s.add_dependency 'slack-notifier'

s.add_development_dependency 'appraisal'
s.add_development_dependency 'better_errors'
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/models/wine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ def self.for_fae_index
order(:position)
end

def notifiable_attributes
[:on_stage, :on_prod, :description_en]
end

end