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
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails_best_practices/reviews/move_finder_to_named_scope_review'
|
3
|
+
require 'rails_best_practices/reviews/use_model_association_review'
|
4
|
+
require 'rails_best_practices/reviews/use_scope_access_review'
|
5
|
+
require 'rails_best_practices/reviews/add_model_virtual_attribute_review'
|
6
|
+
require 'rails_best_practices/reviews/replace_complex_creation_with_factory_method_review'
|
7
|
+
require 'rails_best_practices/reviews/move_model_logic_into_model_review'
|
8
|
+
require 'rails_best_practices/reviews/overuse_route_customizations_review'
|
9
|
+
require 'rails_best_practices/reviews/needless_deep_nesting_review'
|
10
|
+
require 'rails_best_practices/reviews/not_use_default_route_review'
|
11
|
+
require 'rails_best_practices/reviews/keep_finders_on_their_own_model_review'
|
12
|
+
require 'rails_best_practices/reviews/law_of_demeter_review'
|
13
|
+
require 'rails_best_practices/reviews/use_observer_review'
|
14
|
+
require 'rails_best_practices/reviews/isolate_seed_data_review'
|
15
|
+
require 'rails_best_practices/reviews/always_add_db_index_review'
|
16
|
+
require 'rails_best_practices/reviews/use_before_filter_review'
|
17
|
+
require 'rails_best_practices/reviews/move_code_into_controller_review'
|
18
|
+
require 'rails_best_practices/reviews/move_code_into_model_review'
|
19
|
+
require 'rails_best_practices/reviews/move_code_into_helper_review'
|
20
|
+
require 'rails_best_practices/reviews/replace_instance_variable_with_local_variable_review'
|
21
|
+
require 'rails_best_practices/reviews/dry_bundler_in_capistrano_review'
|
22
|
+
require 'rails_best_practices/reviews/use_say_with_time_in_migrations_review'
|
23
|
+
require 'rails_best_practices/reviews/use_query_attribute_review'
|
@@ -1,17 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
5
|
+
module Reviews
|
6
6
|
# Make sure to add a model virual attribute to simplify model creation.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/4-add-model-virtual-attribute
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# check method define nodes in all controller files,
|
17
14
|
# if there are more than one [] method calls with the same subject and arguments,
|
@@ -26,16 +23,16 @@ module RailsBestPractices
|
|
26
23
|
# end
|
27
24
|
#
|
28
25
|
# then the model needs to add a virtual attribute.
|
29
|
-
class
|
26
|
+
class AddModelVirtualAttributeReview < Review
|
30
27
|
def url
|
31
28
|
"http://rails-bestpractices.com/posts/4-add-model-virtual-attribute"
|
32
29
|
end
|
33
30
|
|
34
|
-
def
|
31
|
+
def interesting_nodes
|
35
32
|
[:defn]
|
36
33
|
end
|
37
34
|
|
38
|
-
def
|
35
|
+
def interesting_files
|
39
36
|
CONTROLLER_FILES
|
40
37
|
end
|
41
38
|
|
@@ -53,7 +50,7 @@ module RailsBestPractices
|
|
53
50
|
# 5. and the subject of save or save! call node should be the same with the subject of attribute assignment nodes
|
54
51
|
#
|
55
52
|
# then the attribute assignment nodes can add model virtual attribute instead.
|
56
|
-
def
|
53
|
+
def start_defn(node)
|
57
54
|
@attrasgns = {}
|
58
55
|
node.recursive_children do |child|
|
59
56
|
case child.node_type
|
@@ -1,17 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review db/schema.rb file to make sure every reference key has a database index.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/21-always-add-db-index
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# only check the call nodes and at the end of iter node in db/schema file,
|
17
14
|
# if the subject of call node is :create_table, then remember the table names
|
@@ -26,16 +23,16 @@ module RailsBestPractices
|
|
26
23
|
#
|
27
24
|
# if there are any foreign keys not existed in index columns,
|
28
25
|
# then the foreign keys should add db index.
|
29
|
-
class
|
26
|
+
class AlwaysAddDbIndexReview < Review
|
30
27
|
def url
|
31
28
|
"http://rails-bestpractices.com/posts/21-always-add-db-index"
|
32
29
|
end
|
33
30
|
|
34
|
-
def
|
31
|
+
def interesting_nodes
|
35
32
|
[:call, :iter]
|
36
33
|
end
|
37
34
|
|
38
|
-
def
|
35
|
+
def interesting_files
|
39
36
|
/db\/schema.rb/
|
40
37
|
end
|
41
38
|
|
@@ -46,7 +43,7 @@ module RailsBestPractices
|
|
46
43
|
@table_nodes = {}
|
47
44
|
end
|
48
45
|
|
49
|
-
# check call node
|
46
|
+
# check call node.
|
50
47
|
#
|
51
48
|
# if the message of call node is :create_table,
|
52
49
|
# then remember the table name (@table_nodes) like
|
@@ -75,7 +72,7 @@ module RailsBestPractices
|
|
75
72
|
# "comments" =>
|
76
73
|
# ["post_id", "user_id"]
|
77
74
|
# }
|
78
|
-
def
|
75
|
+
def start_call(node)
|
79
76
|
case node.message
|
80
77
|
when :create_table
|
81
78
|
remember_table_nodes(node)
|
@@ -113,7 +110,7 @@ module RailsBestPractices
|
|
113
110
|
#
|
114
111
|
# if there are any foreign keys not existed in index columns,
|
115
112
|
# then we should add db index for that foreign keys.
|
116
|
-
def
|
113
|
+
def end_iter(node)
|
117
114
|
first_node = node.subject
|
118
115
|
if :call == first_node.node_type && s(:colon2, s(:const, :ActiveRecord), :Schema) == first_node.subject
|
119
116
|
remove_only_type_foreign_keys
|
@@ -1,36 +1,33 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review config/deploy.rb file to make sure using the bundler's capistrano recipe.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/51-dry-bundler-in-capistrano
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# only check the call nodes to see if there is bundler namespace in config/deploy.rb file,
|
17
14
|
#
|
18
15
|
# if the message of call node is :namespace and the arguments of the call node is :bundler,
|
19
16
|
# then it should use bundler's capistrano recipe.
|
20
|
-
class
|
17
|
+
class DryBundlerInCapistranoReview < Review
|
21
18
|
def url
|
22
19
|
"http://rails-bestpractices.com/posts/51-dry-bundler-in-capistrano"
|
23
20
|
end
|
24
21
|
|
25
|
-
def
|
22
|
+
def interesting_nodes
|
26
23
|
[:call]
|
27
24
|
end
|
28
25
|
|
29
|
-
def
|
26
|
+
def interesting_files
|
30
27
|
/config\/deploy.rb/
|
31
28
|
end
|
32
29
|
|
33
|
-
# check call node
|
30
|
+
# check call node to see if it is with message :namespace and arguments :bundler.
|
34
31
|
#
|
35
32
|
# the ruby code is
|
36
33
|
#
|
@@ -41,7 +38,7 @@ module RailsBestPractices
|
|
41
38
|
# then the call node is as follows
|
42
39
|
#
|
43
40
|
# s(:call, nil, :namespace, s(:arglist, s(:lit, :bundler)))
|
44
|
-
def
|
41
|
+
def start_call(node)
|
45
42
|
if :namespace == node.message and equal?(node.arguments[1], "bundler")
|
46
43
|
add_error "dry bundler in capistrano"
|
47
44
|
end
|
@@ -1,17 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
5
|
+
module Reviews
|
6
6
|
# Make sure not to insert data in migration, move them to seed file.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/20-isolating-seed-data.
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# 1. check all local assignment and instance assignment nodes,
|
17
14
|
# if the right value is a call node with message :new,
|
@@ -23,16 +20,16 @@ module RailsBestPractices
|
|
23
20
|
# if the message is :save or :save!,
|
24
21
|
# and the subject is included in new variables,
|
25
22
|
# then it should be isolated to db seed.
|
26
|
-
class
|
23
|
+
class IsolateSeedDataReview < Review
|
27
24
|
def url
|
28
25
|
"http://rails-bestpractices.com/posts/20-isolating-seed-data"
|
29
26
|
end
|
30
27
|
|
31
|
-
def
|
28
|
+
def interesting_nodes
|
32
29
|
[:call, :lasgn, :iasgn]
|
33
30
|
end
|
34
31
|
|
35
|
-
def
|
32
|
+
def interesting_files
|
36
33
|
MIGRATION_FILES
|
37
34
|
end
|
38
35
|
|
@@ -41,23 +38,23 @@ module RailsBestPractices
|
|
41
38
|
@new_variables = []
|
42
39
|
end
|
43
40
|
|
44
|
-
# check local assignment node
|
41
|
+
# check local assignment node.
|
45
42
|
#
|
46
43
|
# if the right value of the node is a call node with :new message,
|
47
44
|
# then remember it as new variables (@new_variables).
|
48
|
-
def
|
45
|
+
def start_lasgn(node)
|
49
46
|
remember_new_variable(node)
|
50
47
|
end
|
51
48
|
|
52
|
-
# check instance assignment node
|
49
|
+
# check instance assignment node.
|
53
50
|
#
|
54
51
|
# if the right value of the node is a call node with :new message,
|
55
52
|
# then remember it as new variables (@new_variables).
|
56
|
-
def
|
53
|
+
def start_iasgn(node)
|
57
54
|
remember_new_variable(node)
|
58
55
|
end
|
59
56
|
|
60
|
-
# check the call node
|
57
|
+
# check the call node.
|
61
58
|
#
|
62
59
|
# if the message of the call node is :create or :create!,
|
63
60
|
# then you should isolate it to seed data.
|
@@ -65,7 +62,7 @@ module RailsBestPractices
|
|
65
62
|
# if the message of the call node is :save or :save!,
|
66
63
|
# and the subject of the call node is included in @new_variables,
|
67
64
|
# then you should isolate it to seed data.
|
68
|
-
def
|
65
|
+
def start_call(node)
|
69
66
|
if [:create, :create!].include? node.message
|
70
67
|
add_error("isolate seed data")
|
71
68
|
elsif [:save, :save!].include? node.message
|
@@ -1,17 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review model files to ake sure finders are on their own model.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/13-keep-finders-on-their-own-model.
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# check all call nodes in model files.
|
17
14
|
#
|
@@ -19,7 +16,7 @@ module RailsBestPractices
|
|
19
16
|
# and the it calls the other model,
|
20
17
|
# and there is a hash argument for finder,
|
21
18
|
# then it should keep finders on its own model.
|
22
|
-
class
|
19
|
+
class KeepFindersOnTheirOwnModelReview < Review
|
23
20
|
|
24
21
|
FINDERS = [:find, :all, :first, :last]
|
25
22
|
|
@@ -27,11 +24,11 @@ module RailsBestPractices
|
|
27
24
|
"http://rails-bestpractices.com/posts/13-keep-finders-on-their-own-model"
|
28
25
|
end
|
29
26
|
|
30
|
-
def
|
27
|
+
def interesting_nodes
|
31
28
|
[:call]
|
32
29
|
end
|
33
30
|
|
34
|
-
def
|
31
|
+
def interesting_files
|
35
32
|
MODEL_FILES
|
36
33
|
end
|
37
34
|
|
@@ -44,7 +41,7 @@ module RailsBestPractices
|
|
44
41
|
# 3. the any of its arguments is a hash (complex finder)
|
45
42
|
#
|
46
43
|
# then it should keep finders on its own model.
|
47
|
-
def
|
44
|
+
def start_call(node)
|
48
45
|
add_error "keep finders on their own model" if other_finder?(node)
|
49
46
|
end
|
50
47
|
|
data/lib/rails_best_practices/{checks/law_of_demeter_check.rb → reviews/law_of_demeter_review.rb}
RENAMED
@@ -1,16 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review to make sure not to avoid the law of demeter.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/15-the-law-of-demeter.
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
12
|
# Prepare process:
|
13
|
-
# only
|
13
|
+
# only review all model files to save model names and association names.
|
14
14
|
#
|
15
15
|
# Review process:
|
16
16
|
# check all method calls to see if there is method call to the association object.
|
@@ -18,19 +18,17 @@ module RailsBestPractices
|
|
18
18
|
# and whose message is an association of that model (also compare by name),
|
19
19
|
# and outer the call node, it is also a call node,
|
20
20
|
# then it violate the law of demeter.
|
21
|
-
class
|
22
|
-
|
23
|
-
prepare_model_associations
|
21
|
+
class LawOfDemeterReview < Review
|
24
22
|
|
25
23
|
def url
|
26
24
|
"http://rails-bestpractices.com/posts/15-the-law-of-demeter"
|
27
25
|
end
|
28
26
|
|
29
|
-
def
|
27
|
+
def interesting_nodes
|
30
28
|
[:call]
|
31
29
|
end
|
32
30
|
|
33
|
-
# check the call node
|
31
|
+
# check the call node,
|
34
32
|
#
|
35
33
|
# if the subject of the call node is also a call node,
|
36
34
|
# and the subject of the subject call node matchs one of the class names,
|
@@ -43,7 +41,7 @@ module RailsBestPractices
|
|
43
41
|
# )
|
44
42
|
#
|
45
43
|
# then it violates the law of demeter.
|
46
|
-
def
|
44
|
+
def start_call(node)
|
47
45
|
if [:lvar, :ivar].include?(node.subject.subject.node_type) && need_delegate?(node)
|
48
46
|
add_error "law of demeter"
|
49
47
|
end
|
@@ -67,10 +65,8 @@ module RailsBestPractices
|
|
67
65
|
# as you see the subject of subject of the call node is [:ivar, @invoice],
|
68
66
|
# and the message of subject of the call node is :user
|
69
67
|
def need_delegate?(node)
|
70
|
-
|
71
|
-
|
72
|
-
end
|
73
|
-
false
|
68
|
+
class_name = node.subject.subject.to_s(:remove_at => true).classify
|
69
|
+
association_methods.include? model_associations[class_name][node.subject.message.to_s]
|
74
70
|
end
|
75
71
|
|
76
72
|
# only check belongs_to and has_one association.
|
@@ -1,20 +1,17 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review a view file to make sure there is no finder, finder should be moved to controller.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/24-move-code-into-controller.
|
9
9
|
#
|
10
10
|
# Implementation:
|
11
11
|
#
|
12
|
-
# Prepare process:
|
13
|
-
# none
|
14
|
-
#
|
15
12
|
# Review process:
|
16
13
|
# only check all view files to see if there are finders, then the finders should be moved to controller.
|
17
|
-
class
|
14
|
+
class MoveCodeIntoControllerReview < Review
|
18
15
|
|
19
16
|
FINDERS = [:find, :all, :first, :last]
|
20
17
|
|
@@ -22,20 +19,20 @@ module RailsBestPractices
|
|
22
19
|
"http://rails-bestpractices.com/posts/24-move-code-into-controller"
|
23
20
|
end
|
24
21
|
|
25
|
-
def
|
22
|
+
def interesting_nodes
|
26
23
|
[:call]
|
27
24
|
end
|
28
25
|
|
29
|
-
def
|
26
|
+
def interesting_files
|
30
27
|
VIEW_FILES
|
31
28
|
end
|
32
29
|
|
33
|
-
# check call nodes
|
30
|
+
# check call nodes.
|
34
31
|
#
|
35
32
|
# if the subject of the call node is a constant,
|
36
33
|
# and the message of the call node is one of the :find, :all, :first and :last,
|
37
34
|
# then it is a finder and should be moved to controller.
|
38
|
-
def
|
35
|
+
def start_call(node)
|
39
36
|
add_error "move code into controller" if finder?(node)
|
40
37
|
end
|
41
38
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require 'rails_best_practices/
|
2
|
+
require 'rails_best_practices/reviews/review'
|
3
3
|
|
4
4
|
module RailsBestPractices
|
5
|
-
module
|
6
|
-
#
|
5
|
+
module Reviews
|
6
|
+
# Review a view file to make sure there is no complex options_for_select message call.
|
7
7
|
#
|
8
8
|
# See the best practice details here http://rails-bestpractices.com/posts/26-move-code-into-helper.
|
9
9
|
#
|
@@ -11,9 +11,6 @@ module RailsBestPractices
|
|
11
11
|
#
|
12
12
|
# Implementation:
|
13
13
|
#
|
14
|
-
# Prepare process:
|
15
|
-
# none
|
16
|
-
#
|
17
14
|
# Review process:
|
18
15
|
# check al method calls to see if there is a complex options_for_select helper.
|
19
16
|
#
|
@@ -21,16 +18,16 @@ module RailsBestPractices
|
|
21
18
|
# and the first argument of the call node is array,
|
22
19
|
# and the size of the array is greater than array_count defined,
|
23
20
|
# then the options_for_select method should be moved into helper.
|
24
|
-
class
|
21
|
+
class MoveCodeIntoHelperReview < Review
|
25
22
|
def url
|
26
23
|
"http://rails-bestpractices.com/posts/26-move-code-into-helper"
|
27
24
|
end
|
28
25
|
|
29
|
-
def
|
26
|
+
def interesting_nodes
|
30
27
|
[:call]
|
31
28
|
end
|
32
29
|
|
33
|
-
def
|
30
|
+
def interesting_files
|
34
31
|
VIEW_FILES
|
35
32
|
end
|
36
33
|
|
@@ -44,7 +41,7 @@ module RailsBestPractices
|
|
44
41
|
# if the first argument of options_for_select method call is an array,
|
45
42
|
# and the size of the array is more than @array_count defined,
|
46
43
|
# then the options_for_select helper should be moved into helper.
|
47
|
-
def
|
44
|
+
def start_call(node)
|
48
45
|
add_error "move code into helper (array_count >= #{@array_count})" if complex_select_options?(node)
|
49
46
|
end
|
50
47
|
|