rails_best_practices 1.19.0 → 1.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +2 -2
- data/Rakefile +8 -8
- data/bin/rails_best_practices +1 -1
- data/lib/rails_best_practices/analyzer.rb +45 -45
- data/lib/rails_best_practices/command.rb +40 -40
- data/lib/rails_best_practices/core/check.rb +15 -15
- data/lib/rails_best_practices/core/klasses.rb +1 -1
- data/lib/rails_best_practices/core/methods.rb +7 -7
- data/lib/rails_best_practices/core/model_associations.rb +3 -3
- data/lib/rails_best_practices/core/modules.rb +1 -1
- data/lib/rails_best_practices/core/routes.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +3 -3
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -4
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -2
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -2
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -8
- data/lib/rails_best_practices/prepares/helper_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +4 -4
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +19 -19
- data/lib/rails_best_practices/prepares/route_prepare.rb +34 -34
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +7 -7
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +1 -1
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +3 -3
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -3
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +1 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +6 -6
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -3
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -2
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +2 -2
- 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 +4 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -2
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +3 -3
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +9 -9
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -11
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +8 -7
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
- data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -2
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +22 -22
- data/lib/rails_best_practices/reviews/review.rb +2 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +4 -4
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -4
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +24 -24
- data/spec/rails_best_practices/analyzer_spec.rb +43 -43
- data/spec/rails_best_practices/core/check_spec.rb +2 -2
- data/spec/rails_best_practices/core/checks_loader_spec.rb +4 -4
- data/spec/rails_best_practices/core/error_spec.rb +16 -16
- data/spec/rails_best_practices/core/except_methods_spec.rb +15 -15
- data/spec/rails_best_practices/core/gems_spec.rb +9 -9
- data/spec/rails_best_practices/core/klasses_spec.rb +17 -17
- data/spec/rails_best_practices/core/methods_spec.rb +28 -28
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -10
- data/spec/rails_best_practices/core/model_attributes_spec.rb +10 -10
- data/spec/rails_best_practices/core/modules_spec.rb +10 -10
- data/spec/rails_best_practices/core/routes_spec.rb +10 -10
- data/spec/rails_best_practices/core/runner_spec.rb +4 -4
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +26 -26
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +5 -5
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +108 -108
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +77 -77
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +27 -27
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +17 -17
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +16 -16
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +16 -16
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -33
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +106 -106
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +23 -23
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0107d87cb5888b9ef62445563d9af1328cbc9761
|
4
|
+
data.tar.gz: 26ff059e4a7437ee9606afbd2c200d27615283fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 921598700949438fd5975a90e2091112a2c5296ddf14a11560f7b391a20c7cc6936c875e399327924990128944bdae455bd3311ffcf0473d0bcc4b8f40388719
|
7
|
+
data.tar.gz: 50585713ae56c85ee34c38c19c9b4cb13e0fe8e55a2f328b5f3609226ce77680dfc783edccde4614cd48658497161be5bf45c6ac76c863eb28ff2be93be92dc1
|
data/CHANGELOG.md
CHANGED
data/Guardfile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# A sample Guardfile
|
2
2
|
# More info at https://github.com/guard/guard#readme
|
3
3
|
|
4
|
-
guard 'rspec', :version => 2, :all_after_pass => false, :all_on_start => false, :cli =>
|
4
|
+
guard 'rspec', :version => 2, :all_after_pass => false, :all_on_start => false, :cli => '--color --format nested --fail-fast' do
|
5
5
|
watch(%r{^spec/.+_spec\.rb$})
|
6
6
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
|
-
watch('spec/spec_helper.rb') {
|
7
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
8
8
|
end
|
data/Rakefile
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler'
|
2
|
+
require 'bundler/gem_tasks'
|
3
3
|
|
4
4
|
Bundler.setup
|
5
5
|
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
6
|
+
require 'rake'
|
7
|
+
require 'rspec'
|
8
|
+
require 'rspec/core/rake_task'
|
9
9
|
|
10
|
-
$LOAD_PATH.unshift File.expand_path(
|
11
|
-
require
|
10
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
11
|
+
require 'rails_best_practices/version'
|
12
12
|
|
13
13
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
14
|
-
spec.pattern =
|
14
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
15
15
|
end
|
16
16
|
|
17
17
|
task :default => :spec
|
data/bin/rails_best_practices
CHANGED
@@ -17,7 +17,7 @@ module RailsBestPractices
|
|
17
17
|
attr_accessor :runner
|
18
18
|
attr_reader :path
|
19
19
|
|
20
|
-
DEFAULT_CONFIG = File.join(File.dirname(__FILE__),
|
20
|
+
DEFAULT_CONFIG = File.join(File.dirname(__FILE__), '..', '..', 'rails_best_practices.yml')
|
21
21
|
GITHUB_URL = 'https://github.com/'
|
22
22
|
|
23
23
|
# initialize
|
@@ -25,11 +25,11 @@ module RailsBestPractices
|
|
25
25
|
# @param [String] path where to generate the configuration yaml file
|
26
26
|
# @param [Hash] options
|
27
27
|
def initialize(path, options = {})
|
28
|
-
@path = File.expand_path(path ||
|
28
|
+
@path = File.expand_path(path || '.')
|
29
29
|
|
30
30
|
@options = options
|
31
|
-
@options[
|
32
|
-
@options[
|
31
|
+
@options['exclude'] ||= []
|
32
|
+
@options['only'] ||= []
|
33
33
|
end
|
34
34
|
|
35
35
|
# generate configuration yaml file.
|
@@ -59,18 +59,18 @@ module RailsBestPractices
|
|
59
59
|
|
60
60
|
# Output the analyze result.
|
61
61
|
def output
|
62
|
-
case @options[
|
63
|
-
when
|
64
|
-
@options[
|
62
|
+
case @options['format']
|
63
|
+
when 'html'
|
64
|
+
@options['output-file'] ||= 'rails_best_practices_output.html'
|
65
65
|
output_html_errors
|
66
|
-
when
|
67
|
-
@options[
|
66
|
+
when 'json'
|
67
|
+
@options['output-file'] ||= 'rails_best_practices_output.json'
|
68
68
|
output_json_errors
|
69
|
-
when
|
70
|
-
@options[
|
69
|
+
when 'yaml'
|
70
|
+
@options['output-file'] ||= 'rails_best_practices_output.yaml'
|
71
71
|
output_yaml_errors
|
72
|
-
when
|
73
|
-
@options[
|
72
|
+
when 'xml'
|
73
|
+
@options['output-file'] ||= 'rails_best_practices_output.xml'
|
74
74
|
output_xml_errors
|
75
75
|
else
|
76
76
|
output_terminal_errors
|
@@ -86,10 +86,10 @@ module RailsBestPractices
|
|
86
86
|
def process(process)
|
87
87
|
parse_files.each do |file|
|
88
88
|
begin
|
89
|
-
puts file if @options[
|
89
|
+
puts file if @options['debug']
|
90
90
|
@runner.send(process, file, File.read(file))
|
91
91
|
rescue
|
92
|
-
if @options[
|
92
|
+
if @options['debug']
|
93
93
|
warning = "#{file} looks like it's not a valid Ruby file. Skipping..."
|
94
94
|
plain_output(warning, 'red')
|
95
95
|
end
|
@@ -107,17 +107,17 @@ module RailsBestPractices
|
|
107
107
|
files = expand_dirs_to_files(@path)
|
108
108
|
files = file_sort(files)
|
109
109
|
|
110
|
-
if @options[
|
111
|
-
files = file_accept(files, @options[
|
110
|
+
if @options['only'].present?
|
111
|
+
files = file_accept(files, @options['only'])
|
112
112
|
end
|
113
113
|
|
114
114
|
# By default, tmp, vender, spec, test, features are ignored.
|
115
|
-
[
|
115
|
+
['vendor', 'spec', 'test', 'features', 'tmp'].each do |dir|
|
116
116
|
files = file_ignore(files, File.join(@path, dir)) unless @options[dir]
|
117
117
|
end
|
118
118
|
|
119
119
|
# Exclude files based on exclude regexes if the option is set.
|
120
|
-
@options[
|
120
|
+
@options['exclude'].each do |pattern|
|
121
121
|
files = file_ignore(files, pattern)
|
122
122
|
end
|
123
123
|
|
@@ -192,8 +192,8 @@ module RailsBestPractices
|
|
192
192
|
def load_hg_info
|
193
193
|
hg_progressbar = ProgressBar.create(:title => 'Hg Info', :total => errors.size) if display_bar?
|
194
194
|
errors.each do |error|
|
195
|
-
hg_info = `cd #{@runner.class.base_path} && hg blame -lvcu #{error.filename[@runner.class.base_path.size..-1].gsub(/^\//,
|
196
|
-
unless hg_info ==
|
195
|
+
hg_info = `cd #{@runner.class.base_path} && hg blame -lvcu #{error.filename[@runner.class.base_path.size..-1].gsub(/^\//, '')} | sed -n /:#{error.line_number.split(',').first}:/p`
|
196
|
+
unless hg_info == ''
|
197
197
|
hg_commit_username = hg_info.split(':')[0].strip
|
198
198
|
error.hg_username = hg_commit_username.split(/\ /)[0..-2].join(' ')
|
199
199
|
error.hg_commit = hg_commit_username.split(/\ /)[-1]
|
@@ -209,9 +209,9 @@ module RailsBestPractices
|
|
209
209
|
start = @runner.class.base_path =~ /\/$/ ? @runner.class.base_path.size : @runner.class.base_path.size + 1
|
210
210
|
errors.each do |error|
|
211
211
|
git_info = `cd #{@runner.class.base_path} && git blame -L #{error.line_number.split(',').first},+1 #{error.filename[start..-1]}`
|
212
|
-
unless git_info ==
|
213
|
-
git_commit, git_username = git_info.split(/\d{4}-\d{2}-\d{2}/).first.split(
|
214
|
-
error.git_commit = git_commit.split(
|
212
|
+
unless git_info == ''
|
213
|
+
git_commit, git_username = git_info.split(/\d{4}-\d{2}-\d{2}/).first.split('(')
|
214
|
+
error.git_commit = git_commit.split(' ').first.strip
|
215
215
|
error.git_username = git_username.strip
|
216
216
|
end
|
217
217
|
git_progressbar.increment if display_bar?
|
@@ -222,27 +222,27 @@ module RailsBestPractices
|
|
222
222
|
# output errors with html format.
|
223
223
|
def output_html_errors
|
224
224
|
require 'erubis'
|
225
|
-
template = @options[
|
225
|
+
template = @options['template'] ? File.read(File.expand_path(@options['template'])) : File.read(File.join(File.dirname(__FILE__), '..', '..', 'assets', 'result.html.erb'))
|
226
226
|
|
227
|
-
if @options[
|
228
|
-
last_commit_id = @options[
|
229
|
-
unless @options[
|
230
|
-
@options[
|
227
|
+
if @options['with-github']
|
228
|
+
last_commit_id = @options['last-commit-id'] ? @options['last-commit-id'] : `cd #{@runner.class.base_path} && git rev-parse HEAD`.chomp
|
229
|
+
unless @options['github-name'].start_with?('https')
|
230
|
+
@options['github-name'] = GITHUB_URL + @options['github-name']
|
231
231
|
end
|
232
232
|
end
|
233
|
-
File.open(@options[
|
233
|
+
File.open(@options['output-file'], 'w+') do |file|
|
234
234
|
eruby = Erubis::Eruby.new(template)
|
235
235
|
file.puts eruby.evaluate(
|
236
236
|
errors: errors,
|
237
237
|
error_types: error_types,
|
238
|
-
textmate: @options[
|
239
|
-
sublime: @options[
|
240
|
-
mvim: @options[
|
241
|
-
github: @options[
|
242
|
-
github_name: @options[
|
238
|
+
textmate: @options['with-textmate'],
|
239
|
+
sublime: @options['with-sublime'],
|
240
|
+
mvim: @options['with-mvim'],
|
241
|
+
github: @options['with-github'],
|
242
|
+
github_name: @options['github-name'],
|
243
243
|
last_commit_id: last_commit_id,
|
244
|
-
git: @options[
|
245
|
-
hg: @options[
|
244
|
+
git: @options['with-git'],
|
245
|
+
hg: @options['with-hg']
|
246
246
|
)
|
247
247
|
end
|
248
248
|
end
|
@@ -272,14 +272,14 @@ module RailsBestPractices
|
|
272
272
|
end
|
273
273
|
|
274
274
|
formatter = REXML::Formatters::Default.new
|
275
|
-
File.open(@options[
|
275
|
+
File.open(@options['output-file'], 'w+') do |result|
|
276
276
|
formatter.write(document, result)
|
277
277
|
end
|
278
278
|
end
|
279
279
|
|
280
280
|
# output errors with yaml format.
|
281
281
|
def output_yaml_errors
|
282
|
-
File.open(@options[
|
282
|
+
File.open(@options['output-file'], 'w+') do |file|
|
283
283
|
file.write YAML.dump(errors)
|
284
284
|
end
|
285
285
|
end
|
@@ -294,7 +294,7 @@ module RailsBestPractices
|
|
294
294
|
}
|
295
295
|
end
|
296
296
|
|
297
|
-
File.open(@options[
|
297
|
+
File.open(@options['output-file'], 'w+') do |file|
|
298
298
|
file.write JSON.dump(errors_as_hashes)
|
299
299
|
end
|
300
300
|
end
|
@@ -304,7 +304,7 @@ module RailsBestPractices
|
|
304
304
|
# @param [String] message to output
|
305
305
|
# @param [String] color
|
306
306
|
def plain_output(message, color)
|
307
|
-
if @options[
|
307
|
+
if @options['without-color']
|
308
308
|
puts message
|
309
309
|
else
|
310
310
|
puts Colorize.send(color, message)
|
@@ -314,19 +314,19 @@ module RailsBestPractices
|
|
314
314
|
# analyze source codes.
|
315
315
|
def analyze_source_codes
|
316
316
|
@bar = ProgressBar.create(:title => 'Source Code', :total => parse_files.size * 3) if display_bar?
|
317
|
-
[
|
317
|
+
['lexical', 'prepare', 'review'].each { |process| send(:process, process) }
|
318
318
|
@bar.finish if display_bar?
|
319
319
|
end
|
320
320
|
|
321
321
|
# analyze version control system info.
|
322
322
|
def analyze_vcs
|
323
|
-
load_git_info if @options[
|
324
|
-
load_hg_info if @options[
|
323
|
+
load_git_info if @options['with-git']
|
324
|
+
load_hg_info if @options['with-hg']
|
325
325
|
end
|
326
326
|
|
327
327
|
# if disaply progress bar.
|
328
328
|
def display_bar?
|
329
|
-
!@options[
|
329
|
+
!@options['debug'] && !@options['silent']
|
330
330
|
end
|
331
331
|
|
332
332
|
# unique error types.
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
2
|
+
require 'optparse'
|
3
3
|
|
4
4
|
# Usage: rails_best_practices [options] path
|
5
5
|
# -d, --debug debug mode
|
@@ -29,96 +29,96 @@ require "optparse"
|
|
29
29
|
options = {}
|
30
30
|
|
31
31
|
OptionParser.new do |opts|
|
32
|
-
opts.banner =
|
32
|
+
opts.banner = 'Usage: rails_best_practices [options] path'
|
33
33
|
|
34
|
-
opts.on(
|
35
|
-
options[
|
34
|
+
opts.on('-d', '--debug', 'Debug mode') do
|
35
|
+
options['debug'] = true
|
36
36
|
end
|
37
37
|
|
38
|
-
opts.on(
|
39
|
-
options[
|
38
|
+
opts.on('-f', '--format FORMAT', 'output format (text, html, yml, json, xml)') do |format|
|
39
|
+
options['format'] = format
|
40
40
|
end
|
41
41
|
|
42
|
-
opts.on(
|
43
|
-
options[
|
42
|
+
opts.on('--without-color', 'only output plain text without color') do
|
43
|
+
options['without-color'] = true
|
44
44
|
end
|
45
45
|
|
46
|
-
opts.on(
|
47
|
-
options[
|
46
|
+
opts.on('--with-textmate', 'open file by textmate in html format') do
|
47
|
+
options['with-textmate'] = true
|
48
48
|
end
|
49
49
|
|
50
|
-
opts.on(
|
51
|
-
options[
|
50
|
+
opts.on('--with-sublime', 'open file by sublime in html format') do
|
51
|
+
options['with-sublime'] = true
|
52
52
|
end
|
53
53
|
|
54
|
-
opts.on(
|
55
|
-
options[
|
54
|
+
opts.on('--with-mvim', 'open file by mvim in html format') do
|
55
|
+
options['with-mvim'] = true
|
56
56
|
end
|
57
57
|
|
58
|
-
opts.on(
|
59
|
-
options[
|
60
|
-
options[
|
58
|
+
opts.on('--with-github GITHUB_NAME', 'open file on github in html format') do |github_name|
|
59
|
+
options['with-github'] = true
|
60
|
+
options['github-name'] = github_name
|
61
61
|
end
|
62
62
|
|
63
|
-
opts.on(
|
64
|
-
options[
|
63
|
+
opts.on('--last-commit-id COMMIT_ID', 'last commit id') do |commit_id|
|
64
|
+
options['last-commit-id'] = commit_id
|
65
65
|
end
|
66
66
|
|
67
|
-
opts.on(
|
68
|
-
options[
|
67
|
+
opts.on('--with-hg', 'display hg commit and username, only support html format') do
|
68
|
+
options['with-hg'] = true
|
69
69
|
end
|
70
70
|
|
71
|
-
opts.on(
|
72
|
-
options[
|
71
|
+
opts.on('--with-git', 'display git commit and username, only support html format') do
|
72
|
+
options['with-git'] = true
|
73
73
|
end
|
74
74
|
|
75
|
-
opts.on(
|
76
|
-
options[
|
75
|
+
opts.on('--template TEMPLATE', 'customize erb template') do |template|
|
76
|
+
options['template'] = template
|
77
77
|
end
|
78
78
|
|
79
|
-
opts.on(
|
80
|
-
options[
|
79
|
+
opts.on('--output-file OUTPUT_FILE', 'output html file for the analyzing result') do |output_file|
|
80
|
+
options['output-file'] = output_file
|
81
81
|
end
|
82
82
|
|
83
|
-
opts.on(
|
84
|
-
options[
|
83
|
+
opts.on('--silent', 'silent mode') do
|
84
|
+
options['silent'] = true
|
85
85
|
end
|
86
86
|
|
87
|
-
[
|
87
|
+
['vendor', 'spec', 'test', 'features'].each do |pattern|
|
88
88
|
opts.on("--#{pattern}", "include #{pattern} files") do
|
89
89
|
options[pattern] = true
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
opts.on_tail(
|
94
|
-
require
|
93
|
+
opts.on_tail('-v', '--version', 'Show this version') do
|
94
|
+
require 'rails_best_practices/version'
|
95
95
|
puts RailsBestPractices::VERSION
|
96
96
|
exit
|
97
97
|
end
|
98
98
|
|
99
|
-
opts.on_tail(
|
99
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
100
100
|
puts opts
|
101
101
|
exit
|
102
102
|
end
|
103
103
|
|
104
|
-
opts.on(
|
104
|
+
opts.on('-x', '--exclude PATTERNS', "Don't analyze files matching a pattern", '(comma-separated regexp list)') do |list|
|
105
105
|
begin
|
106
|
-
options[
|
106
|
+
options['exclude'] = list.split(',').map{|x| Regexp.new x}
|
107
107
|
rescue RegexpError => e
|
108
108
|
raise OptionParser::InvalidArgument, e.message
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
-
opts.on(
|
112
|
+
opts.on('-o', '--only PATTERNS', 'Analyze files only matching a pattern', '(comma-separated regexp list)') do |list|
|
113
113
|
begin
|
114
|
-
options[
|
114
|
+
options['only'] = list.split(',').map { |x| Regexp.new x }
|
115
115
|
rescue RegexpError => e
|
116
116
|
raise OptionParser::InvalidArgument e.message
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
-
opts.on(
|
121
|
-
options[
|
120
|
+
opts.on('-g', '--generate', 'Generate configuration yaml') do
|
121
|
+
options['generate'] = true
|
122
122
|
end
|
123
123
|
|
124
124
|
opts.on(
|
@@ -136,7 +136,7 @@ if ARGV.size > 0 && !File.exists?(ARGV.first)
|
|
136
136
|
exit 1
|
137
137
|
end
|
138
138
|
|
139
|
-
if options[
|
139
|
+
if options['generate']
|
140
140
|
RailsBestPractices::Analyzer.new(ARGV.first).generate
|
141
141
|
else
|
142
142
|
analyzer = RailsBestPractices::Analyzer.new(ARGV.first, options)
|
@@ -183,7 +183,7 @@ module RailsBestPractices
|
|
183
183
|
|
184
184
|
# get the current module name.
|
185
185
|
def current_module_name
|
186
|
-
moduleable_modules.join(
|
186
|
+
moduleable_modules.join('::')
|
187
187
|
end
|
188
188
|
|
189
189
|
# modules.
|
@@ -229,9 +229,9 @@ module RailsBestPractices
|
|
229
229
|
case node.message.to_s
|
230
230
|
when *skip_command_callback_nodes
|
231
231
|
# nothing
|
232
|
-
when
|
232
|
+
when 'alias_method'
|
233
233
|
mark_used(node.arguments.all[1])
|
234
|
-
when
|
234
|
+
when 'alias_method_chain'
|
235
235
|
method, feature = *node.arguments.all.map(&:to_s)
|
236
236
|
call_method("#{method}_with_#{feature}")
|
237
237
|
when /^(before|after)_/
|
@@ -261,17 +261,17 @@ module RailsBestPractices
|
|
261
261
|
# super options.merge(exclude: :visible, methods: [:is_discussion_conversation])
|
262
262
|
# end
|
263
263
|
add_callback :start_bare_assoc_hash do |node|
|
264
|
-
if node.hash_keys.include?
|
265
|
-
mark_used(node.hash_value(
|
264
|
+
if node.hash_keys.include? 'methods'
|
265
|
+
mark_used(node.hash_value('methods'))
|
266
266
|
end
|
267
267
|
end
|
268
268
|
|
269
269
|
# remember the first argument for try and send method.
|
270
270
|
add_callback :start_method_add_arg do |node|
|
271
271
|
case node.message.to_s
|
272
|
-
when
|
272
|
+
when 'try'
|
273
273
|
mark_used(node.arguments.all.first)
|
274
|
-
when
|
274
|
+
when 'send'
|
275
275
|
if [:symbol_literal, :string_literal].include?(node.arguments.all.first.sexp_type)
|
276
276
|
mark_used(node.arguments.all.first)
|
277
277
|
end
|
@@ -315,21 +315,21 @@ module RailsBestPractices
|
|
315
315
|
|
316
316
|
# check if the controller is inherit from InheritedResources::Base.
|
317
317
|
add_callback :start_class do |node|
|
318
|
-
if
|
318
|
+
if 'InheritedResources::Base' == current_extend_class_name
|
319
319
|
@inherited_resources = true
|
320
320
|
end
|
321
321
|
end
|
322
322
|
|
323
323
|
# check if there is a DSL call inherit_resources.
|
324
324
|
add_callback :start_var_ref do |node|
|
325
|
-
if
|
325
|
+
if 'inherit_resources' == node.to_s
|
326
326
|
@inherited_resources = true
|
327
327
|
end
|
328
328
|
end
|
329
329
|
|
330
330
|
# check if there is a DSL call inherit_resources.
|
331
331
|
add_callback :start_vcall do |node|
|
332
|
-
if
|
332
|
+
if 'inherit_resources' == node.to_s
|
333
333
|
@inherited_resources = true
|
334
334
|
end
|
335
335
|
end
|
@@ -352,7 +352,7 @@ module RailsBestPractices
|
|
352
352
|
end
|
353
353
|
|
354
354
|
def internal_except_methods
|
355
|
-
raise NoMethodError.new
|
355
|
+
raise NoMethodError.new 'no method internal_except_methods'
|
356
356
|
end
|
357
357
|
end
|
358
358
|
end
|
@@ -360,12 +360,12 @@ module RailsBestPractices
|
|
360
360
|
def self.matches method, except_method
|
361
361
|
class_name, method_name = except_method.split('#')
|
362
362
|
|
363
|
-
method_name =
|
363
|
+
method_name = '.*' if method_name == '*'
|
364
364
|
method_expression = Regexp.new method_name
|
365
365
|
matched = method.method_name =~ method_expression
|
366
366
|
|
367
367
|
if matched
|
368
|
-
class_name =
|
368
|
+
class_name = '.*' if class_name == '*'
|
369
369
|
class_expression = Regexp.new class_name
|
370
370
|
|
371
371
|
class_names = Prepares.klasses
|
@@ -403,12 +403,12 @@ module RailsBestPractices
|
|
403
403
|
|
404
404
|
# set access control to "public" by default.
|
405
405
|
add_callback :start_class do |node|
|
406
|
-
@access_control =
|
406
|
+
@access_control = 'public'
|
407
407
|
end
|
408
408
|
|
409
409
|
# set access control to "public" by default.
|
410
410
|
add_callback :start_module do |node|
|
411
|
-
@access_control =
|
411
|
+
@access_control = 'public'
|
412
412
|
end
|
413
413
|
end
|
414
414
|
|