rails_best_practices 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_best_practices (1.5.0)
4
+ rails_best_practices (1.5.1)
5
5
  activesupport
6
6
  colored
7
7
  erubis
@@ -172,6 +172,11 @@ module RailsBestPractices
172
172
  @klass.extend_class_name
173
173
  end
174
174
 
175
+ # get the current module name.
176
+ def current_module_name
177
+ modules.join("::")
178
+ end
179
+
175
180
  # modules.
176
181
  def modules
177
182
  @moduels ||= []
@@ -215,11 +220,16 @@ module RailsBestPractices
215
220
  mark_used(node)
216
221
  end
217
222
 
223
+ # skip start_command callback for these nodes
224
+ def skip_command_callback_nodes
225
+ []
226
+ end
227
+
218
228
  # remember the message of command node.
219
229
  # remember the argument of alias_method and alias_method_chain as well.
220
230
  add_callback "start_command" do |node|
221
231
  case node.message.to_s
222
- when "named_scope", "scope"
232
+ when *skip_command_callback_nodes
223
233
  # nothing
224
234
  when "alias_method"
225
235
  mark_used(node.arguments.all[1])
@@ -75,7 +75,28 @@ module RailsBestPractices
75
75
  end
76
76
  end
77
77
 
78
- # remomber the method name, the method is probably be used for the class' public method.
78
+ # Mark the method as public.
79
+ #
80
+ # @param [String] class name
81
+ # @param [String] method name
82
+ def mark_publicize(class_name, method_name)
83
+ method = get_method(class_name, method_name)
84
+ method.publicize if method
85
+ end
86
+
87
+ # Mark parent classs' method as public.
88
+ #
89
+ # @param [String] class name
90
+ # @param [String] method name
91
+ def mark_parent_class_methods_publicize(class_name, method_name)
92
+ klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
93
+ if klass && klass.extend_class_name
94
+ mark_parent_class_methods_publicize(klass.extend_class_name, method_name)
95
+ mark_publicize(class_name, method_name)
96
+ end
97
+ end
98
+
99
+ # Remomber the method name, the method is probably be used for the class' public method.
79
100
  #
80
101
  # @param [String] method name
81
102
  def possible_public_used(method_name)
@@ -133,10 +154,15 @@ module RailsBestPractices
133
154
  @used = false
134
155
  end
135
156
 
136
- # Mark this method as used.
157
+ # Mark the method as used.
137
158
  def mark_used
138
159
  @used = true
139
160
  end
161
+
162
+ # Mark the method as public
163
+ def publicize
164
+ @access_control = "public"
165
+ end
140
166
  end
141
167
  end
142
168
  end
@@ -11,7 +11,7 @@ module RailsBestPractices
11
11
  include InheritedResourcesable
12
12
 
13
13
  interesting_nodes :class, :command, :method_add_arg
14
- interesting_files CONTROLLER_FILES, VIEW_FILES
14
+ interesting_files CONTROLLER_FILES, VIEW_FILES, HELPER_FILES
15
15
 
16
16
  INHERITED_RESOURCES_METHODS = %w(resource collection begin_of_association_chain build_resource)
17
17
 
@@ -31,6 +31,12 @@ module RailsBestPractices
31
31
  end
32
32
  end
33
33
 
34
+ # skip render and around_filter nodes for start_command callbacks.
35
+ def skip_command_callback_nodes
36
+ %w(render_cell render around_filter)
37
+ end
38
+
39
+ # mark corresponding action as used for cells' render and render_call.
34
40
  def start_command(node)
35
41
  case node.message.to_s
36
42
  when "render_cell"
@@ -42,6 +48,18 @@ module RailsBestPractices
42
48
  action_name = first_argument.hash_value("state").to_s
43
49
  call_method(action_name, current_class_name)
44
50
  end
51
+ when "around_filter"
52
+ node.arguments.all.each { |argument| mark_used(argument) }
53
+ when "helper_method"
54
+ node.arguments.all.each { |argument| mark_publicize(argument.to_s) }
55
+ when "delegate"
56
+ last_argument = node.arguments.all.last
57
+ if :bare_assoc_hash == last_argument.sexp_type && "controller" == last_argument.hash_value("to").to_s
58
+ controller_name = current_module_name.sub("Helper", "Controller")
59
+ node.arguments.all[0..-2].each { |method| mark_publicize(method.to_s, controller_name) }
60
+ end
61
+ else
62
+ # nothing
45
63
  end
