rails_best_practices 1.14.4 → 1.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/README.md +26 -6
- data/lib/rails_best_practices/core/check.rb +15 -2
- data/lib/rails_best_practices/core/klasses.rb +13 -3
- data/lib/rails_best_practices/core/model_associations.rb +1 -1
- data/lib/rails_best_practices/core/runner.rb +2 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +1 -1
- data/lib/rails_best_practices/prepares/route_prepare.rb +17 -1
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +2 -2
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +2 -2
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +1 -2
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +1 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +15 -26
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -2
- data/lib/rails_best_practices/version.rb +1 -1
- data/spec/rails_best_practices/analyzer_spec.rb +11 -11
- data/spec/rails_best_practices/core/check_spec.rb +1 -1
- data/spec/rails_best_practices/core/error_spec.rb +6 -6
- data/spec/rails_best_practices/core/klasses_spec.rb +26 -2
- data/spec/rails_best_practices/core/methods_spec.rb +18 -18
- data/spec/rails_best_practices/core/model_associations_spec.rb +6 -6
- data/spec/rails_best_practices/core/model_attributes_spec.rb +6 -6
- data/spec/rails_best_practices/core/modules_spec.rb +3 -3
- data/spec/rails_best_practices/core/routes_spec.rb +4 -4
- data/spec/rails_best_practices/core/runner_spec.rb +1 -1
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +18 -6
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +18 -5
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +17 -5
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +13 -13
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +44 -44
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +102 -78
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +25 -9
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +43 -23
- data/spec/rails_best_practices/reviews/check_save_return_value_spec.rb +34 -20
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +16 -5
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +25 -3
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +18 -7
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +30 -7
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +24 -8
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +24 -12
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +17 -5
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +14 -3
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +24 -13
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +25 -6
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +26 -4
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +31 -18
- data/spec/rails_best_practices/reviews/not_rescue_exception_spec.rb +23 -10
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +34 -7
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +19 -8
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +33 -19
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +25 -13
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +14 -4
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +91 -23
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +20 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +66 -41
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +28 -4
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +16 -7
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +45 -32
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +20 -9
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +23 -14
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +34 -7
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +21 -6
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +124 -79
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +12 -12
- data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +16 -4
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +39 -27
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +22 -8
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +39 -21
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +66 -7
- metadata +36 -37
- data/.ruby-gemset +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aad6be6297629a1706250552dd2dc67a88d088f4
|
4
|
+
data.tar.gz: 7416b40f8860088f0ef6734f44fb7dc68531a226
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3ccfe6ee0f2164b49c2d00db11505d858b0750c9167d39e7e9e3da251d3d7e4a122e7601dc158efc462bcc5a422c94c65e6e02ccce95fa68ac2a787fd30eaad
|
7
|
+
data.tar.gz: 1be5179a45ca10b0b5ebb3436bf56af16fcca01a35ff93d8846365eb8390bda417750945ea2c21eec659b68cbde1cfd17e67dbdbecbddb1ad2f2d4c5a337d93d
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.1.0
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,10 +3,9 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/rails_best_practices.png)](http://badge.fury.io/rb/rails_best_practices)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/railsbp/rails_best_practices.png)](http://travis-ci.org/railsbp/rails_best_practices)
|
5
5
|
[![Coverage Status](https://coveralls.io/repos/railsbp/rails_best_practices/badge.png?branch=master)](https://coveralls.io/r/railsbp/rails_best_practices)
|
6
|
-
[![Code Climate](https://codeclimate.com/repos/51eb7073f3ea00179b00db0b/badges/095127e7bb641dbc825b/gpa.png)](https://codeclimate.com/repos/51eb7073f3ea00179b00db0b/feed)
|
7
6
|
|
8
7
|
[![Coderwall Endorse](http://api.coderwall.com/flyerhzm/endorsecount.png)](http://coderwall.com/flyerhzm)
|
9
|
-
[![Click here to lend your support to: rails best practices and make a donation at www.pledgie.com !](
|
8
|
+
[![Click here to lend your support to: rails best practices and make a donation at www.pledgie.com !](https://pledgie.com/campaigns/12057.png?skin_name=chrome)](https://pledgie.com/campaigns/12057)
|
10
9
|
|
11
10
|
rails_best_practices is a code metric tool to check the quality of rails codes.
|
12
11
|
|
@@ -23,12 +22,13 @@ following template engines:
|
|
23
22
|
* slim
|
24
23
|
* rabl
|
25
24
|
|
26
|
-
rails_best_practices works well only in ruby 1.9.2 and
|
25
|
+
rails_best_practices works well only in ruby 1.9.3, 2.0.0, and 2.1.0 so far. It is incompatible with ruby 1.8.
|
27
26
|
|
28
27
|
## External Introduction
|
29
28
|
|
30
|
-
|
31
|
-
|
29
|
+
[Ruby5 - Episode #253](http://ruby5.envylabs.com/episodes/257-episode-253-march-9th-2012/stories/2253-rails_best_practices)
|
30
|
+
|
31
|
+
[Railscasts - #252 Metrics Metrics Metrics](http://railscasts.com/episodes/252-metrics-metrics-metrics)
|
32
32
|
|
33
33
|
## Usage
|
34
34
|
|
@@ -40,9 +40,24 @@ or html output
|
|
40
40
|
|
41
41
|
rails_best_practices -f html .
|
42
42
|
|
43
|
-
By default rails_best_practices will do parse codes in vendor, spec, test and features directories.
|
43
|
+
By default rails_best_practices will do parse codes in vendor, spec, test and features directories.
|
44
|
+
|
45
|
+
### Excluding folders
|
46
|
+
|
47
|
+
To exclude folder simply call it with -e or --exclude
|
48
|
+
|
49
|
+
rails_best_practices -e "db/migrate" .
|
50
|
+
|
51
|
+
To exclude multiple folders, separate them with comma
|
52
|
+
|
53
|
+
rails_best_practices -e "db/migrate,vendor" .
|
54
|
+
|
55
|
+
### Other command line options
|
56
|
+
|
57
|
+
To see full list of commandline options call:
|
44
58
|
|
45
59
|
$ rails_best_practices -h
|
60
|
+
|
46
61
|
Usage: rails_best_practices [options]
|
47
62
|
-d, --debug Debug mode
|
48
63
|
-f, --format FORMAT output format
|
@@ -166,6 +181,11 @@ Now you can customize this configuration file, the default configuration is as f
|
|
166
181
|
|
167
182
|
You can remove or comment one review to disable it, and you can change the options.
|
168
183
|
|
184
|
+
You can apply the `ignored_files` option on any rule by giving a regexp or array of regexps describing the path of the files you don't want to be checked:
|
185
|
+
|
186
|
+
DefaultScopeIsEvilCheck: { ignored_files: 'user\.rb' }
|
187
|
+
LongLineCheck: { max_line_length: 80, ignored_files: ['db/migrate', 'config/initializers'] }
|
188
|
+
|
169
189
|
## Implementation
|
170
190
|
|
171
191
|
Move code from Controller to Model
|
@@ -32,6 +32,10 @@ module RailsBestPractices
|
|
32
32
|
# @param [String] the file name of node.
|
33
33
|
# @return [Boolean] true if the check will need to parse the file.
|
34
34
|
def parse_file?(node_file)
|
35
|
+
is_interesting_file?(node_file) and !is_ignored?(node_file)
|
36
|
+
end
|
37
|
+
|
38
|
+
def is_interesting_file?(node_file)
|
35
39
|
interesting_files.any? do |pattern|
|
36
40
|
if pattern == ALL_FILES
|
37
41
|
node_file =~ pattern && node_file !~ SKIP_FILES
|
@@ -41,6 +45,14 @@ module RailsBestPractices
|
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
48
|
+
def is_ignored?(node_file)
|
49
|
+
regex_ignored_files.map{ |r| !!r.match(node_file) }.inject(:|)
|
50
|
+
end
|
51
|
+
|
52
|
+
def regex_ignored_files
|
53
|
+
@regex_ignored_files ||= Array(@ignored_files).map{ |pattern| Regexp.new(pattern) }
|
54
|
+
end
|
55
|
+
|
44
56
|
# add error if source code violates rails best practice.
|
45
57
|
#
|
46
58
|
# @param [String] message, is the string message for violation of the rails best practice
|
@@ -56,7 +68,7 @@ module RailsBestPractices
|
|
56
68
|
)
|
57
69
|
end
|
58
70
|
|
59
|
-
# errors that
|
71
|
+
# errors that violate the rails best practices.
|
60
72
|
def errors
|
61
73
|
@errors ||= []
|
62
74
|
end
|
@@ -116,7 +128,7 @@ module RailsBestPractices
|
|
116
128
|
classable_modules.pop
|
117
129
|
end
|
118
130
|
|
119
|
-
# remember the class
|
131
|
+
# remember the class name
|
120
132
|
add_callback :start_class do |node|
|
121
133
|
@klass = Core::Klass.new(node.class_name.to_s, node.base_class.to_s, classable_modules)
|
122
134
|
klasses << @klass
|
@@ -334,6 +346,7 @@ module RailsBestPractices
|
|
334
346
|
|
335
347
|
# check if the method is in the except methods list.
|
336
348
|
def excepted?(method)
|
349
|
+
is_ignored?(method.file) ||
|
337
350
|
except_methods.any? do |except_method|
|
338
351
|
class_name, method_name = except_method.split('#')
|
339
352
|
(class_name == '*' && method_name == method.method_name) ||
|
@@ -14,21 +14,31 @@ module RailsBestPractices
|
|
14
14
|
|
15
15
|
# Class info includes class name, extend class name and module names.
|
16
16
|
class Klass
|
17
|
-
attr_reader :class_name, :extend_class_name
|
18
|
-
|
19
17
|
def initialize(class_name, extend_class_name, modules)
|
20
18
|
@class_name = class_name
|
21
19
|
@extend_class_name = extend_class_name
|
22
20
|
@modules = modules.dup
|
23
21
|
end
|
24
22
|
|
25
|
-
def
|
23
|
+
def class_name
|
26
24
|
if @modules.empty?
|
27
25
|
@class_name
|
28
26
|
else
|
29
27
|
@modules.map { |modu| "#{modu}::" }.join("") + @class_name
|
30
28
|
end
|
31
29
|
end
|
30
|
+
|
31
|
+
def extend_class_name
|
32
|
+
if @modules.empty?
|
33
|
+
@extend_class_name
|
34
|
+
else
|
35
|
+
@modules.map { |modu| "#{modu}::" }.join("") + @extend_class_name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
class_name
|
41
|
+
end
|
32
42
|
end
|
33
43
|
end
|
34
44
|
end
|
@@ -34,7 +34,7 @@ module RailsBestPractices
|
|
34
34
|
# @return [Boolean] true if it is the model's association
|
35
35
|
def is_association?(model_name, association_name)
|
36
36
|
associations = @associations[model_name]
|
37
|
-
associations && associations[association_name]
|
37
|
+
!!(associations && associations[association_name])
|
38
38
|
end
|
39
39
|
|
40
40
|
# delegate each to @associations.
|
@@ -138,6 +138,7 @@ module RailsBestPractices
|
|
138
138
|
begin
|
139
139
|
check_name, options = *check
|
140
140
|
klass = RailsBestPractices::Lexicals.const_get(check_name)
|
141
|
+
options = Hash(options)
|
141
142
|
active_checks << (options.empty? ? klass.new : klass.new(options))
|
142
143
|
rescue
|
143
144
|
# the check does not exist in the Lexicals namepace.
|
@@ -157,6 +158,7 @@ module RailsBestPractices
|
|
157
158
|
begin
|
158
159
|
check_name, options = *check
|
159
160
|
klass = RailsBestPractices::Reviews.const_get(check_name.gsub(/Check$/, 'Review'))
|
161
|
+
options = Hash(options)
|
160
162
|
active_checks << (options.empty? ? klass.new : klass.new(options))
|
161
163
|
rescue
|
162
164
|
# the check does not exist in the Reviews namepace.
|
@@ -30,6 +30,9 @@ module RailsBestPractices
|
|
30
30
|
action_names = [first_argument.hash_values.first.to_s]
|
31
31
|
elsif :array == first_argument.sexp_type
|
32
32
|
action_names = first_argument.array_values.map(&:to_s)
|
33
|
+
elsif :bare_assoc_hash == second_argument.try(:sexp_type) && second_argument.hash_value("to").present?
|
34
|
+
controller_name, action_name = second_argument.hash_value("to").to_s.split('#')
|
35
|
+
action_names = [action_name]
|
33
36
|
else
|
34
37
|
action_names = [first_argument.to_s]
|
35
38
|
end
|
@@ -61,7 +64,7 @@ module RailsBestPractices
|
|
61
64
|
@routes.add_route(current_namespaces, controller_name.underscore, action_name)
|
62
65
|
end
|
63
66
|
end
|
64
|
-
when "match"
|
67
|
+
when "match"
|
65
68
|
options = node.arguments.all.last
|
66
69
|
case options.sexp_type
|
67
70
|
when :bare_assoc_hash
|
@@ -84,6 +87,19 @@ module RailsBestPractices
|
|
84
87
|
else
|
85
88
|
# do nothing
|
86
89
|
end
|
90
|
+
when "root"
|
91
|
+
options = node.arguments.all.last
|
92
|
+
case options.sexp_type
|
93
|
+
when :bare_assoc_hash
|
94
|
+
route_node = options.hash_values.find { |value_node| :string_literal == value_node.sexp_type && value_node.to_s.include?('#') }
|
95
|
+
if route_node.present?
|
96
|
+
controller_name, action_name = route_node.to_s.split('#')
|
97
|
+
@routes.add_route(current_namespaces, controller_name.underscore, action_name)
|
98
|
+
end
|
99
|
+
when :string_literal
|
100
|
+
controller_name, action_name = options.to_s.split('#')
|
101
|
+
@routes.add_route(current_namespaces, controller_name.underscore, action_name)
|
102
|
+
end
|
87
103
|
else
|
88
104
|
# nothing to do
|
89
105
|
end
|
@@ -26,8 +26,8 @@ module RailsBestPractices
|
|
26
26
|
interesting_files SCHEMA_FILE
|
27
27
|
url "http://rails-bestpractices.com/posts/21-always-add-db-index"
|
28
28
|
|
29
|
-
def initialize
|
30
|
-
super
|
29
|
+
def initialize(options={})
|
30
|
+
super(options)
|
31
31
|
@index_columns = {}
|
32
32
|
@foreign_keys = {}
|
33
33
|
@table_nodes = {}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module RailsBestPractices
|
3
3
|
module Reviews
|
4
|
-
# Review model files to
|
4
|
+
# Review model files to make sure finders are on their own model.
|
5
5
|
#
|
6
6
|
# See the best practice details here http://rails-bestpractices.com/posts/13-keep-finders-on-their-own-model.
|
7
7
|
#
|
@@ -94,8 +94,7 @@ module RailsBestPractices
|
|
94
94
|
%w(
|
95
95
|
initialize
|
96
96
|
validate validate_each validate_on_create validate_on_update
|
97
|
-
human_attribute_name
|
98
|
-
assign_attributes
|
97
|
+
human_attribute_name assign_attributes attributes attribute
|
99
98
|
to_xml to_json as_json to_param
|
100
99
|
before_save before_create before_update before_destroy after_save after_create
|
101
100
|
after_update after_destroy after_find after_initialize
|
@@ -20,7 +20,8 @@ module RailsBestPractices
|
|
20
20
|
RESOURCE_METHODS = ["show", "new", "create", "edit", "update", "destroy"]
|
21
21
|
RESOURCES_METHODS = RESOURCE_METHODS + ["index"]
|
22
22
|
|
23
|
-
def initialize
|
23
|
+
def initialize(options={})
|
24
|
+
super(options)
|
24
25
|
@namespaces = []
|
25
26
|
@resource_controllers = []
|
26
27
|
end
|
data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb
CHANGED
@@ -24,7 +24,7 @@ module RailsBestPractices
|
|
24
24
|
# check def node and find if the corresponding views exist or not?
|
25
25
|
add_callback :start_def do |node|
|
26
26
|
name = node.method_name.to_s
|
27
|
-
if
|
27
|
+
if !rails_canonical_mailer_views?(name)
|
28
28
|
add_error("use multipart/alternative as content_type of email")
|
29
29
|
end
|
30
30
|
end
|
@@ -34,45 +34,34 @@ module RailsBestPractices
|
|
34
34
|
#
|
35
35
|
# @param [String] name method name in action_mailer
|
36
36
|
def rails_canonical_mailer_views?(name)
|
37
|
-
|
37
|
+
if Prepares.gems.gem_version("rails").to_i > 2
|
38
|
+
rails3_canonical_mailer_views?(name)
|
39
|
+
else
|
40
|
+
rails2_canonical_mailer_views?(name)
|
41
|
+
end
|
38
42
|
end
|
39
43
|
|
40
44
|
# check if rails2's syntax mailer views are canonical.
|
41
45
|
#
|
42
46
|
# @param [String] name method name in action_mailer
|
43
47
|
def rails2_canonical_mailer_views?(name)
|
44
|
-
|
45
|
-
(
|
46
|
-
|
47
|
-
(exist?("#{name}.text.html.rhtml") && !exist?("#{name}.text.plain.rhtml"))
|
48
|
+
return true if mailer_files(name).length == 0
|
49
|
+
mailer_files(name).any? { |filename| filename.index 'text.html' } &&
|
50
|
+
mailer_files(name).any? { |filename| filename.index 'text.plain' }
|
48
51
|
end
|
49
52
|
|
50
53
|
# check if rails3's syntax mailer views are canonical.
|
51
54
|
#
|
52
55
|
# @param [String] name method name in action_mailer
|
53
56
|
def rails3_canonical_mailer_views?(name)
|
54
|
-
|
55
|
-
(
|
56
|
-
|
57
|
+
return true if mailer_files(name).length == 0
|
58
|
+
mailer_files(name).any? { |filename| filename.index 'html' } &&
|
59
|
+
mailer_files(name).any? { |filename| filename.index 'text' }
|
57
60
|
end
|
58
61
|
|
59
|
-
#
|
60
|
-
def
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
# check if erb, haml or slim exists
|
65
|
-
def html_tempalte_exists?(filename)
|
66
|
-
exist?("#{filename}.erb") || exist?("#{filename}.haml") || exist?("#{filename}.slim")
|
67
|
-
end
|
68
|
-
|
69
|
-
# check if the method is a deliver_method.
|
70
|
-
#
|
71
|
-
# @param [String] name the name of the method
|
72
|
-
def deliver_method?(name)
|
73
|
-
Dir.entries(mailer_directory).find { |filename| filename.index name.to_s }
|
74
|
-
rescue
|
75
|
-
false
|
62
|
+
# all mail view files for a method name.
|
63
|
+
def mailer_files(name)
|
64
|
+
Dir.entries(mailer_directory) { |filename| filename.index name.to_s }
|
76
65
|
end
|
77
66
|
|
78
67
|
# the view directory of mailer.
|
@@ -16,8 +16,10 @@ module RailsBestPractices
|
|
16
16
|
|
17
17
|
# check command node to see if load 'deploy/assets'
|
18
18
|
add_callback :start_command do |node|
|
19
|
-
if
|
20
|
-
|
19
|
+
if Prepares.gems.gem_version("rails").to_i == 3
|
20
|
+
if !Prepares.gems.has_gem?("turbo-sprockets-rails3") && "load" == node.message.to_s && "deploy/assets" == node.arguments.to_s
|
21
|
+
add_error "speed up assets precompile with turbo-sprockets-rails3"
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
end
|
@@ -7,21 +7,21 @@ module RailsBestPractices
|
|
7
7
|
|
8
8
|
describe '::new' do
|
9
9
|
it 'should expand a relative path to an absolute' do
|
10
|
-
subject.path.
|
10
|
+
expect(subject.path).to eq File.expand_path('.')
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
describe "expand_dirs_to_files" do
|
15
15
|
it "should expand all files in spec directory" do
|
16
16
|
dir = File.dirname(__FILE__)
|
17
|
-
subject.expand_dirs_to_files(dir).
|
17
|
+
expect(subject.expand_dirs_to_files(dir)).to be_include(dir + '/analyzer_spec.rb')
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "file_sort" do
|
22
22
|
it "should get models first, mailers, helpers and then others" do
|
23
23
|
files = ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
|
24
|
-
subject.file_sort(files).
|
24
|
+
expect(subject.file_sort(files)).to eq(["app/models/user.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/controllers/users_controller.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"])
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -32,11 +32,11 @@ module RailsBestPractices
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should ignore lib" do
|
35
|
-
subject.file_ignore(@all, 'lib/').
|
35
|
+
expect(subject.file_ignore(@all, 'lib/')).to eq(@filtered)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should ignore regexp patterns" do
|
39
|
-
subject.file_ignore(@all, /lib/).
|
39
|
+
expect(subject.file_ignore(@all, /lib/)).to eq(@filtered)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -55,7 +55,7 @@ module RailsBestPractices
|
|
55
55
|
subject.output_terminal_errors
|
56
56
|
result = $stdout.string
|
57
57
|
$stdout = $origin_stdout
|
58
|
-
result.
|
58
|
+
expect(result).to eq(["app/models/user.rb:10 - law of demeter".red, "app/models/post.rb:100 - use query attribute".red, "\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.".green, "\nFound 2 warnings.".red].join("\n") + "\n")
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -67,7 +67,7 @@ module RailsBestPractices
|
|
67
67
|
Dir.mkdir(File.join(random_dir, 'vendor', 'my_project'))
|
68
68
|
File.open(File.join(random_dir, 'vendor', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
|
69
69
|
analyzer = Analyzer.new(File.join(random_dir, 'vendor', 'my_project'))
|
70
|
-
analyzer.parse_files.
|
70
|
+
expect(analyzer.parse_files).to be_include File.join(random_dir, 'vendor', 'my_project', 'my_file.rb')
|
71
71
|
}
|
72
72
|
end
|
73
73
|
|
@@ -77,7 +77,7 @@ module RailsBestPractices
|
|
77
77
|
Dir.mkdir(File.join(random_dir, 'spec', 'my_project'))
|
78
78
|
File.open(File.join(random_dir, 'spec', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
|
79
79
|
analyzer = Analyzer.new(File.join(random_dir, 'spec', 'my_project'))
|
80
|
-
analyzer.parse_files.
|
80
|
+
expect(analyzer.parse_files).to be_include File.join(random_dir, 'spec', 'my_project', 'my_file.rb')
|
81
81
|
}
|
82
82
|
end
|
83
83
|
|
@@ -87,7 +87,7 @@ module RailsBestPractices
|
|
87
87
|
Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
|
88
88
|
File.open(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
|
89
89
|
analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
|
90
|
-
analyzer.parse_files.
|
90
|
+
expect(analyzer.parse_files).to be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
|
91
91
|
}
|
92
92
|
end
|
93
93
|
|
@@ -97,7 +97,7 @@ module RailsBestPractices
|
|
97
97
|
Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
|
98
98
|
File.open(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
|
99
99
|
analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
|
100
|
-
analyzer.parse_files.
|
100
|
+
expect(analyzer.parse_files).to be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
|
101
101
|
}
|
102
102
|
end
|
103
103
|
|
@@ -107,7 +107,7 @@ module RailsBestPractices
|
|
107
107
|
Dir.mkdir(File.join(random_dir, 'tmp', 'my_project'))
|
108
108
|
File.open(File.join(random_dir, 'tmp', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
|
109
109
|
analyzer = Analyzer.new(File.join(random_dir, 'tmp', 'my_project'))
|
110
|
-
analyzer.parse_files.
|
110
|
+
expect(analyzer.parse_files).to be_include File.join(random_dir, 'tmp', 'my_project', 'my_file.rb')
|
111
111
|
}
|
112
112
|
end
|
113
113
|
|
@@ -3,28 +3,28 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices::Core
|
4
4
|
describe Error do
|
5
5
|
it "should return error with filename, line number and message" do
|
6
|
-
Error.new(
|
6
|
+
expect(Error.new(
|
7
7
|
filename: "app/models/user.rb",
|
8
8
|
line_number: "100",
|
9
9
|
message: "not good",
|
10
|
-
type: "BogusReview").to_s.
|
10
|
+
type: "BogusReview").to_s).to eq("app/models/user.rb:100 - not good")
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return short filename" do
|
14
14
|
Runner.base_path = "../rails-bestpractices.com"
|
15
|
-
Error.new(
|
15
|
+
expect(Error.new(
|
16
16
|
filename: "../rails-bestpractices.com/app/models/user.rb",
|
17
17
|
line_number: "100",
|
18
18
|
message: "not good",
|
19
|
-
type: "BogusReview").short_filename.
|
19
|
+
type: "BogusReview").short_filename).to eq("app/models/user.rb")
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should return first line number" do
|
23
|
-
Error.new(
|
23
|
+
expect(Error.new(
|
24
24
|
filename: "app/models/user.rb",
|
25
25
|
line_number: "50,70,100",
|
26
26
|
message: "not good",
|
27
|
-
type: "BogusReview").first_line_number.
|
27
|
+
type: "BogusReview").first_line_number).to eq("50")
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -5,9 +5,33 @@ module RailsBestPractices::Core
|
|
5
5
|
it { should be_a_kind_of Array }
|
6
6
|
|
7
7
|
context "Klass" do
|
8
|
-
|
8
|
+
context "#class_name" do
|
9
|
+
it "gets class name without module" do
|
10
|
+
klass = Klass.new("BlogPost", "Post", [])
|
11
|
+
expect(klass.class_name).to eq("BlogPost")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "gets class name with moduel" do
|
15
|
+
klass = Klass.new("BlogPost", "Post", ["Admin"])
|
16
|
+
expect(klass.class_name).to eq("Admin::BlogPost")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "#extend_class_name" do
|
21
|
+
it "gets extend class name without module" do
|
22
|
+
klass = Klass.new("BlogPost", "Post", [])
|
23
|
+
expect(klass.extend_class_name).to eq("Post")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "gets extend class name with module" do
|
27
|
+
klass = Klass.new("BlogPost", "Post", ["Admin"])
|
28
|
+
expect(klass.extend_class_name).to eq("Admin::Post")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "gets to_s equal to class_name" do
|
9
33
|
klass = Klass.new("BlogPost", "Post", ["Admin"])
|
10
|
-
klass.to_s.
|
34
|
+
expect(klass.to_s).to eq(klass.class_name)
|
11
35
|
end
|
12
36
|
end
|
13
37
|
end
|