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
32 changes: 32 additions & 0 deletions app/models/concerns/fae/base_model_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@ module Fae
module BaseModelConcern
extend ActiveSupport::Concern
require 'csv'
require 'slack-notifier'

attr_accessor :filter

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?
jasonfine marked this conversation as resolved.
Show resolved Hide resolved
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?
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 +62,12 @@ def fae_form_manager_model_id
self.id
end

def send_slack(field_name_symbol)
notifier = Slack::Notifier.new ENV["SLACK_WEBHOOK_URL"]
notifier.ping "#{Rails.application.class.module_parent_name} - #{name} (#{self.class.name.constantize}) - #{field_name_symbol.to_s} set to '#{self.send(field_name_symbol)}'"

end

module ClassMethods
def for_fae_index
order(order_method)
Expand Down
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