rails_best_practices 1.12.0 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc +1 -1
- data/README.md +4 -0
- data/lib/rails_best_practices/analyzer.rb +3 -0
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +2 -0
- data/lib/rails_best_practices/core/gems.rb +7 -0
- data/lib/rails_best_practices/core/routes.rb +8 -0
- data/lib/rails_best_practices/core/runner.rb +1 -1
- data/lib/rails_best_practices/prepares.rb +5 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +2 -0
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +23 -0
- data/lib/rails_best_practices/reviews.rb +2 -0
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +78 -0
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +27 -0
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.yml +2 -0
- data/spec/rails_best_practices/core/gems_spec.rb +7 -0
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +22 -0
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +22 -0
- data/spec/rails_best_practices/reviews/check_save_return_value_spec.rb +198 -0
- data/spec/rails_best_practices/reviews/not_rescue_exception_spec.rb +0 -0
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +10 -3
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +54 -0
- metadata +15 -3
data/.rvmrc
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
rvm_gemset_create_on_use_flag=1
|
2
|
-
rvm gemset use ruby-1.9.3
|
2
|
+
rvm gemset use ruby-1.9.3@rails_best_practices
|
data/README.md
CHANGED
@@ -116,6 +116,7 @@ Now you can customize this configuration file, the default configuration is as f
|
|
116
116
|
|
117
117
|
AddModelVirtualAttributeCheck: { }
|
118
118
|
AlwaysAddDbIndexCheck: { }
|
119
|
+
CheckSaveReturnValueCheck: { }
|
119
120
|
DryBundlerInCapistranoCheck: { }
|
120
121
|
#HashSyntaxCheck: { }
|
121
122
|
IsolateSeedDataCheck: { }
|
@@ -152,6 +153,7 @@ Now you can customize this configuration file, the default configuration is as f
|
|
152
153
|
UseQueryAttributeCheck: { }
|
153
154
|
UseSayWithTimeInMigrationsCheck: { }
|
154
155
|
UseScopeAccessCheck: { }
|
156
|
+
UseTurboSprocketsRails3Check: { }
|
155
157
|
|
156
158
|
You can remove or comment one review to disable it, and you can change the options.
|
157
159
|
|
@@ -165,6 +167,7 @@ Move code from Controller to Model
|
|
165
167
|
4. Add model virtual attribute
|
166
168
|
5. Replace complex creation with factory method
|
167
169
|
6. Move model logic into the Model
|
170
|
+
7. Check return value of "save!"
|
168
171
|
|
169
172
|
RESTful Conventions
|
170
173
|
|
@@ -215,6 +218,7 @@ View
|
|
215
218
|
Deployment
|
216
219
|
|
217
220
|
1. Dry bundler in capistrano
|
221
|
+
2. Speed up assets precompile with turbo-sprockets-rails3
|
218
222
|
|
219
223
|
Other
|
220
224
|
|
@@ -13,5 +13,6 @@ require 'rails_best_practices/core/controllers'
|
|
13
13
|
require 'rails_best_practices/core/helpers'
|
14
14
|
require 'rails_best_practices/core/routes'
|
15
15
|
require 'rails_best_practices/core/configs'
|
16
|
+
require 'rails_best_practices/core/gems'
|
16
17
|
|
17
18
|
require 'rails_best_practices/core_ext/erubis'
|
@@ -15,6 +15,8 @@ module RailsBestPractices
|
|
15
15
|
HELPER_FILES = /app\/helpers\/.*\.rb$/
|
16
16
|
DEPLOY_FILES = /config\/deploy.*\.rb/
|
17
17
|
CONFIG_FILES = /config\/(application|environment|environments\/.*)\.rb/
|
18
|
+
CAPFILE = /Capfile/
|
19
|
+
GEMFILE = /Gemfile/
|
18
20
|
|
19
21
|
SKIP_FILES = /db\/schema.rb/
|
20
22
|
|
@@ -19,6 +19,14 @@ module RailsBestPractices
|
|
19
19
|
|
20
20
|
def initialize(namespaces, controller_name, action_name)
|
21
21
|
@namespaces = namespaces
|
22
|
+
|
23
|
+
# mappings can be specified by e.g.
|
24
|
+
# post 'some/:pattern' => 'controller#action'
|
25
|
+
if action_name =~ /^(\w+)#(\w+)$/
|
26
|
+
controller_name = $1
|
27
|
+
action_name = $2
|
28
|
+
end
|
29
|
+
|
22
30
|
if controller_name
|
23
31
|
entities = controller_name.split('/')
|
24
32
|
@namespaces += entities[0..-2] if entities.size > 1
|
@@ -153,7 +153,7 @@ module RailsBestPractices
|
|
153
153
|
checks_from_config.inject([]) { |active_checks, check|
|
154
154
|
begin
|
155
155
|
check_name, options = *check
|
156
|
-
klass = RailsBestPractices::Reviews.const_get(check_name.gsub(/Check
|
156
|
+
klass = RailsBestPractices::Reviews.const_get(check_name.gsub(/Check$/, 'Review'))
|
157
157
|
active_checks << (options.empty? ? klass.new : klass.new(options))
|
158
158
|
rescue
|
159
159
|
# the check does not exist in the Reviews namepace.
|
@@ -6,6 +6,7 @@ require 'rails_best_practices/prepares/controller_prepare'
|
|
6
6
|
require 'rails_best_practices/prepares/route_prepare'
|
7
7
|
require 'rails_best_practices/prepares/helper_prepare'
|
8
8
|
require 'rails_best_practices/prepares/config_prepare'
|
9
|
+
require 'rails_best_practices/prepares/gemfile_prepare'
|
9
10
|
|
10
11
|
module RailsBestPractices
|
11
12
|
module Prepares
|
@@ -58,6 +59,10 @@ module RailsBestPractices
|
|
58
59
|
@configs ||= Core::Configs.new
|
59
60
|
end
|
60
61
|
|
62
|
+
def gems
|
63
|
+
@gems ||= Core::Gems.new
|
64
|
+
end
|
65
|
+
|
61
66
|
# Clear all prepare objects.
|
62
67
|
def clear
|
63
68
|
instance_variables.each do |instance_variable|
|
@@ -3,6 +3,7 @@ require 'rails_best_practices/core/check'
|
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
5
|
module Prepares
|
6
|
+
# Remember all configs
|
6
7
|
class ConfigPrepare < Core::Check
|
7
8
|
interesting_nodes :assign
|
8
9
|
interesting_files CONFIG_FILES
|
@@ -11,6 +12,7 @@ module RailsBestPractices
|
|
11
12
|
@configs = Prepares.configs
|
12
13
|
end
|
13
14
|
|
15
|
+
# check assignments to config
|
14
16
|
add_callback :start_assign do |node|
|
15
17
|
if node.left_value.grep_node(sexp_type: [:vcall, :var_ref], to_s: "config").present?
|
16
18
|
@configs[node.left_value.to_s] = node.right_value.to_s
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails_best_practices/core/check'
|
3
|
+
|
4
|
+
module RailsBestPractices
|
5
|
+
module Prepares
|
6
|
+
# Remember all gems in Gemfile
|
7
|
+
class GemfilePrepare < Core::Check
|
8
|
+
interesting_nodes :command
|
9
|
+
interesting_files GEMFILE
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@gems = Prepares.gems
|
13
|
+
end
|
14
|
+
|
15
|
+
# Check all command nodes to get gem names.
|
16
|
+
add_callback :start_command do |node|
|
17
|
+
if "gem" == node.message.to_s
|
18
|
+
@gems << node.arguments.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -34,3 +34,5 @@ require 'rails_best_practices/reviews/protect_mass_assignment_review'
|
|
34
34
|
require 'rails_best_practices/reviews/use_parentheses_in_method_def_review'
|
35
35
|
require 'rails_best_practices/reviews/hash_syntax_review'
|
36
36
|
require 'rails_best_practices/reviews/not_rescue_exception_review'
|
37
|
+
require 'rails_best_practices/reviews/check_save_return_value_review'
|
38
|
+
require 'rails_best_practices/reviews/use_turbo_sprockets_rails3_review'
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
|
+
|
4
|
+
module RailsBestPractices
|
5
|
+
module Reviews
|
6
|
+
# Review all code to make sure we either check the return value of "save"
|
7
|
+
# or use "save!"
|
8
|
+
#
|
9
|
+
# See the best practice details here http://rails-bestpractices.com/posts/703-check-the-return-value-of-save-otherwise-use-save
|
10
|
+
#
|
11
|
+
# Implementation:
|
12
|
+
#
|
13
|
+
# Review process:
|
14
|
+
# Track which nodes are used by 'if', 'unless', '&&' nodes etc. as we pass them by.
|
15
|
+
# Check all "save" calls to check the return value is used by a node we have visited.
|
16
|
+
class CheckSaveReturnValueReview < Review
|
17
|
+
include Classable
|
18
|
+
interesting_nodes :call, :command_call, :method_add_arg, :if, :ifop, :unless, :assign, :binary
|
19
|
+
interesting_files ALL_FILES
|
20
|
+
url "http://rails-bestpractices.com/posts/703-check-the-return-value-of-save-otherwise-use-save"
|
21
|
+
|
22
|
+
add_callback :start_if, :start_ifop, :start_unless do |node|
|
23
|
+
@used_return_value_of = node.conditional_statement.all_conditions
|
24
|
+
end
|
25
|
+
|
26
|
+
add_callback :start_assign do |node|
|
27
|
+
@used_return_value_of = node.right_value
|
28
|
+
end
|
29
|
+
|
30
|
+
add_callback :start_binary do |node|
|
31
|
+
# Consider anything used in an expression like "A or B" as used
|
32
|
+
if %w(&& || and or).include?(node[2].to_s)
|
33
|
+
all_conditions = node.all_conditions
|
34
|
+
# if our current binary is a subset of the @used_return_value_of
|
35
|
+
# then don't overwrite it
|
36
|
+
already_included = @used_return_value_of &&
|
37
|
+
(all_conditions - @used_return_value_of).empty?
|
38
|
+
|
39
|
+
@used_return_value_of = node.all_conditions unless already_included
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def return_value_is_used? node
|
44
|
+
return false unless @used_return_value_of
|
45
|
+
node == @used_return_value_of or @used_return_value_of.include?(node)
|
46
|
+
end
|
47
|
+
|
48
|
+
def model_classnames
|
49
|
+
@model_classnames ||= models.map(&:to_s)
|
50
|
+
end
|
51
|
+
|
52
|
+
add_callback :start_call, :start_command_call, :start_method_add_arg do |node|
|
53
|
+
unless @already_checked == node
|
54
|
+
message = node.message.to_s
|
55
|
+
if ['save', 'update_attributes'].include? message
|
56
|
+
unless return_value_is_used? node
|
57
|
+
add_error "check '#{message}' return value or use '#{message}!'"
|
58
|
+
end
|
59
|
+
elsif message == 'create'
|
60
|
+
# We're only interested in 'create' calls on model classes:
|
61
|
+
possible_receiver_classes = [node.receiver.to_s] + classable_modules.map do |mod|
|
62
|
+
"#{mod}::#{node.receiver.to_s}"
|
63
|
+
end
|
64
|
+
unless (possible_receiver_classes & model_classnames).empty?
|
65
|
+
add_error "use 'create!' instead of 'create' as the latter may not always save"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
if node.sexp_type == :method_add_arg
|
70
|
+
@already_checked = node[1]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
|
+
|
4
|
+
module RailsBestPractices
|
5
|
+
module Reviews
|
6
|
+
# Prepare Gemfile and review Capfile file to make sure using turbo-sprocket-rails3
|
7
|
+
#
|
8
|
+
# See the best practice details here http://rails-bestpractices.com/posts/704-speed-up-assets-precompile-with-turbo-sprockets-rails3
|
9
|
+
#
|
10
|
+
# Implementation:
|
11
|
+
#
|
12
|
+
# Review process:
|
13
|
+
# only check if turbo-sprockets-rails3 gem is not used and load 'deploy/assets' in Capfile.
|
14
|
+
class UseTurboSprocketsRails3Review < Review
|
15
|
+
interesting_nodes :command
|
16
|
+
interesting_files CAPFILE
|
17
|
+
url "http://rails-bestpractices.com/posts/704-speed-up-assets-precompile-with-turbo-sprockets-rails3"
|
18
|
+
|
19
|
+
# check command node to see if load 'deploy/assets'
|
20
|
+
add_callback :start_command do |node|
|
21
|
+
if !Prepares.gems.include?("turbo-sprockets-rails3") && "load" == node.message.to_s && "deploy/assets" == node.arguments.to_s
|
22
|
+
add_error "speed up assets precompile with turbo-sprockets-rails3"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/rails_best_practices.yml
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
AddModelVirtualAttributeCheck: { }
|
2
2
|
AlwaysAddDbIndexCheck: { }
|
3
|
+
CheckSaveReturnValueCheck: { }
|
3
4
|
DryBundlerInCapistranoCheck: { }
|
4
5
|
#HashSyntaxCheck: { }
|
5
6
|
IsolateSeedDataCheck: { }
|
@@ -36,3 +37,4 @@ UseObserverCheck: { }
|
|
36
37
|
UseQueryAttributeCheck: { }
|
37
38
|
UseSayWithTimeInMigrationsCheck: { }
|
38
39
|
UseScopeAccessCheck: { }
|
40
|
+
UseTurboSprocketsRails3Check: { }
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe GemfilePrepare do
|
6
|
+
let(:runner) { Core::Runner.new(prepares: GemfilePrepare.new) }
|
7
|
+
|
8
|
+
context "gemfile" do
|
9
|
+
it "should parse gems" do
|
10
|
+
content =<<-EOF
|
11
|
+
source 'http://rubygems.org'
|
12
|
+
gem 'rails'
|
13
|
+
gem 'mysql2'
|
14
|
+
EOF
|
15
|
+
runner.prepare('Gemfile', content)
|
16
|
+
gems = Prepares.gems
|
17
|
+
gems.should == %w(rails mysql2)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -354,6 +354,28 @@ module RailsBestPractices
|
|
354
354
|
routes.size.should == 0
|
355
355
|
end
|
356
356
|
|
357
|
+
it "should add resources routes with members" do
|
358
|
+
content =<<-EOF
|
359
|
+
RailsBestPracticesCom::Application.routes.draw do
|
360
|
+
namespace :admin do
|
361
|
+
resources :posts, :only => [:edit, :update] do
|
362
|
+
member do
|
363
|
+
post 'link_to/:other_id' => 'posts#link_to_post'
|
364
|
+
post 'extra_update' => 'posts#extra_update'
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
EOF
|
370
|
+
runner.prepare('config/routes.rb', content)
|
371
|
+
routes = Prepares.routes
|
372
|
+
routes.map(&:to_s).should == [
|
373
|
+
"Admin::PostsController#edit",
|
374
|
+
"Admin::PostsController#update",
|
375
|
+
"Admin::PostsController#link_to_post",
|
376
|
+
"Admin::PostsController#extra_update"]
|
377
|
+
end
|
378
|
+
|
357
379
|
it "should add connect route" do
|
358
380
|
content =<<-EOF
|
359
381
|
ActionController::Routing::Routes.draw do |map|
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe CheckSaveReturnValueReview do
|
6
|
+
let(:runner) { Core::Runner.new(reviews: CheckSaveReturnValueReview.new) }
|
7
|
+
|
8
|
+
describe "check_save_return_value" do
|
9
|
+
it "should warn you if you fail to check save return value" do
|
10
|
+
content =<<-EOF
|
11
|
+
def my_method
|
12
|
+
post = Posts.new do |p|
|
13
|
+
p.title = "foo"
|
14
|
+
end
|
15
|
+
post.save
|
16
|
+
end
|
17
|
+
EOF
|
18
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
19
|
+
runner.should have(1).errors
|
20
|
+
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:5 - check 'save' return value or use 'save!'"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should allow save return value assigned to var" do
|
24
|
+
content =<<-EOF
|
25
|
+
def my_method
|
26
|
+
post = Posts.new do |p|
|
27
|
+
p.title = "foo"
|
28
|
+
end
|
29
|
+
check_this_later = post.save
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
33
|
+
runner.should have(0).errors
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow save return value used in if" do
|
37
|
+
content =<<-EOF
|
38
|
+
def my_method
|
39
|
+
post = Posts.new do |p|
|
40
|
+
p.title = "foo"
|
41
|
+
end
|
42
|
+
if post.save
|
43
|
+
"OK"
|
44
|
+
else
|
45
|
+
raise "could not save"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
EOF
|
49
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
50
|
+
runner.should have(0).errors
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should allow save return value used in unless" do
|
54
|
+
content =<<-EOF
|
55
|
+
def my_method
|
56
|
+
unless @post.save
|
57
|
+
raise "could not save"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
EOF
|
61
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
62
|
+
runner.should have(0).errors
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should allow save return value used in unless with &&" do
|
66
|
+
content =<<-EOF
|
67
|
+
def my_method
|
68
|
+
unless some_method(1) && other_method(2) && @post.save
|
69
|
+
raise "could not save"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
EOF
|
73
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
74
|
+
runner.should have(0).errors
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should allow save!" do
|
78
|
+
content =<<-EOF
|
79
|
+
def my_method
|
80
|
+
post = Posts.new do |p|
|
81
|
+
p.title = "foo"
|
82
|
+
end
|
83
|
+
post.save!
|
84
|
+
end
|
85
|
+
EOF
|
86
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
87
|
+
runner.should have(0).errors
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should warn you if you fail to check update_attributes return value" do
|
91
|
+
content =<<-EOF
|
92
|
+
def my_method
|
93
|
+
@post.update_attributes params
|
94
|
+
end
|
95
|
+
EOF
|
96
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
97
|
+
runner.should have(1).errors
|
98
|
+
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:2 - check 'update_attributes' return value or use 'update_attributes!'"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should allow update_attributes if return value is checked" do
|
102
|
+
content =<<-EOF
|
103
|
+
def my_method
|
104
|
+
@post.update_attributes(params) or raise "failed to save"
|
105
|
+
end
|
106
|
+
EOF
|
107
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
108
|
+
runner.should have(0).errors
|
109
|
+
end
|
110
|
+
|
111
|
+
it "is not clever enough to allow update_attributes if value is returned from method" do
|
112
|
+
# This review is not clever enough to do a full liveness analysis
|
113
|
+
# of whether the returned value is used in all cases.
|
114
|
+
content =<<-EOF
|
115
|
+
class PostsController
|
116
|
+
def update
|
117
|
+
@post = Post.find params(:id)
|
118
|
+
if update_post
|
119
|
+
redirect_to view_post_path post
|
120
|
+
else
|
121
|
+
raise "post not saved"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def update_post
|
126
|
+
@post.update_attributes(params)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
EOF
|
130
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
131
|
+
runner.should have(1).errors
|
132
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:12 - check 'update_attributes' return value or use 'update_attributes!'"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should warn you if you use create which is always unsafe" do
|
136
|
+
content =<<-EOF
|
137
|
+
class Post < ActiveRecord::Base
|
138
|
+
end
|
139
|
+
EOF
|
140
|
+
runner.prepare('app/models/post.rb', content)
|
141
|
+
content =<<-EOF
|
142
|
+
def my_method
|
143
|
+
if post = Post.create(params)
|
144
|
+
# post may or may not be saved here!
|
145
|
+
redirect_to view_post_path post
|
146
|
+
end
|
147
|
+
end
|
148
|
+
EOF
|
149
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
150
|
+
runner.should have(1).errors
|
151
|
+
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:2 - use 'create!' instead of 'create' as the latter may not always save"
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should warn you if you use create with a block which is always unsafe" do
|
155
|
+
content =<<-EOF
|
156
|
+
module Blog
|
157
|
+
class Post < ActiveRecord::Base
|
158
|
+
end
|
159
|
+
end
|
160
|
+
EOF
|
161
|
+
runner.prepare('app/models/blog/post.rb', content)
|
162
|
+
content =<<-EOF
|
163
|
+
module Blog
|
164
|
+
class PostsHelper
|
165
|
+
def my_method
|
166
|
+
post = Post.create do |p|
|
167
|
+
p.title = 'new post'
|
168
|
+
end
|
169
|
+
if post
|
170
|
+
# post may or may not be saved here!
|
171
|
+
redirect_to view_post_path post
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
EOF
|
177
|
+
runner.review('app/helpers/blog/posts_helper.rb', content)
|
178
|
+
runner.should have(1).errors
|
179
|
+
runner.errors[0].to_s.should == "app/helpers/blog/posts_helper.rb:4 - use 'create!' instead of 'create' as the latter may not always save"
|
180
|
+
end
|
181
|
+
|
182
|
+
it "allows create called on non-model classes" do
|
183
|
+
content =<<-EOF
|
184
|
+
def my_method
|
185
|
+
pk12 = OpenSSL::PKCS12.create(
|
186
|
+
"", # password
|
187
|
+
descr, # friendly name
|
188
|
+
key,
|
189
|
+
cert)
|
190
|
+
end
|
191
|
+
EOF
|
192
|
+
runner.review('app/helpers/posts_helper.rb', content)
|
193
|
+
runner.errors.map(&:to_s).should == []
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
File without changes
|
@@ -12,13 +12,20 @@ module RailsBestPractices
|
|
12
12
|
it "should remove unused methods" do
|
13
13
|
content =<<-EOF
|
14
14
|
RailsBestPracticesCom::Application.routes.draw do
|
15
|
-
resources :posts
|
15
|
+
resources :posts do
|
16
|
+
member do
|
17
|
+
post 'link_to/:other_id' => 'posts#link_to_post'
|
18
|
+
post 'extra_update' => 'posts#extra_update'
|
19
|
+
end
|
20
|
+
end
|
16
21
|
end
|
17
22
|
EOF
|
18
23
|
runner.prepare('config/routes.rb', content)
|
19
24
|
content =<<-EOF
|
20
25
|
class PostsController < ActiveRecord::Base
|
21
26
|
def show; end
|
27
|
+
def extra_update; end
|
28
|
+
def link_to_post; end
|
22
29
|
protected
|
23
30
|
def load_post; end
|
24
31
|
private
|
@@ -29,8 +36,8 @@ module RailsBestPractices
|
|
29
36
|
runner.review('app/controllers/posts_controller.rb', content)
|
30
37
|
runner.after_review
|
31
38
|
runner.should have(2).errors
|
32
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:
|
33
|
-
runner.errors[1].to_s.should == "app/controllers/posts_controller.rb:
|
39
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - remove unused methods (PostsController#load_post)"
|
40
|
+
runner.errors[1].to_s.should == "app/controllers/posts_controller.rb:8 - remove unused methods (PostsController#load_user)"
|
34
41
|
end
|
35
42
|
|
36
43
|
it "should not remove unused methods for before_filter" do
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe UseTurboSprocketsRails3Review do
|
6
|
+
let(:runner) { Core::Runner.new(prepares: Prepares::GemfilePrepare.new, reviews: UseTurboSprocketsRails3Review.new) }
|
7
|
+
|
8
|
+
it "should use turbo-sprockets-rails3" do
|
9
|
+
content = <<-EOF
|
10
|
+
source "http://rubygems.org"
|
11
|
+
gem "rails"
|
12
|
+
EOF
|
13
|
+
runner.prepare('Gemfile', content)
|
14
|
+
content = <<-EOF
|
15
|
+
load 'deploy' if respond_to?(:namespace)
|
16
|
+
load 'deploy/assets'
|
17
|
+
load 'config/deploy'
|
18
|
+
EOF
|
19
|
+
runner.review('Capfile', content)
|
20
|
+
runner.should have(1).errors
|
21
|
+
runner.errors[0].to_s.should == "Capfile:2 - speed up assets precompile with turbo-sprockets-rails3"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should not use turbo-sprockets-rails3 with turbo-sprockets-rails3 gem" do
|
25
|
+
content = <<-EOF
|
26
|
+
source "http://rubygems.org"
|
27
|
+
gem "rails"
|
28
|
+
group :assets do
|
29
|
+
gem "turbo-sprockets-rails3"
|
30
|
+
end
|
31
|
+
EOF
|
32
|
+
runner.prepare('Gemfile', content)
|
33
|
+
content = <<-EOF
|
34
|
+
load 'deploy' if respond_to?(:namespace)
|
35
|
+
load 'deploy/assets'
|
36
|
+
load 'config/deploy'
|
37
|
+
EOF
|
38
|
+
runner.review('Capfile', content)
|
39
|
+
runner.should have(0).errors
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not use turbo-sprockets-rails3 without deploy/assets" do
|
43
|
+
content = <<-EOF
|
44
|
+
load 'deploy' if respond_to?(:namespace)
|
45
|
+
#load 'deploy/assets'
|
46
|
+
load 'config/deploy'
|
47
|
+
EOF
|
48
|
+
runner.review('Capfile', content)
|
49
|
+
runner.should have(0).errors
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
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.
|
4
|
+
version: 1.13.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: code_analyzer
|
@@ -232,6 +232,7 @@ files:
|
|
232
232
|
- lib/rails_best_practices/core/configs.rb
|
233
233
|
- lib/rails_best_practices/core/controllers.rb
|
234
234
|
- lib/rails_best_practices/core/error.rb
|
235
|
+
- lib/rails_best_practices/core/gems.rb
|
235
236
|
- lib/rails_best_practices/core/helpers.rb
|
236
237
|
- lib/rails_best_practices/core/klasses.rb
|
237
238
|
- lib/rails_best_practices/core/mailers.rb
|
@@ -250,6 +251,7 @@ files:
|
|
250
251
|
- lib/rails_best_practices/prepares.rb
|
251
252
|
- lib/rails_best_practices/prepares/config_prepare.rb
|
252
253
|
- lib/rails_best_practices/prepares/controller_prepare.rb
|
254
|
+
- lib/rails_best_practices/prepares/gemfile_prepare.rb
|
253
255
|
- lib/rails_best_practices/prepares/helper_prepare.rb
|
254
256
|
- lib/rails_best_practices/prepares/mailer_prepare.rb
|
255
257
|
- lib/rails_best_practices/prepares/model_prepare.rb
|
@@ -258,6 +260,7 @@ files:
|
|
258
260
|
- lib/rails_best_practices/reviews.rb
|
259
261
|
- lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb
|
260
262
|
- lib/rails_best_practices/reviews/always_add_db_index_review.rb
|
263
|
+
- lib/rails_best_practices/reviews/check_save_return_value_review.rb
|
261
264
|
- lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb
|
262
265
|
- lib/rails_best_practices/reviews/hash_syntax_review.rb
|
263
266
|
- lib/rails_best_practices/reviews/isolate_seed_data_review.rb
|
@@ -292,6 +295,7 @@ files:
|
|
292
295
|
- lib/rails_best_practices/reviews/use_query_attribute_review.rb
|
293
296
|
- lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb
|
294
297
|
- lib/rails_best_practices/reviews/use_scope_access_review.rb
|
298
|
+
- lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb
|
295
299
|
- lib/rails_best_practices/version.rb
|
296
300
|
- rails_best_practices.gemspec
|
297
301
|
- rails_best_practices.yml
|
@@ -302,6 +306,7 @@ files:
|
|
302
306
|
- spec/rails_best_practices/core/configs_spec.rb
|
303
307
|
- spec/rails_best_practices/core/controllers_spec.rb
|
304
308
|
- spec/rails_best_practices/core/error_spec.rb
|
309
|
+
- spec/rails_best_practices/core/gems_spec.rb
|
305
310
|
- spec/rails_best_practices/core/helpers_spec.rb
|
306
311
|
- spec/rails_best_practices/core/klasses_spec.rb
|
307
312
|
- spec/rails_best_practices/core/mailers_spec.rb
|
@@ -318,6 +323,7 @@ files:
|
|
318
323
|
- spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb
|
319
324
|
- spec/rails_best_practices/prepares/config_prepare_spec.rb
|
320
325
|
- spec/rails_best_practices/prepares/controller_prepare_spec.rb
|
326
|
+
- spec/rails_best_practices/prepares/gemfile_prepare_spec.rb
|
321
327
|
- spec/rails_best_practices/prepares/helper_prepare_spec.rb
|
322
328
|
- spec/rails_best_practices/prepares/mailer_prepare_spec.rb
|
323
329
|
- spec/rails_best_practices/prepares/model_prepare_spec.rb
|
@@ -325,6 +331,7 @@ files:
|
|
325
331
|
- spec/rails_best_practices/prepares/schema_prepare_spec.rb
|
326
332
|
- spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb
|
327
333
|
- spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb
|
334
|
+
- spec/rails_best_practices/reviews/check_save_return_value_spec.rb
|
328
335
|
- spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb
|
329
336
|
- spec/rails_best_practices/reviews/hash_syntax_review_spec.rb
|
330
337
|
- spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb
|
@@ -358,6 +365,7 @@ files:
|
|
358
365
|
- spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb
|
359
366
|
- spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb
|
360
367
|
- spec/rails_best_practices/reviews/use_scope_access_review_spec.rb
|
368
|
+
- spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb
|
361
369
|
- spec/spec_helper.rb
|
362
370
|
homepage: http://rails-bestpractices.com
|
363
371
|
licenses: []
|
@@ -378,7 +386,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
378
386
|
version: '0'
|
379
387
|
segments:
|
380
388
|
- 0
|
381
|
-
hash:
|
389
|
+
hash: 2112599322304454342
|
382
390
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
383
391
|
none: false
|
384
392
|
requirements:
|
@@ -398,6 +406,7 @@ test_files:
|
|
398
406
|
- spec/rails_best_practices/core/configs_spec.rb
|
399
407
|
- spec/rails_best_practices/core/controllers_spec.rb
|
400
408
|
- spec/rails_best_practices/core/error_spec.rb
|
409
|
+
- spec/rails_best_practices/core/gems_spec.rb
|
401
410
|
- spec/rails_best_practices/core/helpers_spec.rb
|
402
411
|
- spec/rails_best_practices/core/klasses_spec.rb
|
403
412
|
- spec/rails_best_practices/core/mailers_spec.rb
|
@@ -414,6 +423,7 @@ test_files:
|
|
414
423
|
- spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb
|
415
424
|
- spec/rails_best_practices/prepares/config_prepare_spec.rb
|
416
425
|
- spec/rails_best_practices/prepares/controller_prepare_spec.rb
|
426
|
+
- spec/rails_best_practices/prepares/gemfile_prepare_spec.rb
|
417
427
|
- spec/rails_best_practices/prepares/helper_prepare_spec.rb
|
418
428
|
- spec/rails_best_practices/prepares/mailer_prepare_spec.rb
|
419
429
|
- spec/rails_best_practices/prepares/model_prepare_spec.rb
|
@@ -421,6 +431,7 @@ test_files:
|
|
421
431
|
- spec/rails_best_practices/prepares/schema_prepare_spec.rb
|
422
432
|
- spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb
|
423
433
|
- spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb
|
434
|
+
- spec/rails_best_practices/reviews/check_save_return_value_spec.rb
|
424
435
|
- spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb
|
425
436
|
- spec/rails_best_practices/reviews/hash_syntax_review_spec.rb
|
426
437
|
- spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb
|
@@ -454,4 +465,5 @@ test_files:
|
|
454
465
|
- spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb
|
455
466
|
- spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb
|
456
467
|
- spec/rails_best_practices/reviews/use_scope_access_review_spec.rb
|
468
|
+
- spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb
|
457
469
|
- spec/spec_helper.rb
|