46
64
  end
47
65
 
@@ -72,6 +90,11 @@ module RailsBestPractices
72
90
  def internal_except_methods
73
91
  %w(rescue_action).map { |method_name| "*\##{method_name}" }
74
92
  end
93
+
94
+ def mark_publicize(method_name, class_name=current_class_name)
95
+ @controller_methods.mark_publicize(class_name, method_name)
96
+ @controller_methods.mark_parent_class_methods_publicize(class_name, method_name)
97
+ end
75
98
  end
76
99
  end
77
100
  end
@@ -17,6 +17,7 @@ module RailsBestPractices
17
17
  include Callable
18
18
  include Exceptable
19
19
 
20
+ interesting_nodes :command
20
21
  interesting_files ALL_FILES
21
22
 
22
23
  def initialize(options={})
@@ -24,6 +25,21 @@ module RailsBestPractices
24
25
  @model_methods = Prepares.model_methods
25
26
  end
26
27
 
28
+ # skip scope and validate nodes for start_command callbacks.
29
+ def skip_command_callback_nodes
30
+ %w(named_scope scope validate validate_on_create validate_on_update)
31
+ end
32
+
33
+ # mark validate methods as used.
34
+ def start_command(node)
35
+ case node.message.to_s
36
+ when "validate", "validate_on_create", "validate_on_update"
37
+ node.arguments.all.each { |argument| mark_used(argument) }
38
+ else
39
+ # nothing
40
+ end
41
+ end
42
+
27
43
  # get all unused methods at the end of review process.
28
44
  def on_complete
29
45
  @model_methods.get_all_unused_methods.each do |method|
@@ -39,7 +55,7 @@ module RailsBestPractices
39
55
  end
40
56
 
41
57
  def internal_except_methods
42
- %w(initialize validate to_xml to_json assign_attributes after_find after_initialize).map { |method_name| "*\##{method_name}" }
58
+ %w(initialize validate validate_each to_xml to_json assign_attributes after_find after_initialize).map { |method_name| "*\##{method_name}" }
43
59
  end
44
60
  end
45
61
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module RailsBestPractices
3
- VERSION = "1.5.0"
3
+ VERSION = "1.5.1"
4
4
  end
@@ -53,6 +53,31 @@ describe RailsBestPractices::Reviews::RemoveUnusedMethodsInControllersReview do
53
53
  runner.should have(0).errors
54
54
  end
55
55
 
56
+ it "should not remove unused methods for around_filter" do
57
+ content =<<-EOF
58
+ RailsBestPracticesCom::Application.routes.draw do
59
+ resources :posts
60
+ end
61
+ EOF
62
+ runner.prepare('config/routes.rb', content)
63
+ content =<<-EOF
64
+ class PostsController < ActiveRecord::Base
65
+ around_filter :set_timestamp
66
+ protected
67
+ def set_timestamp
68
+ Time.zone = "Pacific Time (US & Canada)"
69
+ yield
70
+ ensure
71
+ Time.zone = "UTC"
72
+ end
73
+ end
74
+ EOF
75
+ runner.prepare('app/controllers/posts_controller.rb', content)
76
+ runner.review('app/controllers/posts_controller.rb', content)
77
+ runner.on_complete
78
+ runner.should have(0).errors
79
+ end
80
+
56
81
  it "should not remove inherited_resources methods" do
57
82
  content =<<-EOF
58
83
  RailsBestPracticesCom::Application.routes.draw do
@@ -130,6 +155,108 @@ describe RailsBestPractices::Reviews::RemoveUnusedMethodsInControllersReview do
130
155
  end
131
156
  end
132
157
 
