rails_best_practices 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -43,6 +43,7 @@ NeedlessDeepNestingCheck: { nested_count: 2 }
43
43
  NotUseDefaultRouteCheck: { }
44
44
  KeepFindersOnTheirOwnModelCheck: { }
45
45
  LawOfDemeterCheck: { }
46
+ UseObserverCheck: { }
46
47
  </code></pre>
47
48
 
48
49
  *************************************************
@@ -73,7 +74,7 @@ h2. Progress
73
74
  ## DRY: metaprogramming
74
75
  ## Extract into Module
75
76
  ## Extract to composed class
76
- ## Use Observer
77
+ ## [-Use Observer-]
77
78
 
78
79
  * Lesson 4. Migration
79
80
  ## Isolating Seed Data
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -2,6 +2,11 @@ require 'rails_best_practices/checks/check'
2
2
 
3
3
  module RailsBestPractices
4
4
  module Checks
5
+ # Check to make sure not avoid the law of demeter.
6
+ #
7
+ # Implementation:
8
+ # 1. check all models to record belongs_to associations
9
+ # 2. check if calling belongs_to association's method or attribute
5
10
  class LawOfDemeterCheck < Check
6
11
 
7
12
  def interesting_nodes
@@ -0,0 +1,47 @@
1
+ require 'rails_best_practices/checks/check'
2
+
3
+ module RailsBestPractices
4
+ module Checks
5
+ # Check a model file to make sure mail deliver method is in observer not callback.
6
+ #
7
+ # Implementation:
8
+ # Record :after_create callback
9
+ # Check method define, if it is a callback and call deliver_xxx message in method body, then it should use observer.
10
+ class UseObserverCheck < Check
11
+
12
+ def interesting_nodes
13
+ [:defn, :call]
14
+ end
15
+
16
+ def interesting_files
17
+ /models\/.*rb/
18
+ end
19
+
20
+ def initialize
21
+ super
22
+ @callbacks = []
23
+ end
24
+
25
+ def evaluate_start(node)
26
+ if :after_create == node.message
27
+ remember_callbacks(node)
28
+ elsif :defn == node.node_type and @callbacks.include?(node.message_name.to_s)
29
+ add_error "use observer" if use_observer?(node)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def remember_callbacks(node)
36
+ @callbacks << eval(node.arguments.to_ruby).to_s
37
+ end
38
+
39
+ def use_observer?(node)
40
+ node.recursive_children do |child|
41
+ return true if :call == child.node_type and :const == child.subject.node_type and child.message.to_s =~ /^deliver_/
42
+ end
43
+ false
44
+ end
45
+ end
46
+ end
47
+ end
@@ -12,3 +12,4 @@ require 'rails_best_practices/checks/needless_deep_nesting_check'
12
12
  require 'rails_best_practices/checks/not_use_default_route_check'
13
13
  require 'rails_best_practices/checks/keep_finders_on_their_own_model_check'
14
14
  require 'rails_best_practices/checks/law_of_demeter_check'
15
+ require 'rails_best_practices/checks/use_observer_check'
@@ -81,6 +81,12 @@ class Sexp
81
81
  self[3]
82
82
  end
83
83
  end
84
+
85
+ def message_name
86
+ if :defn == node_type
87
+ self[1]
88
+ end
89
+ end
84
90
 
85
91
  def method_body
86
92
  if :block == node_type
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_best_practices}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
12
- s.date = %q{2009-11-07}
12
+ s.date = %q{2009-11-08}
13
13
  s.default_executable = %q{rails_best_practices}
