rails_best_practices 0.6.6 → 0.6.7
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/Gemfile +0 -1
- data/README.md +28 -24
- data/Rakefile +0 -8
- data/install_supported_rubies.sh +2 -3
- data/lib/rails_best_practices.rb +8 -7
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +68 -0
- data/lib/rails_best_practices/core/checking_visitor.rb +18 -15
- data/lib/rails_best_practices/core/runner.rb +22 -12
- data/lib/rails_best_practices/core/visitable_sexp.rb +6 -2
- data/lib/rails_best_practices/prepares.rb +11 -0
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +33 -0
- data/lib/rails_best_practices/prepares/model_prepare.rb +60 -0
- data/lib/rails_best_practices/reviews.rb +23 -0
- data/lib/rails_best_practices/{checks/add_model_virtual_attribute_check.rb → reviews/add_model_virtual_attribute_review.rb} +6 -9
- data/lib/rails_best_practices/{checks/always_add_db_index_check.rb → reviews/always_add_db_index_review.rb} +9 -12
- data/lib/rails_best_practices/{checks/dry_bundler_in_capistrano_check.rb → reviews/dry_bundler_in_capistrano_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/isolate_seed_data_check.rb → reviews/isolate_seed_data_review.rb} +11 -14
- data/lib/rails_best_practices/{checks/keep_finders_on_their_own_model_check.rb → reviews/keep_finders_on_their_own_model_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/law_of_demeter_check.rb → reviews/law_of_demeter_review.rb} +10 -14
- data/lib/rails_best_practices/{checks/move_code_into_controller_check.rb → reviews/move_code_into_controller_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/move_code_into_helper_check.rb → reviews/move_code_into_helper_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_code_into_model_check.rb → reviews/move_code_into_model_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_finder_to_named_scope_check.rb → reviews/move_finder_to_named_scope_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/move_model_logic_into_model_check.rb → reviews/move_model_logic_into_model_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/needless_deep_nesting_check.rb → reviews/needless_deep_nesting_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/not_use_default_route_check.rb → reviews/not_use_default_route_review.rb} +7 -10
- data/lib/rails_best_practices/{checks/overuse_route_customizations_check.rb → reviews/overuse_route_customizations_review.rb} +10 -13
- data/lib/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check.rb → reviews/replace_complex_creation_with_factory_method_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/replace_instance_variable_with_local_variable_check.rb → reviews/replace_instance_variable_with_local_variable_review.rb} +7 -10
- data/lib/rails_best_practices/reviews/review.rb +92 -0
- data/lib/rails_best_practices/{checks/use_before_filter_check.rb → reviews/use_before_filter_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_model_association_check.rb → reviews/use_model_association_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_observer_check.rb → reviews/use_observer_review.rb} +14 -41
- data/lib/rails_best_practices/{checks/use_query_attribute_check.rb → reviews/use_query_attribute_review.rb} +20 -16
- data/lib/rails_best_practices/{checks/use_say_with_time_in_migrations_check.rb → reviews/use_say_with_time_in_migrations_review.rb} +8 -11
- data/lib/rails_best_practices/{checks/use_scope_access_check.rb → reviews/use_scope_access_review.rb} +8 -11
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +3 -2
- data/rails_best_practices.yml +22 -22
- data/rake_rubies.sh +3 -2
- data/spec/rails_best_practices/core/check_spec.rb +41 -0
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +78 -0
- data/spec/rails_best_practices/core/visitable_sexp_spec.rb +39 -36
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +14 -0
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +22 -0
- data/spec/rails_best_practices/{checks/add_model_virtual_attribute_check_spec.rb → reviews/add_model_virtual_attribute_review_spec.rb} +17 -25
- data/spec/rails_best_practices/{checks/always_add_db_index_check_spec.rb → reviews/always_add_db_index_review_spec.rb} +28 -41
- data/spec/rails_best_practices/{checks/dry_bundler_in_capistrano_check_spec.rb → reviews/dry_bundler_in_capistrano_review_spec.rb} +7 -11
- data/spec/rails_best_practices/{checks/isolate_seed_data_check_spec.rb → reviews/isolate_seed_data_review_spec.rb} +13 -20
- data/spec/rails_best_practices/{checks/keep_finders_on_their_own_model_check_spec.rb → reviews/keep_finders_on_their_own_model_review_spec.rb} +16 -24
- data/spec/rails_best_practices/{checks/law_of_demeter_check_spec.rb → reviews/law_of_demeter_review_spec.rb} +24 -26
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +29 -0
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +24 -0
- data/spec/rails_best_practices/{checks/move_code_into_model_check_spec.rb → reviews/move_code_into_model_review_spec.rb} +12 -18
- data/spec/rails_best_practices/{checks/move_finder_to_named_scope_check_spec.rb → reviews/move_finder_to_named_scope_review_spec.rb} +12 -16
- data/spec/rails_best_practices/{checks/move_model_logic_into_model_check_spec.rb → reviews/move_model_logic_into_model_review_spec.rb} +7 -11
- data/spec/rails_best_practices/{checks/needless_deep_nesting_check_spec.rb → reviews/needless_deep_nesting_review_spec.rb} +26 -37
- data/spec/rails_best_practices/{checks/not_use_default_route_check_spec.rb → reviews/not_use_default_route_review_spec.rb} +13 -19
- data/spec/rails_best_practices/{checks/overuse_route_customizations_check_spec.rb → reviews/overuse_route_customizations_review_spec.rb} +27 -39
- data/spec/rails_best_practices/{checks/replace_complex_creation_with_factory_method_check_spec.rb → reviews/replace_complex_creation_with_factory_method_review_spec.rb} +10 -15
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +31 -0
- data/spec/rails_best_practices/reviews/review_spec.rb +11 -0
- data/spec/rails_best_practices/{checks/use_before_filter_check_spec.rb → reviews/use_before_filter_review_spec.rb} +11 -17
- data/spec/rails_best_practices/{checks/use_model_association_check_spec.rb → reviews/use_model_association_review_spec.rb} +12 -18
- data/spec/rails_best_practices/{checks/use_observer_check_spec.rb → reviews/use_observer_review_spec.rb} +28 -29
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +190 -0
- data/spec/rails_best_practices/{checks/use_say_with_time_in_migrations_check_spec.rb → reviews/use_say_with_time_in_migrations_review_spec.rb} +14 -23
- data/spec/rails_best_practices/{checks/use_scope_access_check_spec.rb → reviews/use_scope_access_review_spec.rb} +28 -37
- data/spec/rails_best_practices_spec.rb +4 -4
- data/spec/spec_helper.rb +1 -0
- metadata +128 -102
- data/lib/rails_best_practices/checks.rb +0 -23
- data/lib/rails_best_practices/checks/check.rb +0 -203
- data/spec/rails_best_practices/checks/check_spec.rb +0 -57
- data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +0 -33
- data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +0 -28
- data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +0 -36
- data/spec/rails_best_practices/checks/use_query_attribute_check_spec.rb +0 -192
@@ -1,23 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/checks/move_finder_to_named_scope_check'
|
3
|
-
require 'rails_best_practices/checks/use_model_association_check'
|
4
|
-
require 'rails_best_practices/checks/use_scope_access_check'
|
5
|
-
require 'rails_best_practices/checks/add_model_virtual_attribute_check'
|
6
|
-
require 'rails_best_practices/checks/replace_complex_creation_with_factory_method_check'
|
7
|
-
require 'rails_best_practices/checks/move_model_logic_into_model_check'
|
8
|
-
require 'rails_best_practices/checks/overuse_route_customizations_check'
|
9
|
-
require 'rails_best_practices/checks/needless_deep_nesting_check'
|
10
|
-
require 'rails_best_practices/checks/not_use_default_route_check'
|
11
|
-
require 'rails_best_practices/checks/keep_finders_on_their_own_model_check'
|
12
|
-
require 'rails_best_practices/checks/law_of_demeter_check'
|
13
|
-
require 'rails_best_practices/checks/use_observer_check'
|
14
|
-
require 'rails_best_practices/checks/isolate_seed_data_check'
|
15
|
-
require 'rails_best_practices/checks/always_add_db_index_check'
|
16
|
-
require 'rails_best_practices/checks/use_before_filter_check'
|
17
|
-
require 'rails_best_practices/checks/move_code_into_controller_check'
|
18
|
-
require 'rails_best_practices/checks/move_code_into_model_check'
|
19
|
-
require 'rails_best_practices/checks/move_code_into_helper_check'
|
20
|
-
require 'rails_best_practices/checks/replace_instance_variable_with_local_variable_check'
|
21
|
-
require 'rails_best_practices/checks/dry_bundler_in_capistrano_check'
|
22
|
-
require 'rails_best_practices/checks/use_say_with_time_in_migrations_check'
|
23
|
-
require 'rails_best_practices/checks/use_query_attribute_check'
|
@@ -1,203 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/core/error'
|
3
|
-
|
4
|
-
module RailsBestPractices
|
5
|
-
module Checks
|
6
|
-
# A Check class that takes charge of reviewing one rails best practice.
|
7
|
-
# One check contains two process:
|
8
|
-
# 1. prepare process (optional), in this process, one check will do some preparation, such as analyzing the model associations.The check only does the preparation for the nodes (defined in interesting_prepare_nodes) in the files (defined in interesting_prepare_files).
|
9
|
-
# 2. review process, in this process, one check will really review your rails codes. The check only review the nodes (defined in interesting_review_nodes) in the files # (defined in interesting_review_files).
|
10
|
-
class Check
|
11
|
-
# only nodes whose node_type is in NODE_TYPE will be reviewed.
|
12
|
-
NODE_TYPES = [:call, :defn, :defs, :if, :class, :lasgn, :iasgn, :ivar, :lvar, :block, :iter, :const]
|
13
|
-
|
14
|
-
CONTROLLER_FILES = /_controller\.rb$/
|
15
|
-
MIGRATION_FILES = /db\/migrate\/.*\.rb$/
|
16
|
-
MODEL_FILES = /models\/.*\.rb$/
|
17
|
-
MAILER_FILES = /models\/.*\.rb$|mailers\/.*\.rb/
|
18
|
-
VIEW_FILES = /views\/.*\.(erb|haml)$/
|
19
|
-
PARTIAL_VIEW_FILES = /views\/.*\/_.*\.(erb|haml)$/
|
20
|
-
ROUTE_FILE = /config\/routes.rb/
|
21
|
-
|
22
|
-
attr_reader :errors
|
23
|
-
|
24
|
-
def initialize
|
25
|
-
@errors = []
|
26
|
-
end
|
27
|
-
|
28
|
-
# define default interesting_prepare_nodes, interesting_review_nodes, interesting_prepare_files and interesting_review_files.
|
29
|
-
[:prepare, :review].each do |process|
|
30
|
-
class_eval <<-EOS
|
31
|
-
def interesting_#{process}_nodes # def interesting_review_nodes
|
32
|
-
[] # []
|
33
|
-
end # end
|
34
|
-
#
|
35
|
-
def interesting_#{process}_files # def interesting_review_files
|
36
|
-
/.*/ # /.*/
|
37
|
-
end # end
|
38
|
-
EOS
|
39
|
-
end
|
40
|
-
|
41
|
-
# define method prepare_node_start, prepare_node_end, review_node_start and review_node_end.
|
42
|
-
#
|
43
|
-
# they delegate the node to special process method, like
|
44
|
-
#
|
45
|
-
# review_node_start(call_node) => review_start_call(call_node)
|
46
|
-
# review_node_end(defn_node) => review_end_defn(defn_node)
|
47
|
-
# prepare_node_start(calss_node) => prepare_start_class(class_node)
|
48
|
-
# prepare_node_end(if_node) => prepare_end_if(if_node)
|
49
|
-
[:prepare, :review].each do |process|
|
50
|
-
class_eval <<-EOS
|
51
|
-
def #{process}_node_start(node) # def review_node_start(node)
|
52
|
-
@node = node # @node = node
|
53
|
-
method = "#{process}_start_" + node.node_type.to_s # method = "review_start_" + node.node_type.to_s
|
54
|
-
self.send(method, node) # self.send(method, node)
|
55
|
-
end # end
|
56
|
-
#
|
57
|
-
def #{process}_node_end(node) # def review_node_end(node)
|
58
|
-
@node = node # @node = node
|
59
|
-
method = "#{process}_end_" + node.node_type.to_s # method = "review_end_" + node.node_type.to_s
|
60
|
-
self.send(method, node) # self.send(method, node)
|
61
|
-
end # end
|
62
|
-
EOS
|
63
|
-
end
|
64
|
-
|
65
|
-
# method_missing to catch all start and end process for each node type, like
|
66
|
-
#
|
67
|
-
# prepare_start_defn
|
68
|
-
# prepare_end_defn
|
69
|
-
# review_start_call
|
70
|
-
# review_end_call
|
71
|
-
#
|
72
|
-
# if there is a ""debug"" method defined in check, each node will be output.
|
73
|
-
def method_missing(method_name, *args)
|
74
|
-
if method_name.to_s =~ /^(prepare|review)_start_/
|
75
|
-
p args if respond_to?(:debug)
|
76
|
-
elsif method_name.to_s =~ /^(prepare|review)_end_/
|
77
|
-
# nothing to do
|
78
|
-
else
|
79
|
-
super
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# remember the model names and model associations in prepare process.
|
84
|
-
def self.prepare_model_associations
|
85
|
-
class_eval <<-EOS
|
86
|
-
def initialize
|
87
|
-
super
|
88
|
-
@klazzes = []
|
89
|
-
@associations = {}
|
90
|
-
end
|
91
|
-
|
92
|
-
def interesting_prepare_nodes
|
93
|
-
[:class, :call]
|
94
|
-
end
|
95
|
-
|
96
|
-
def interesting_prepare_files
|
97
|
-
MODEL_FILES
|
98
|
-
end
|
99
|
-
|
100
|
-
# check class node to remember all class name in prepare process.
|
101
|
-
#
|
102
|
-
# the remembered class names (@klazzes) are like
|
103
|
-
# [ :User, :Post ]
|
104
|
-
def prepare_start_class(node)
|
105
|
-
remember_klazz(node)
|
106
|
-
end
|
107
|
-
|
108
|
-
# check call node to remember all assoication names in prepare process.
|
109
|
-
#
|
110
|
-
# the remembered association names (@associations) are like
|
111
|
-
# { :User => [":projects", ":location"], :Post => [":comments"] }
|
112
|
-
def prepare_start_call(node)
|
113
|
-
remember_association(node) if association_methods.include? node.message
|
114
|
-
end
|
115
|
-
|
116
|
-
# remember class models, just the subject of class node.
|
117
|
-
def remember_klazz(class_node)
|
118
|
-
@klazzes << class_node.class_name
|
119
|
-
end
|
120
|
-
|
121
|
-
# remember associations, with class to association names.
|
122
|
-
def remember_association(association_node)
|
123
|
-
@associations[@klazzes.last] ||= []
|
124
|
-
@associations[@klazzes.last] << association_node.arguments[1].to_s
|
125
|
-
end
|
126
|
-
|
127
|
-
# default rails association methods.
|
128
|
-
def association_methods
|
129
|
-
[:belongs_to, :has_one, :has_many, :has_and_belongs_to_many]
|
130
|
-
end
|
131
|
-
|
132
|
-
EOS
|
133
|
-
end
|
134
|
-
|
135
|
-
# add error if source code violates rails best practice.
|
136
|
-
# error is the string message for violation of the rails best practice
|
137
|
-
# file is the filename of source code
|
138
|
-
# line is the line number of the source code which is reviewing
|
139
|
-
def add_error(error, file = @node.file, line = @node.line)
|
140
|
-
@errors << RailsBestPractices::Core::Error.new("#{file}", "#{line}", error, url)
|
141
|
-
end
|
142
|
-
|
143
|
-
# remember use count for the local or instance variable in the call or attrasgn node.
|
144
|
-
#
|
145
|
-
# find the local variable or instance variable in the call or attrasgn node,
|
146
|
-
# then save it to as key in @variable_use_count hash, and add the call count (hash value).
|
147
|
-
def remember_variable_use_count(node)
|
148
|
-
variable_node = variable(node)
|
149
|
-
if variable_node
|
150
|
-
variable_use_count[variable_node] ||= 0
|
151
|
-
variable_use_count[variable_node] += 1
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
# return @variable_use_count hash.
|
156
|
-
def variable_use_count
|
157
|
-
@variable_use_count ||= {}
|
158
|
-
end
|
159
|
-
|
160
|
-
# reset @variable_use_count hash.
|
161
|
-
def reset_variable_use_count
|
162
|
-
@variable_use_count = nil
|
163
|
-
end
|
164
|
-
|
165
|
-
# find local variable or instance variable in the most inner call node, e.g.
|
166
|
-
#
|
167
|
-
# if the call node is
|
168
|
-
#
|
169
|
-
# s(:call, s(:ivar, :@post), :editors, s(:arglist)),
|
170
|
-
#
|
171
|
-
# or it is
|
172
|
-
#
|
173
|
-
# s(:call,
|
174
|
-
# s(:call, s(:ivar, :@post), :editors, s(:arglist)),
|
175
|
-
# :include?,
|
176
|
-
# s(:arglist, s(:call, nil, :current_user, s(:arglist)))
|
177
|
-
# )
|
178
|
-
#
|
179
|
-
# then the variable both are s(:ivar, :@post).
|
180
|
-
#
|
181
|
-
def variable(node)
|
182
|
-
while node.subject.node_type == :call
|
183
|
-
node = node.subject
|
184
|
-
end
|
185
|
-
subject_node = node.subject
|
186
|
-
if [:ivar, :lvar].include?(subject_node.node_type) and subject_node[1] != :_erbout
|
187
|
-
subject_node
|
188
|
-
else
|
189
|
-
nil
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
# compare two sexp nodes' to_s.
|
194
|
-
# equal?(":test", :test) => true
|
195
|
-
# equai?("@test", :test) => true
|
196
|
-
def equal?(node, expected_node)
|
197
|
-
actual = node.to_s.downcase
|
198
|
-
expected = expected_node.to_s.downcase
|
199
|
-
actual == expected || actual == ':' + expected || actual == '@' + expected
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::Check do
|
4
|
-
before :each do
|
5
|
-
@check = RailsBestPractices::Checks::Check.new
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should get empty interesting prepare nodes" do
|
9
|
-
@check.interesting_prepare_nodes.should == []
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should get empty interesting review nodes" do
|
13
|
-
@check.interesting_review_nodes.should == []
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should match all files of interesting prepare files" do
|
17
|
-
@check.interesting_prepare_files.should == /.*/
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should match all files of interesting review files" do
|
21
|
-
@check.interesting_review_files.should == /.*/
|
22
|
-
end
|
23
|
-
|
24
|
-
context "review_node_start" do
|
25
|
-
it "should call review_start_if" do
|
26
|
-
node = stub(:node_type => :if)
|
27
|
-
@check.should_receive(:send).with("review_start_if", node)
|
28
|
-
@check.review_node_start(node)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should call review_start_call" do
|
32
|
-
node = stub(:node_type => :call)
|
33
|
-
@check.should_receive(:send).with("review_start_call", node)
|
34
|
-
@check.review_node_start(node)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "review_node_end" do
|
39
|
-
it "should call review_end_if" do
|
40
|
-
node = stub(:node_type => :if)
|
41
|
-
@check.should_receive(:send).with("review_end_if", node)
|
42
|
-
@check.review_node_end(node)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should call review_end_call" do
|
46
|
-
node = stub(:node_type => :call)
|
47
|
-
@check.should_receive(:send).with("review_end_call", node)
|
48
|
-
@check.review_node_end(node)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context "equal?" do
|
53
|
-
it "should be true when compare symbol string with symbol" do
|
54
|
-
@check.equal?(":test", :test).should be_true
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::MoveCodeIntoControllerCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoControllerCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should move code into controller" do
|
9
|
-
content = <<-EOF
|
10
|
-
<% @posts = Post.find(:all) %>
|
11
|
-
<% @posts.each do |post| %>
|
12
|
-
<%=h post.title %>
|
13
|
-
<%=h post.content %>
|
14
|
-
<% end %>
|
15
|
-
EOF
|
16
|
-
@runner.review('app/views/posts/index.html.erb', content)
|
17
|
-
errors = @runner.errors
|
18
|
-
errors.should_not be_empty
|
19
|
-
errors[0].to_s.should == "app/views/posts/index.html.erb:1 - move code into controller"
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should not move code into controller" do
|
23
|
-
content = <<-EOF
|
24
|
-
<% @posts.each do |post| %>
|
25
|
-
<%=h post.title %>
|
26
|
-
<%=h post.content %>
|
27
|
-
<% end %>
|
28
|
-
EOF
|
29
|
-
@runner.review('app/views/posts/index.html.erb', content)
|
30
|
-
errors = @runner.errors
|
31
|
-
errors.should be_empty
|
32
|
-
end
|
33
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::MoveCodeIntoHelperCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoHelperCheck.new('array_count' => 2))
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should move code into helper" do
|
9
|
-
content = <<-EOF
|
10
|
-
<%= select_tag :state, options_for_select( [[t(:draft), "draft"],
|
11
|
-
[t(:published), "published"]],
|
12
|
-
params[:default_state] ) %>
|
13
|
-
EOF
|
14
|
-
@runner.review('app/views/posts/show.html.erb', content)
|
15
|
-
errors = @runner.errors
|
16
|
-
errors.should_not be_empty
|
17
|
-
errors[0].to_s.should == "app/views/posts/show.html.erb:3 - move code into helper (array_count >= 2)"
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should not move code into helper with simple arguments" do
|
21
|
-
content = <<-EOF
|
22
|
-
<%= select_tag :state, options_for_select( Post.STATES ) %>
|
23
|
-
EOF
|
24
|
-
@runner.review('app/views/posts/show.html.erb', content)
|
25
|
-
errors = @runner.errors
|
26
|
-
errors.should be_empty
|
27
|
-
end
|
28
|
-
end
|
data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should replace instance variable with local varialbe" do
|
9
|
-
content = <<-EOF
|
10
|
-
<%= @post.title %>
|
11
|
-
EOF
|
12
|
-
@runner.review('app/views/posts/_post.html.erb', content)
|
13
|
-
errors = @runner.errors
|
14
|
-
errors.should_not be_empty
|
15
|
-
errors[0].to_s.should == "app/views/posts/_post.html.erb:1 - replace instance variable with local variable"
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should replace instance variable with local varialbe in haml file" do
|
19
|
-
content = <<-EOF
|
20
|
-
= @post.title
|
21
|
-
EOF
|
22
|
-
@runner.review('app/views/posts/_post.html.haml', content)
|
23
|
-
errors = @runner.errors
|
24
|
-
errors.should_not be_empty
|
25
|
-
errors[0].to_s.should == "app/views/posts/_post.html.haml:1 - replace instance variable with local variable"
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should not replace instance variable with local varialbe" do
|
29
|
-
content = <<-EOF
|
30
|
-
<%= post.title %>
|
31
|
-
EOF
|
32
|
-
@runner.review('app/views/posts/_post.html.erb', content)
|
33
|
-
errors = @runner.errors
|
34
|
-
errors.should be_empty
|
35
|
-
end
|
36
|
-
end
|
@@ -1,192 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::UseQueryAttributeCheck do
|
4
|
-
|
5
|
-
before(:each) do
|
6
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::UseQueryAttributeCheck.new)
|
7
|
-
|
8
|
-
content = <<-EOF
|
9
|
-
class User < ActiveRecord::Base
|
10
|
-
has_many :projects
|
11
|
-
belongs_to :location
|
12
|
-
has_one :phone
|
13
|
-
|
14
|
-
belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
|
15
|
-
end
|
16
|
-
EOF
|
17
|
-
@runner.prepare('app/models/user.rb', content)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should use query attribute by blank call" do
|
21
|
-
content = <<-EOF
|
22
|
-
<% if @user.login.blank? %>
|
23
|
-
<%= link_to 'login', new_session_path %>
|
24
|
-
<% end %>
|
25
|
-
EOF
|
26
|
-
@runner.review('app/views/users/show.html.erb', content)
|
27
|
-
errors = @runner.errors
|
28
|
-
errors.should_not be_empty
|
29
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should use query attribute by comparing empty string" do
|
33
|
-
content = <<-EOF
|
34
|
-
<% if @user.login == "" %>
|
35
|
-
<%= link_to 'login', new_session_path %>
|
36
|
-
<% end %>
|
37
|
-
EOF
|
38
|
-
@runner.review('app/views/users/show.html.erb', content)
|
39
|
-
errors = @runner.errors
|
40
|
-
errors.should_not be_empty
|
41
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should use query attribute by nil call" do
|
45
|
-
content = <<-EOF
|
46
|
-
<% if @user.login.nil? %>
|
47
|
-
<%= link_to 'login', new_session_path %>
|
48
|
-
<% end %>
|
49
|
-
EOF
|
50
|
-
@runner.review('app/views/users/show.html.erb', content)
|
51
|
-
errors = @runner.errors
|
52
|
-
errors.should_not be_empty
|
53
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should use query attribute by present call" do
|
57
|
-
content = <<-EOF
|
58
|
-
<% if @user.login.present? %>
|
59
|
-
<%= @user.login %>
|
60
|
-
<% end %>
|
61
|
-
EOF
|
62
|
-
@runner.review('app/views/users/show.html.erb', content)
|
63
|
-
errors = @runner.errors
|
64
|
-
errors.should_not be_empty
|
65
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should use query attribute within and conditions" do
|
69
|
-
content = <<-EOF
|
70
|
-
<% if @user.active? and @user.login.present? %>
|
71
|
-
<%= @user.login %>
|
72
|
-
<% end %>
|
73
|
-
EOF
|
74
|
-
@runner.review('app/views/users/show.html.erb', content)
|
75
|
-
errors = @runner.errors
|
76
|
-
errors.should_not be_empty
|
77
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should use query attribute within or conditions" do
|
81
|
-
content = <<-EOF
|
82
|
-
<% if @user.active? or @user.login != "" %>
|
83
|
-
<%= @user.login %>
|
84
|
-
<% end %>
|
85
|
-
EOF
|
86
|
-
@runner.review('app/views/users/show.html.erb', content)
|
87
|
-
errors = @runner.errors
|
88
|
-
errors.should_not be_empty
|
89
|
-
errors[0].to_s.should == "app/views/users/show.html.erb:1 - use query attribute (@user.login?)"
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should not use query attribute" do
|
93
|
-
content = <<-EOF
|
94
|
-
<% if @user.login? %>
|
95
|
-
<%= @user.login %>
|
96
|
-
<% end %>
|
97
|
-
EOF
|
98
|
-
@runner.review('app/views/users/show.html.erb', content)
|
99
|
-
errors = @runner.errors
|
100
|
-
errors.should be_empty
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should not review for pluralize attribute" do
|
104
|
-
content = <<-EOF
|
105
|
-
<% if @user.roles.blank? %>
|
106
|
-
<%= @user.login %>
|
107
|
-
<% end %>
|
108
|
-
EOF
|
109
|
-
@runner.review('app/views/users/show.html.erb', content)
|
110
|
-
errors = @runner.errors
|
111
|
-
errors.should be_empty
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should not review non model class" do
|
115
|
-
content = <<-EOF
|
116
|
-
<% if @person.login.present? %>
|
117
|
-
<%= @person.login %>
|
118
|
-
<% end %>
|
119
|
-
EOF
|
120
|
-
@runner.review('app/views/users/show.html.erb', content)
|
121
|
-
errors = @runner.errors
|
122
|
-
errors.should be_empty
|
123
|
-
end
|
124
|
-
|
125
|
-
context "association" do
|
126
|
-
it "should not review belongs_to association" do
|
127
|
-
content = <<-EOF
|
128
|
-
<% if @user.location.present? %>
|
129
|
-
<%= @user.location.name %>
|
130
|
-
<% end %>
|
131
|
-
EOF
|
132
|
-
@runner.review('app/views/users/show.html.erb', content)
|
133
|
-
errors = @runner.errors
|
134
|
-
errors.should be_empty
|
135
|
-
end
|
136
|
-
|
137
|
-
it "should not review belongs_to category" do
|
138
|
-
content = <<-EOF
|
139
|
-
<% if @user.category.present? %>
|
140
|
-
<%= @user.category.name %>
|
141
|
-
<% end %>
|
142
|
-
EOF
|
143
|
-
@runner.review('app/views/users/show.html.erb', content)
|
144
|
-
errors = @runner.errors
|
145
|
-
errors.should be_empty
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should not review has_one association" do
|
149
|
-
content = <<-EOF
|
150
|
-
<% if @user.phone.present? %>
|
151
|
-
<%= @user.phone.number %>
|
152
|
-
<% end %>
|
153
|
-
EOF
|
154
|
-
@runner.review('app/views/users/show.html.erb', content)
|
155
|
-
errors = @runner.errors
|
156
|
-
errors.should be_empty
|
157
|
-
end
|
158
|
-
|
159
|
-
it "should not review has_many association" do
|
160
|
-
content = <<-EOF
|
161
|
-
<% if @user.projects.present? %>
|
162
|
-
<%= @user.projects.first.name %>
|
163
|
-
<% end %>
|
164
|
-
EOF
|
165
|
-
@runner.review('app/views/users/show.html.erb', content)
|
166
|
-
errors = @runner.errors
|
167
|
-
errors.should be_empty
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should not review for class method" do
|
172
|
-
content = <<-EOF
|
173
|
-
<% if User.name.present? %>
|
174
|
-
<%= User.name %>
|
175
|
-
<% end %>
|
176
|
-
EOF
|
177
|
-
@runner.review('app/views/users/show.html.erb', content)
|
178
|
-
errors = @runner.errors
|
179
|
-
errors.should be_empty
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should not review for non attribute call" do
|
183
|
-
content = <<-EOF
|
184
|
-
if @user.login(false).nil?
|
185
|
-
puts @user.login(false)
|
186
|
-
end
|
187
|
-
EOF
|
188
|
-
@runner.review('app/models/users_controller.rb', content)
|
189
|
-
errors = @runner.errors
|
190
|
-
errors.should be_empty
|
191
|
-
end
|
192
|
-
end
|