158
+ context "helper_method" do
159
+ it "should remove unused methods if helper method is not called" do
160
+ content = <<-EOF
161
+ class PostsController < ApplicationController
162
+ helper_method :helper_post
163
+ protected
164
+ def helper_post; end
165
+ end
166
+ EOF
167
+ runner.prepare('app/controllers/posts_controller.rb', content)
168
+ runner.review('app/controllers/posts_controller.rb', content)
169
+ runner.on_complete
170
+ runner.should have(1).errors
171
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:4 - remove unused methods (PostsController#helper_post)"
172
+ end
173
+
174
+ it "should not remove unused methods if call helper method in views" do
175
+ content = <<-EOF
176
+ class PostsController < ApplicationController
177
+ helper_method :helper_post
178
+ protected
179
+ def helper_post; end
180
+ end
181
+ EOF
182
+ runner.prepare('app/controllers/posts_controller.rb', content)
183
+ runner.review('app/controllers/posts_controller.rb', content)
184
+ content = <<-EOF
185
+ <%= helper_post %>
186
+ EOF
187
+ runner.review('app/views/posts/show.html.erb', content)
188
+ runner.on_complete
189
+ runner.should have(0).errors
190
+ end
191
+
192
+ it "should not remove unused methods if call helper method in helpers" do
193
+ content = <<-EOF
194
+ class PostsController < ApplicationController
195
+ helper_method :helper_post
196
+ protected
197
+ def helper_post; end
198
+ end
199
+ EOF
200
+ runner.prepare('app/controllers/posts_controller.rb', content)
201
+ runner.review('app/controllers/posts_controller.rb', content)
202
+ content = <<-EOF
203
+ module PostsHelper
204
+ def new_post
205
+ helper_post
206
+ end
207
+ end
208
+ EOF
209
+ runner.review('app/helpers/posts_helper.rb', content)
210
+ runner.on_complete
211
+ runner.should have(0).errors
212
+ end
213
+ end
214
+
215
+ context "delegate :to => :controller" do
216
+ it "should remove unused methods if delegate method is not called" do
217
+ content = <<-EOF
218
+ class PostsController < ApplicationController
219
+ protected
220
+ def helper_post(type); end
221
+ end
222
+ EOF
223
+ runner.prepare('app/controllers/posts_controller.rb', content)
224
+ runner.review('app/controllers/posts_controller.rb', content)
225
+ content = <<-EOF
226
+ module PostsHelper
227
+ delegate :helper_post, :to => :controller
228
+ end
229
+ EOF
230
+ runner.review('app/helpers/posts_helper.rb', content)
231
+ runner.on_complete
232
+ runner.should have(1).errors
233
+ runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:3 - remove unused methods (PostsController#helper_post)"
234
+ end
235
+
236
+ it "should remove unused methods if delegate method is called" do
237
+ content = <<-EOF
238
+ class PostsController < ApplicationController
239
+ protected
240
+ def helper_post(type); end
241
+ end
242
+ EOF
243
+ runner.prepare('app/controllers/posts_controller.rb', content)
244
+ runner.review('app/controllers/posts_controller.rb', content)
245
+ content = <<-EOF
246
+ module PostsHelper
247
+ delegate :helper_post, :to => :controller
248
+ end
249
+ EOF
250
+ runner.review('app/helpers/posts_helper.rb', content)
251
+ content = <<-EOF
252
+ <%= helper_post("new") %>
253
+ EOF
254
+ runner.review('app/views/posts/show.html.erb', content)
255
+ runner.on_complete
256
+ runner.should have(0).errors
257
+ end
258
+ end
259
+
133
260
  context "cells" do
134
261
  it "should remove unused methods" do
135
262
  content =<<-EOF
@@ -550,4 +550,57 @@ describe RailsBestPractices::Reviews::RemoveUnusedMethodsInModelsReview do
550
550
  runner.should have(0).errors
551
551
  end
552
552
  end
553
+
554
+ context "callbacks" do
555
+ it "should not remove unused method" do
556
+ content =<<-EOF
557
+ class Post < ActiveRecord::Base
558
+ before_save :init_columns
559
+ after_destroy :remove_dependencies
560
+
561
+ protected
562
+ def init_columns; end
563
+ def remove_dependencies; end
564
+ end
565
+ EOF
566
+ runner.prepare("app/models/post.rb", content)
567
+ runner.review("app/models/post.rb", content)
568
+ runner.on_complete
569
+ runner.should have(0).errors
570
+ end
571
+ end
572
+
573
+ context "validates" do
574
+ it "should not remove unused method" do
575
+ content =<<-EOF
576
+ class Post < ActiveRecord::Base
577
+ validate :valid_birth_date
578
+
579
+ protected
580
+ def valid_birth_date; end
581
+ end
582
+ EOF
583
+ runner.prepare("app/models/post.rb", content)
584
+ runner.review("app/models/post.rb", content)
585
+ runner.on_complete
586
+ runner.should have(0).errors
587
+ end
588
+
589
+ it "should not remove unused method for validate_on_create and validate_on_update" do
590
+ content =<<-EOF
591
+ class Post < ActiveRecord::Base
592
+ validate_on_create :valid_email
593
+ validate_on_update :valid_birth_date
594
+
595
+ protected
596
+ def valid_email; end
597
+ def valid_birth_date; end
598
+ end
599
+ EOF
600
+ runner.prepare("app/models/post.rb", content)
601
+ runner.review("app/models/post.rb", content)
602
+ runner.on_complete
603
+ runner.should have(0).errors
604
+ end
605
+ end
553
606
  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: 1.5.0
4
+ version: 1.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-26 00:00:00.000000000Z
12
+ date: 2011-11-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sexp_processor
16
- requirement: &70247886541600 !ruby/object:Gem::Requirement
16
+ requirement: &70264679380100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70247886541600
24
+ version_requirements: *70264679380100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: progressbar
27
- requirement: &70247886540700 !ruby/object:Gem::Requirement
27
+ requirement: &70264679364420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70247886540700
35
+ version_requirements: *70264679364420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: colored
38
- requirement: &70247886540000 !ruby/object:Gem::Requirement
38
+ requirement: &70264679363200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70247886540000
46
+ version_requirements: *70264679363200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: erubis
49
- requirement: &70247886539360 !ruby/object:Gem::Requirement
49
+ requirement: &70264679361180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70247886539360
57
+ version_requirements: *70264679361180
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: i18n
60
- requirement: &70247886538640 !ruby/object:Gem::Requirement
60
+ requirement: &70264679359900 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70247886538640
68
+ version_requirements: *70264679359900
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
- requirement: &70247886537980 !ruby/object:Gem::Requirement
71
+ requirement: &70264679358160 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70247886537980
79
+ version_requirements: *70264679358160
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rake
82
- requirement: &70247886537200 !ruby/object:Gem::Requirement
82
+ requirement: &70264679356880 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70247886537200
90
+ version_requirements: *70264679356880
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rspec
93
- requirement: &70247886536480 !ruby/object:Gem::Requirement
93
+ requirement: &70264679355940 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70247886536480
101
+ version_requirements: *70264679355940
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: haml
104
- requirement: &70247886535760 !ruby/object:Gem::Requirement
104
+ requirement: &70264679354960 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70247886535760
112
+ version_requirements: *70264679354960
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: bundler
115
- requirement: &70247886534860 !ruby/object:Gem::Requirement
115
+ requirement: &70264679353820 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70247886534860
123
+ version_requirements: *70264679353820
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: spork
126
- requirement: &70247886533880 !ruby/object:Gem::Requirement
126
+ requirement: &70264679351020 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - =
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: 0.9.0.rc9
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70247886533880
134
+ version_requirements: *70264679351020
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: guard
137
- requirement: &70247886532280 !ruby/object:Gem::Requirement
137
+ requirement: &70264679348780 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70247886532280
145
+ version_requirements: *70264679348780
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: guard-spork
148
- requirement: &70247886531300 !ruby/object:Gem::Requirement
148
+ requirement: &70264679326140 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *70247886531300
156
+ version_requirements: *70264679326140
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: guard-rspec
159
- requirement: &70247886530520 !ruby/object:Gem::Requirement
159
+ requirement: &70264679325060 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,7 +164,7 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *70247886530520
167
+ version_requirements: *70264679325060
168
168
  description: a code metric tool for rails codes, written in Ruby.
169
169
  email:
170
170
  - flyerhzm@gmail.com
@@ -324,7 +324,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
324
324
  version: '0'
325
325
  segments:
326
326
  - 0
327
- hash: -3104358296029422528
327
+ hash: -1541278919148785467
328
328
  required_rubygems_version: !ruby/object:Gem::Requirement
329
329
  none: false
330
330
  requirements: