rails_best_practices 0.3.23 → 0.3.24

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -25,7 +25,7 @@ Jeweler::Tasks.new do |gemspec|
25
25
  gemspec.summary = "check rails files according to ihower's presentation 'rails best practices'"
26
26
  gemspec.description = "check rails files according to ihower's presentation 'rails best practices'"
27
27
  gemspec.email = "flyerhzm@gmail.com"
28
- gemspec.homepage = "http://github.com/flyerhzm/rails_best_practices"
28
+ gemspec.homepage = "http://rails-bestpractices.com"
29
29
  gemspec.authors = ["Richard Huang"]
30
30
  gemspec.add_dependency 'ruby_parser', '>= 2.0.4'
31
31
  gemspec.add_dependency 'ruby2ruby', '>= 1.2.4'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.23
1
+ 0.3.24
@@ -5,8 +5,8 @@ module RailsBestPractices
5
5
  # Check to make sure not avoid the law of demeter.
6
6
  #
7
7
  # Implementation:
8
- # 1. check all models to record belongs_to associations
9
- # 2. check if calling belongs_to association's method or attribute
8
+ # 1. check all models to record belongs_to and has_one associations
9
+ # 2. check if calling belongs_to and has_one association's method or attribute
10
10
  class LawOfDemeterCheck < Check
11
11
 
12
12
  def interesting_nodes
@@ -20,7 +20,7 @@ module RailsBestPractices
20
20
 
21
21
  def evaluate_start(node)
22
22
  if node.node_type == :class
23
- remember_belongs_to(node)
23
+ remember_association(node)
24
24
  elsif [:lvar, :ivar].include?(node.subject.node_type) and node.subject != s(:lvar, :_erbout)
25
25
  add_error "law of demeter" if need_delegate?(node)
26
26
  end
@@ -28,8 +28,9 @@ module RailsBestPractices
28
28
 
29
29
  private
30
30
 
31
- def remember_belongs_to(node)
32
- node.body.grep_nodes(:message => :belongs_to).collect do |body_node|
31
+ # remember belongs_to or has_one node
32
+ def remember_association(node)
33
+ (node.body.grep_nodes(:message => :belongs_to) + node.body.grep_nodes(:message => :has_one)).collect do |body_node|
33
34
  class_name = node.subject.to_s.underscore
34
35
  @associations[class_name] ||= []
35
36
  @associations[class_name] << body_node.arguments[1].to_ruby_string
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_best_practices}
8
- s.version = "0.3.23"
8
+ s.version = "0.3.24"
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"]
@@ -85,7 +85,7 @@ Gem::Specification.new do |s|
85
85
  "spec/spec.opts",
86
86
  "spec/spec_helper.rb"
87
87
  ]
88
- s.homepage = %q{http://github.com/flyerhzm/rails_best_practices}
88
+ s.homepage = %q{http://rails-bestpractices.com}
89
89
  s.rdoc_options = ["--charset=UTF-8"]
90
90
  s.require_paths = ["lib"]
91
91
  s.rubygems_version = %q{1.3.6}
@@ -1,49 +1,76 @@
1
1
  require File.join(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe RailsBestPractices::Checks::LawOfDemeterCheck do
4
- before(:each) do
5
- @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
4
+
5
+ describe "belongs_to" do
6
+ before(:each) do
7
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
6
8
 
7
- content = <<-EOF
8
- class Invoice < ActiveRecord::Base
9
- belongs_to :user
9
+ content = <<-EOF
10
+ class Invoice < ActiveRecord::Base
11
+ belongs_to :user
12
+ end
13
+ EOF
14
+ @runner.check('app/models/invoice.rb', content)
10
15
  end
11
- EOF
12
- @runner.check('app/models/invoice.rb', content)
13
- end
14
16
 
15
- it "should law of demeter" do
16
- content = <<-EOF
17
- <%= @invoice.user.name %>
18
- <%= @invoice.user.address %>
19
- <%= @invoice.user.cellphone %>
20
- EOF
21
- @runner.check('app/views/invoices/show.html.erb', content)
22
- errors = @runner.errors
23
- errors.should_not be_empty
24
- errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
25
- end
17
+ it "should law of demeter" do
18
+ content = <<-EOF
19
+ <%= @invoice.user.name %>
20
+ <%= @invoice.user.address %>
21
+ <%= @invoice.user.cellphone %>
22
+ EOF
23
+ @runner.check('app/views/invoices/show.html.erb', content)
24
+ errors = @runner.errors
25
+ errors.should_not be_empty
26
+ errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
27
+ end
26
28
 
27
- it "should law of demeter" do
28
- content = <<-EOF
29
+ it "should law of demeter" do
30
+ content = <<-EOF
29
31
  = @invoice.user.name
30
32
  = @invoice.user.address
31
33
  = @invoice.user.cellphone
32
- EOF
33
- @runner.check('app/views/invoices/show.html.haml', content)
34
- errors = @runner.errors
35
- errors.should_not be_empty
36
- errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
34
+ EOF
35
+ @runner.check('app/views/invoices/show.html.haml', content)
36
+ errors = @runner.errors
37
+ errors.should_not be_empty
38
+ errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
39
+ end
40
+
41
+ it "should no law of demeter" do
42
+ content = <<-EOF
43
+ <%= @invoice.user_name %>
44
+ <%= @invoice.user_address %>
45
+ <%= @invoice.user_cellphone %>
46
+ EOF
47
+ @runner.check('app/views/invoices/show.html.erb', content)
48
+ errors = @runner.errors
49
+ errors.should be_empty
50
+ end
37
51
  end
38
52
 
39
- it "should no law of demeter" do
40
- content = <<-EOF
41
- <%= @invoice.user_name %>
42
- <%= @invoice.user_address %>
43
- <%= @invoice.user_cellphone %>
44
- EOF
45
- @runner.check('app/views/invoices/show.html.erb', content)
46
- errors = @runner.errors
47
- errors.should be_empty
53
+ describe "has_one" do
54
+ before(:each) do
55
+ @runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
56
+
57
+ content = <<-EOF
58
+ class Invoice < ActiveRecord::Base
59
+ has_one :price
60
+ end
61
+ EOF
62
+ @runner.check('app/models/invoice.rb', content)
63
+ end
64
+
65
+ it "should law of demeter" do
66
+ content = <<-EOF
67
+ <%= @invoice.price.currency %>
68
+ <%= @invoice.price.number %>
69
+ EOF
70
+ @runner.check('app/views/invoices/show.html.erb', content)
71
+ errors = @runner.errors
72
+ errors.should_not be_empty
73
+ errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
74
+ end
48
75
  end
49
76
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 23
9
- version: 0.3.23
8
+ - 24
9
+ version: 0.3.24
10
10
  platform: ruby
11
11
  authors:
12
12
  - Richard Huang
@@ -121,7 +121,7 @@ files:
121
121
  - spec/spec.opts
122
122
  - spec/spec_helper.rb
123
123
  has_rdoc: true
124
- homepage: http://github.com/flyerhzm/rails_best_practices
124
+ homepage: http://rails-bestpractices.com
125
125
  licenses: []
126
126
 
127
127
  post_install_message: