Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'rails_best_practices/checks/check' | ||
|
||
module RailsBestPractices | ||
module Checks | ||
# Check a model file to make sure mail deliver method is in observer not callback. | ||
# | ||
# Implementation: | ||
# Record :after_create callback | ||
# Check method define, if it is a callback and call deliver_xxx message in method body, then it should use observer. | ||
class UseObserverCheck < Check | ||
|
||
def interesting_nodes | ||
[:defn, :call] | ||
end | ||
|
||
def interesting_files | ||
/models\/.*rb/ | ||
end | ||
|
||
def initialize | ||
super | ||
@callbacks = [] | ||
end | ||
|
||
def evaluate_start(node) | ||
if :after_create == node.message | ||
remember_callbacks(node) | ||
elsif :defn == node.node_type and @callbacks.include?(node.message_name.to_s) | ||
add_error "use observer" if use_observer?(node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def remember_callbacks(node) | ||
@callbacks << eval(node.arguments.to_ruby).to_s | ||
end | ||
|
||
def use_observer?(node) | ||
node.recursive_children do |child| | ||
return true if :call == child.node_type and :const == child.subject.node_type and child.message.to_s =~ /^deliver_/ | ||
end | ||
false | ||
end | ||
end | ||
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
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
44 changes: 44 additions & 0 deletions
44
spec/rails_best_practices/checks/use_observer_check_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require File.join(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
describe RailsBestPractices::Checks::UseObserverCheck do | ||
before(:each) do | ||
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseObserverCheck.new) | ||
end | ||
|
||
it "should use observer" do | ||
content =<<-EOF | ||
class Project < ActiveRecord::Base | ||
after_create :send_create_notification | ||
private | ||
def send_create_notification | ||
self.members.each do |member| | ||
ProjectMailer.deliver_notification(self, member) | ||
end | ||
end | ||
end | ||
EOF | ||
@runner.check('app/models/project.rb', content) | ||
errors = @runner.errors | ||
errors.should_not be_empty | ||
errors[0].to_s.should == "app/models/project.rb:6 - use observer" | ||
end | ||
|
||
it "should not use observer without callback" do | ||
content =<<-EOF | ||
class Project < ActiveRecord::Base | ||
private | ||
def send_create_notification | ||
self.members.each do |member| | ||
ProjectMailer.deliver_notification(self, member) | ||
end | ||
end | ||
end | ||
EOF | ||
@runner.check('app/models/project.rb', content) | ||
errors = @runner.errors | ||
errors.should be_empty | ||
end | ||
end |