Skip to content

Commit

Permalink
replace observers by moving functionality inside the observed object
Browse files Browse the repository at this point in the history
The observers should have been removed a long time ago and with rails 5 there is
rails/rails-observers#52
to consider as well.
  • Loading branch information
ulferts committed Sep 8, 2016
1 parent 59fcaf9 commit 1815147
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 195 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -34,7 +34,6 @@ gem 'rails', '~> 5.0.0'
gem 'actionpack-xml_parser'
gem 'activemodel-serializers-xml'
gem 'activerecord-session_store', '~> 1.0.0'
gem 'rails-observers', github: 'rails/rails-observers', ref: '3fe157d6'
gem 'responders', '~> 2.0'

gem 'coderay', '~> 1.1.0'
Expand Down
9 changes: 0 additions & 9 deletions Gemfile.lock
Expand Up @@ -39,14 +39,6 @@ GIT
rails-angular-xss (0.2.0.pre.pre)
rails (>= 5.0.0, < 5.1)

GIT
remote: git://github.com/rails/rails-observers.git
revision: 3fe157d6cbb5b5e767ded248009fc59443d63fa1
ref: 3fe157d6
specs:
rails-observers (0.1.3.alpha)
activemodel (>= 4.0, < 5.1)

GIT
remote: https://github.com/carrierwaveuploader/carrierwave
revision: b31f7ce006bade550be0ad946d0b993b799358e3
Expand Down Expand Up @@ -671,7 +663,6 @@ DEPENDENCIES
rails (~> 5.0.0)
rails-angular-xss!
rails-controller-testing!
rails-observers!
rails_12factor
rails_autolink (~> 1.1.6)
rdoc (>= 2.4.2)
Expand Down
15 changes: 15 additions & 0 deletions app/models/comment.rb
Expand Up @@ -33,11 +33,26 @@ class Comment < ActiveRecord::Base

validates :commented, :author, :comments, presence: true

after_create :send_news_comment_added_mail

def text
comments
end

def post!
save!
end

private

def send_news_comment_added_mail
return unless Setting.notified_events.include?('news_comment_added')

return unless commented.is_a?(News)

recipients = commented.recipients + commented.watcher_recipients
recipients.uniq.each do |user|
UserMailer.news_comment_added(user, self, User.current).deliver_now
end
end
end
42 changes: 0 additions & 42 deletions app/models/comment_observer.rb

This file was deleted.

17 changes: 15 additions & 2 deletions app/models/message.rb
Expand Up @@ -63,8 +63,9 @@ class Message < ActiveRecord::Base
validates_presence_of :board, :subject, :content
validates_length_of :subject, maximum: 255

after_create :add_author_as_watcher
after_create :update_last_reply_in_parent
after_create :add_author_as_watcher,
:update_last_reply_in_parent,
:send_message_posted_mail
after_update :update_ancestors
after_destroy :reset_counters

Expand Down Expand Up @@ -143,4 +144,16 @@ def add_author_as_watcher
watchers.reload
watcher_users.reload
end

def send_message_posted_mail
return unless Setting.notified_events.include?('message_posted')

to_mail = recipients +
root.watcher_recipients +
board.watcher_recipients

to_mail.uniq.each do |user|
UserMailer.message_posted(user, self, User.current).deliver_now
end
end
end
41 changes: 0 additions & 41 deletions app/models/message_observer.rb

This file was deleted.

11 changes: 10 additions & 1 deletion app/models/news.rb
Expand Up @@ -49,7 +49,8 @@ class News < ActiveRecord::Base

acts_as_watchable

after_create :add_author_as_watcher
after_create :add_author_as_watcher,
:send_news_added_mail

scope :visible, -> (*args) {
includes(:project)
Expand Down Expand Up @@ -100,4 +101,12 @@ def to_param
def add_author_as_watcher
Watcher.create(watchable: self, user: author)
end

def send_news_added_mail
if Setting.notified_events.include?('news_added')
recipients.uniq.each do |user|
UserMailer.news_added(user, self, User.current).deliver_now
end
end
end
end
38 changes: 0 additions & 38 deletions app/models/news_observer.rb

This file was deleted.

30 changes: 30 additions & 0 deletions app/models/wiki_content.rb
Expand Up @@ -38,6 +38,8 @@ class WikiContent < ActiveRecord::Base
attr_accessor :comments

before_save :comments_to_journal_notes
after_create :send_content_added_mail
after_update :send_content_updated_mail

acts_as_journalized

Expand Down Expand Up @@ -82,4 +84,32 @@ def version
def comments_to_journal_notes
add_journal author, comments
end

def send_content_added_mail
return unless Setting.notified_events.include?('wiki_content_added')

create_recipients.uniq.each do |user|
UserMailer.wiki_content_added(user, self, User.current).deliver_now
end
end

def send_content_updated_mail
return unless wiki_content.text_changed? &&
Setting.notified_events.include?('wiki_content_updated')

update_recipients.uniq.each do |user|
UserMailer.wiki_content_updated(user, self, User.current).deliver_now
end
end

def create_recipients
recipients +
page.wiki.watcher_recipients
end

def update_recipients
recipients +
page.wiki.watcher_recipients +
page.watcher_recipients
end
end
50 changes: 0 additions & 50 deletions app/models/wiki_content_observer.rb

This file was deleted.

7 changes: 0 additions & 7 deletions config/application.rb
Expand Up @@ -100,13 +100,6 @@ class Application < Rails::Application
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
config.active_record.observers = :message_observer,
:news_observer,
:wiki_content_observer,
:comment_observer

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
Expand Down
10 changes: 6 additions & 4 deletions spec/models/news_spec.rb
Expand Up @@ -41,6 +41,8 @@
}

let!(:news) { FactoryGirl.create(:news, project: project) }
let(:permissions) { [] }
let(:role) { FactoryGirl.build(:role, permissions: permissions) }

it_behaves_like 'acts_as_watchable included' do
let(:model_instance) { FactoryGirl.create(:news) }
Expand Down Expand Up @@ -106,10 +108,10 @@
describe '#save',
with_settings: { notified_events: %w(news_added) } do
it 'sends email notifications when created' do
user = FactoryGirl.create(:user)
become_member_with_permissions(project, user)
# reload
project.members(true)
FactoryGirl.create(:user,
member_in_project: project,
member_through_role: role)
project.members.reload

FactoryGirl.create(:news, project: project)
expect(ActionMailer::Base.deliveries.size).to eq(1)
Expand Down

0 comments on commit 1815147

Please sign in to comment.