14
14
  s.description = %q{check rails files according to ihower's presentation 'rails best practices'}
15
15
  s.email = %q{flyerhzm@gmail.com}
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb",
42
42
  "lib/rails_best_practices/checks/use_model_association_check.rb",
43
43
  "lib/rails_best_practices/checks/use_model_callback_check.rb",
44
+ "lib/rails_best_practices/checks/use_observer_check.rb",
44
45
  "lib/rails_best_practices/checks/use_scope_access_check.rb",
45
46
  "lib/rails_best_practices/command.rb",
46
47
  "lib/rails_best_practices/core.rb",
@@ -64,6 +65,7 @@ Gem::Specification.new do |s|
64
65
  "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
65
66
  "spec/rails_best_practices/checks/use_model_association_check_spec.rb",
66
67
  "spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
68
+ "spec/rails_best_practices/checks/use_observer_check_spec.rb",
67
69
  "spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
68
70
  "spec/spec.opts",
69
71
  "spec/spec_helper.rb"
@@ -85,6 +87,7 @@ Gem::Specification.new do |s|
85
87
  "spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
86
88
  "spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
87
89
  "spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
90
+ "spec/rails_best_practices/checks/use_observer_check_spec.rb",
88
91
  "spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
89
92
  "spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
90
93
  "spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
@@ -12,3 +12,4 @@ NeedlessDeepNestingCheck: { nested_count: 2 }
12
12
  NotUseDefaultRouteCheck: { }
13
13
  KeepFindersOnTheirOwnModelCheck: { }
14
14
  LawOfDemeterCheck: { }
15
+ UseObserverCheck: { }
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe RailsBestPractices::Checks::UseObserverCheck do
4
+ before(:each) do
5
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseObserverCheck.new)
6
+ end
7
+
8
+ it "should use observer" do
9
+ content =<<-EOF
10
+ class Project < ActiveRecord::Base
11
+ after_create :send_create_notification
12
+
13
+ private
14
+
15
+ def send_create_notification
16
+ self.members.each do |member|
17
+ ProjectMailer.deliver_notification(self, member)
18
+ end
19
+ end
20
+ end
21
+ EOF
22
+ @runner.check('app/models/project.rb', content)
23
+ errors = @runner.errors
24
+ errors.should_not be_empty
25
+ errors[0].to_s.should == "app/models/project.rb:6 - use observer"
26
+ end
27
+
28
+ it "should not use observer without callback" do
29
+ content =<<-EOF
30
+ class Project < ActiveRecord::Base
31
+ private
32
+
33
+ def send_create_notification
34
+ self.members.each do |member|
35
+ ProjectMailer.deliver_notification(self, member)
36
+ end
37
+ end
38
+ end
39
+ EOF
40
+ @runner.check('app/models/project.rb', content)
41
+ errors = @runner.errors
42
+ errors.should be_empty
43
+ end
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_best_practices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-07 00:00:00 +08:00
12
+ date: 2009-11-08 00:00:00 +08:00
13
13
  default_executable: rails_best_practices
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -64,6 +64,7 @@ files:
64
64
  - lib/rails_best_practices/checks/replace_complex_creation_with_factory_method_check.rb
65
65
  - lib/rails_best_practices/checks/use_model_association_check.rb
66
66
  - lib/rails_best_practices/checks/use_model_callback_check.rb
67
+ - lib/rails_best_practices/checks/use_observer_check.rb
67
68
  - lib/rails_best_practices/checks/use_scope_access_check.rb
68
69
  - lib/rails_best_practices/command.rb
69
70
  - lib/rails_best_practices/core.rb
@@ -87,6 +88,7 @@ files:
87
88
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
88
89
  - spec/rails_best_practices/checks/use_model_association_check_spec.rb
89
90
  - spec/rails_best_practices/checks/use_model_callback_check_spec.rb
91
+ - spec/rails_best_practices/checks/use_observer_check_spec.rb
90
92
  - spec/rails_best_practices/checks/use_scope_access_check_spec.rb
91
93
  - spec/spec.opts
92
94
  - spec/spec_helper.rb
@@ -130,6 +132,7 @@ test_files:
130
132
  - spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
131
133
  - spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
132
134
  - spec/rails_best_practices/checks/nested_model_forms_check_spec.rb
135
+ - spec/rails_best_practices/checks/use_observer_check_spec.rb
133
136
  - spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
134
137
  - spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb
135
138
  - spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb