rails_best_practices 1.19.2 → 1.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.travis.yml +2 -3
- data/CHANGELOG.md +7 -7
- data/Gemfile +3 -5
- data/Gemfile.lock +125 -0
- data/Guardfile +2 -0
- data/README.md +6 -6
- data/Rakefile +2 -17
- data/assets/result.html.erb +2 -0
- data/lib/rails_best_practices.rb +3 -2
- data/lib/rails_best_practices/analyzer.rb +61 -49
- data/lib/rails_best_practices/cli.rb +22 -0
- data/lib/rails_best_practices/command.rb +1 -131
- data/lib/rails_best_practices/core/check.rb +64 -56
- data/lib/rails_best_practices/core/checks_loader.rb +24 -23
- data/lib/rails_best_practices/core/configs.rb +1 -2
- data/lib/rails_best_practices/core/controllers.rb +1 -2
- data/lib/rails_best_practices/core/error.rb +1 -1
- data/lib/rails_best_practices/core/helpers.rb +1 -2
- data/lib/rails_best_practices/core/mailers.rb +1 -2
- data/lib/rails_best_practices/core/methods.rb +27 -21
- data/lib/rails_best_practices/core/model_associations.rb +10 -5
- data/lib/rails_best_practices/core/models.rb +1 -2
- 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 +67 -73
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +156 -0
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +24 -17
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +3 -3
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +63 -23
- data/lib/rails_best_practices/prepares/route_prepare.rb +28 -21
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +38 -34
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +94 -89
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +15 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +20 -8
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +1 -1
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +1 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +16 -16
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +12 -12
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +10 -11
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +25 -24
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +9 -10
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +10 -11
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -22
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +1 -1
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +8 -8
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +35 -32
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +26 -19
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +12 -10
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +38 -18
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +11 -11
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +77 -74
- data/lib/rails_best_practices/reviews/review.rb +2 -1
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +12 -12
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +18 -15
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +15 -15
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +24 -22
- data/lib/rails_best_practices/reviews/use_observer_review.rb +28 -28
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +6 -6
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +68 -66
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +16 -14
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +38 -43
- data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
- data/spec/rails_best_practices/analyzer_spec.rb +73 -42
- data/spec/rails_best_practices/core/check_spec.rb +5 -5
- data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
- data/spec/rails_best_practices/core/configs_spec.rb +1 -1
- data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
- data/spec/rails_best_practices/core/error_spec.rb +21 -18
- data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
- data/spec/rails_best_practices/core/gems_spec.rb +4 -4
- data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
- data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
- data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
- data/spec/rails_best_practices/core/methods_spec.rb +6 -6
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
- data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
- data/spec/rails_best_practices/core/models_spec.rb +1 -1
- data/spec/rails_best_practices/core/modules_spec.rb +5 -5
- data/spec/rails_best_practices/core/routes_spec.rb +5 -5
- data/spec/rails_best_practices/core/runner_spec.rb +9 -7
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +32 -31
- 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 +2 -2
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +17 -17
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +141 -76
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
- 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 +5 -5
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
- 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 +29 -22
- 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 +11 -6
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +32 -22
- 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 +9 -7
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
- 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 +24 -17
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +64 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +21 -14
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +20 -14
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
- 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 +35 -31
- 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 +10 -8
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +61 -59
- metadata +16 -18
@@ -4,9 +4,9 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module RailsBestPractices::Core
|
6
6
|
describe Methods do
|
7
|
-
let(:methods) {
|
7
|
+
let(:methods) { described_class.new }
|
8
8
|
|
9
|
-
before
|
9
|
+
before do
|
10
10
|
methods.add_method('Post', 'create')
|
11
11
|
methods.add_method('Post', 'destroy')
|
12
12
|
methods.add_method('Post', 'save_or_update', {}, 'protected')
|
@@ -14,7 +14,7 @@ module RailsBestPractices::Core
|
|
14
14
|
methods.add_method('Comment', 'create')
|
15
15
|
end
|
16
16
|
|
17
|
-
it '
|
17
|
+
it 'get_methodses' do
|
18
18
|
expect(methods.get_methods('Post').map(&:method_name)).to eq(%w[create destroy save_or_update find_by_sql])
|
19
19
|
expect(methods.get_methods('Post', 'public').map(&:method_name)).to eq(%w[create destroy])
|
20
20
|
expect(methods.get_methods('Post', 'protected').map(&:method_name)).to eq(['save_or_update'])
|
@@ -22,7 +22,7 @@ module RailsBestPractices::Core
|
|
22
22
|
expect(methods.get_methods('Comment').map(&:method_name)).to eq(['create'])
|
23
23
|
end
|
24
24
|
|
25
|
-
it '
|
25
|
+
it 'has_method?s' do
|
26
26
|
expect(methods).to be_has_method('Post', 'create', 'public')
|
27
27
|
expect(methods).to be_has_method('Post', 'destroy', 'public')
|
28
28
|
expect(methods).not_to be_has_method('Post', 'save_or_update', 'public')
|
@@ -32,12 +32,12 @@ module RailsBestPractices::Core
|
|
32
32
|
expect(methods).not_to be_has_method('Comment', 'destroy')
|
33
33
|
end
|
34
34
|
|
35
|
-
it '
|
35
|
+
it 'get_methods' do
|
36
36
|
expect(methods.get_method('Post', 'create', 'public')).not_to be_nil
|
37
37
|
expect(methods.get_method('Post', 'create', 'protected')).to be_nil
|
38
38
|
end
|
39
39
|
|
40
|
-
it '
|
40
|
+
it 'get_all_unused_methodses' do
|
41
41
|
methods.get_method('Comment', 'create').mark_used
|
42
42
|
expect(methods.get_all_unused_methods('public').map(&:method_name)).to eq(%w[create destroy])
|
43
43
|
expect(methods.get_all_unused_methods('protected').map(&:method_name)).to eq(['save_or_update'])
|
@@ -4,20 +4,24 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module RailsBestPractices::Core
|
6
6
|
describe ModelAssociations do
|
7
|
-
let(:model_associations) {
|
7
|
+
let(:model_associations) { described_class.new }
|
8
8
|
|
9
|
-
before
|
9
|
+
before do
|
10
10
|
model_associations.add_association('Project', 'project_manager', 'belongs_to')
|
11
11
|
model_associations.add_association('Project', 'people', 'has_many', 'Person')
|
12
12
|
end
|
13
13
|
|
14
|
-
it '
|
15
|
-
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
16
|
-
|
14
|
+
it 'gets model associations' do
|
15
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
16
|
+
'meta' => 'belongs_to', 'class_name' => 'ProjectManager'
|
17
|
+
)
|
18
|
+
expect(model_associations.get_association('Project', 'people')).to eq(
|
19
|
+
'meta' => 'has_many', 'class_name' => 'Person'
|
20
|
+
)
|
17
21
|
expect(model_associations.get_association('Project', 'unknown')).to be_nil
|
18
22
|
end
|
19
23
|
|
20
|
-
it '
|
24
|
+
it 'checks is model associatiosn' do
|
21
25
|
expect(model_associations.is_association?('Project', 'project_manager')).to eq true
|
22
26
|
expect(model_associations.is_association?('Project', 'people')).to eq true
|
23
27
|
expect(model_associations.is_association?('Project', 'unknown')).to eq false
|
@@ -4,20 +4,20 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module RailsBestPractices::Core
|
6
6
|
describe ModelAttributes do
|
7
|
-
let(:model_attributes) {
|
7
|
+
let(:model_attributes) { described_class.new }
|
8
8
|
|
9
|
-
before
|
9
|
+
before do
|
10
10
|
model_attributes.add_attribute('Post', 'title', :string)
|
11
11
|
model_attributes.add_attribute('Post', 'user_id', :integer)
|
12
12
|
end
|
13
13
|
|
14
|
-
it '
|
14
|
+
it 'gets model attributes' do
|
15
15
|
expect(model_attributes.get_attribute_type('Post', 'title')).to eq(:string)
|
16
16
|
expect(model_attributes.get_attribute_type('Post', 'user_id')).to eq(:integer)
|
17
17
|
expect(model_attributes.get_attribute_type('Post', 'unknonw')).to be_nil
|
18
18
|
end
|
19
19
|
|
20
|
-
it '
|
20
|
+
it 'checks is model attributes' do
|
21
21
|
expect(model_attributes.is_attribute?('Post', 'title')).to be true
|
22
22
|
expect(model_attributes.is_attribute?('Post', 'user_id')).to be true
|
23
23
|
expect(model_attributes.is_attribute?('Post', 'unknonw')).to be false
|
@@ -4,25 +4,25 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module RailsBestPractices::Core
|
6
6
|
describe Modules do
|
7
|
-
it {
|
7
|
+
it { is_expected.to be_a_kind_of Array }
|
8
8
|
|
9
9
|
context 'Modules' do
|
10
10
|
before do
|
11
11
|
@mod = Mod.new('PostsHelper', [])
|
12
12
|
end
|
13
|
-
subject {
|
14
|
-
it '
|
13
|
+
subject { described_class.new.tap { |modules| modules << @mod } }
|
14
|
+
it 'adds descendant to the corresponding module' do
|
15
15
|
expect(@mod).to receive(:add_descendant).with('PostsController')
|
16
16
|
subject.add_module_descendant('PostsHelper', 'PostsController')
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'Mod' do
|
21
|
-
subject
|
21
|
+
subject do
|
22
22
|
Mod.new('UsersHelper', ['Admin']).tap do |mod|
|
23
23
|
mod.add_descendant('Admin::UsersController')
|
24
24
|
end
|
25
|
-
|
25
|
+
end
|
26
26
|
it { expect(subject.to_s).to eq('Admin::UsersHelper') }
|
27
27
|
it { expect(subject.descendants).to eq(['Admin::UsersController']) }
|
28
28
|
end
|
@@ -4,25 +4,25 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module RailsBestPractices::Core
|
6
6
|
describe Routes do
|
7
|
-
let(:routes) {
|
7
|
+
let(:routes) { described_class.new }
|
8
8
|
|
9
|
-
it '
|
9
|
+
it 'adds route' do
|
10
10
|
routes.add_route(%w[admin test], 'posts', 'new')
|
11
11
|
expect(routes.map(&:to_s)).to eq(['Admin::Test::PostsController#new'])
|
12
12
|
end
|
13
13
|
|
14
14
|
context 'route' do
|
15
|
-
it '
|
15
|
+
it 'adds namesapces, controller name and action name' do
|
16
16
|
route = Route.new(%w[admin test], 'posts', 'new')
|
17
17
|
expect(route.to_s).to eq('Admin::Test::PostsController#new')
|
18
18
|
end
|
19
19
|
|
20
|
-
it '
|
20
|
+
it 'adds controller name with namespace' do
|
21
21
|
route = Route.new(['admin'], 'test/posts', 'new')
|
22
22
|
expect(route.to_s).to eq('Admin::Test::PostsController#new')
|
23
23
|
end
|
24
24
|
|
25
|
-
it '
|
25
|
+
it 'adds routes without controller' do
|
26
26
|
route = Route.new(['posts'], nil, 'new')
|
27
27
|
expect(route.to_s).to eq('PostsController#new')
|
28
28
|
end
|
@@ -6,20 +6,22 @@ module RailsBestPractices::Core
|
|
6
6
|
describe Runner do
|
7
7
|
describe 'load_plugin_reviews' do
|
8
8
|
shared_examples_for 'load_plugin_reviews' do
|
9
|
-
it '
|
10
|
-
runner =
|
11
|
-
expect(runner.instance_variable_get('@reviews').map(&:class)).to include(
|
9
|
+
it 'loads plugins in lib/rails_best_practices/plugins/reviews' do
|
10
|
+
runner = described_class.new
|
11
|
+
expect(runner.instance_variable_get('@reviews').map(&:class)).to include(
|
12
|
+
RailsBestPractices::Plugins::Reviews::NotUseRailsRootReview
|
13
|
+
)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
17
|
context 'given a path that ends with a slash' do
|
16
|
-
before {
|
17
|
-
|
18
|
+
before { described_class.base_path = 'spec/fixtures/' }
|
19
|
+
it_behaves_like 'load_plugin_reviews'
|
18
20
|
end
|
19
21
|
|
20
22
|
context 'given a path that does not end with a slash' do
|
21
|
-
before {
|
22
|
-
|
23
|
+
before { described_class.base_path = 'spec/fixtures' }
|
24
|
+
it_behaves_like 'load_plugin_reviews'
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Erubis::OnlyRuby do
|
6
|
-
subject
|
6
|
+
subject do
|
7
7
|
content = <<-EOF
|
8
8
|
<h1>Title</h1>
|
9
9
|
<% if current_user %>
|
@@ -13,14 +13,14 @@ describe Erubis::OnlyRuby do
|
|
13
13
|
Not logged in
|
14
14
|
<% end %>
|
15
15
|
EOF
|
16
|
-
|
17
|
-
|
16
|
+
described_class.new(content).src
|
17
|
+
end
|
18
18
|
|
19
|
-
it {
|
20
|
-
it {
|
21
|
-
it {
|
22
|
-
it {
|
23
|
-
it {
|
24
|
-
it {
|
25
|
-
it {
|
19
|
+
it { is_expected.not_to include('h1') }
|
20
|
+
it { is_expected.not_to include('Title') }
|
21
|
+
it { is_expected.not_to include('Not logged in') }
|
22
|
+
it { is_expected.to include('current_user') }
|
23
|
+
it { is_expected.to include('if') }
|
24
|
+
it { is_expected.to include('else') }
|
25
|
+
it { is_expected.to include('end') }
|
26
26
|
end
|
@@ -5,51 +5,52 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Lexicals
|
7
7
|
describe LongLineCheck do
|
8
|
-
it '
|
9
|
-
runner = Core::Runner.new(lexicals:
|
10
|
-
content =
|
11
|
-
class User < ActiveRecord::Base
|
12
|
-
# 81 Chars
|
13
|
-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
14
|
-
# 80 Chars
|
15
|
-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
16
|
-
end
|
17
|
-
EOF
|
8
|
+
it 'finds long lines' do
|
9
|
+
runner = Core::Runner.new(lexicals: described_class.new)
|
10
|
+
content = <<~EOF
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
# 81 Chars
|
13
|
+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
14
|
+
# 80 Chars
|
15
|
+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
16
|
+
end
|
17
|
+
EOF
|
18
18
|
content = content.gsub("\n", "\t\n")
|
19
19
|
runner.lexical('app/models/user.rb', content)
|
20
20
|
expect(runner.errors.size).to eq(1)
|
21
21
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:3 - line is longer than 80 characters (81 characters)')
|
22
22
|
end
|
23
|
-
it '
|
24
|
-
runner = Core::Runner.new(lexicals:
|
25
|
-
content =
|
26
|
-
class User < ActiveRecord::Base
|
27
|
-
# 91 Chars
|
28
|
-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
29
|
-
# 90 Chars
|
30
|
-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
31
|
-
end
|
32
|
-
EOF
|
23
|
+
it 'finds long lines with own max size' do
|
24
|
+
runner = Core::Runner.new(lexicals: described_class.new('max_line_length' => 90))
|
25
|
+
content = <<~EOF
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
# 91 Chars
|
28
|
+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
29
|
+
# 90 Chars
|
30
|
+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
31
|
+
end
|
32
|
+
EOF
|
33
33
|
content = content.gsub("\n", "\t\n")
|
34
34
|
runner.lexical('app/models/user.rb', content)
|
35
35
|
expect(runner.errors.size).to eq(1)
|
36
36
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:3 - line is longer than 90 characters (91 characters)')
|
37
37
|
end
|
38
|
-
it '
|
39
|
-
runner = Core::Runner.new(lexicals:
|
40
|
-
content =
|
38
|
+
it 'does not check non .rb files' do
|
39
|
+
runner = Core::Runner.new(lexicals: described_class.new)
|
40
|
+
content =
|
41
|
+
'
|
41
42
|
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
42
|
-
|
43
|
+
'
|
43
44
|
runner.lexical('app/views/users/index.html.erb', content)
|
44
45
|
expect(runner.errors.size).to eq(0)
|
45
46
|
end
|
46
|
-
it '
|
47
|
-
runner = Core::Runner.new(lexicals:
|
48
|
-
content =
|
49
|
-
class User < ActiveRecord::Base
|
50
|
-
# 81 Chars
|
51
|
-
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
52
|
-
end
|
47
|
+
it 'does not check ignored files' do
|
48
|
+
runner = Core::Runner.new(lexicals: described_class.new(max_line_length: 80, ignored_files: /user/))
|
49
|
+
content = <<~EOF
|
50
|
+
class User < ActiveRecord::Base
|
51
|
+
# 81 Chars
|
52
|
+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
53
|
+
end
|
53
54
|
EOF
|
54
55
|
content = content.gsub("\n", "\t\n")
|
55
56
|
runner.lexical('app/models/user.rb', content)
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Lexicals
|
7
7
|
describe RemoveTabCheck do
|
8
|
-
let(:runner) { Core::Runner.new(lexicals:
|
8
|
+
let(:runner) { Core::Runner.new(lexicals: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'removes tab' do
|
11
11
|
content = <<-EOF
|
12
12
|
class User < ActiveRecord::Base
|
13
13
|
has_many :projects
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:1 - remove tab, use spaces instead')
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'removes tab with third line' do
|
23
23
|
content = <<-EOF
|
24
24
|
class User < ActiveRecord::Base
|
25
25
|
has_many :projects
|
@@ -31,7 +31,7 @@ module RailsBestPractices
|
|
31
31
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:3 - remove tab, use spaces instead')
|
32
32
|
end
|
33
33
|
|
34
|
-
it '
|
34
|
+
it 'does not remove trailing whitespace' do
|
35
35
|
content = <<-EOF
|
36
36
|
class User < ActiveRecord::Base
|
37
37
|
has_many :projects
|
@@ -40,8 +40,8 @@ module RailsBestPractices
|
|
40
40
|
runner.lexical('app/models/user.rb', content)
|
41
41
|
expect(runner.errors.size).to eq(0)
|
42
42
|
end
|
43
|
-
it '
|
44
|
-
runner = Core::Runner.new(lexicals:
|
43
|
+
it 'does not check ignored files' do
|
44
|
+
runner = Core::Runner.new(lexicals: described_class.new(ignored_files: /user/))
|
45
45
|
content = <<-EOF
|
46
46
|
class User < ActiveRecord::Base
|
47
47
|
has_many :projects
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Lexicals
|
7
7
|
describe RemoveTrailingWhitespaceCheck do
|
8
|
-
let(:runner) { Core::Runner.new(lexicals:
|
8
|
+
let(:runner) { Core::Runner.new(lexicals: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'removes trailing whitespace' do
|
11
11
|
content = <<-EOF
|
12
12
|
class User < ActiveRecord::Base
|
13
13
|
has_many :projects
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:1 - remove trailing whitespace')
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'removes whitespace with third line' do
|
23
23
|
content = <<-EOF
|
24
24
|
class User < ActiveRecord::Base
|
25
25
|
has_many :projects
|
@@ -31,7 +31,7 @@ module RailsBestPractices
|
|
31
31
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:3 - remove trailing whitespace')
|
32
32
|
end
|
33
33
|
|
34
|
-
it '
|
34
|
+
it 'does not remove trailing whitespace' do
|
35
35
|
content = <<-EOF
|
36
36
|
class User < ActiveRecord::Base
|
37
37
|
has_many :projects
|
@@ -41,8 +41,8 @@ module RailsBestPractices
|
|
41
41
|
expect(runner.errors.size).to eq(0)
|
42
42
|
end
|
43
43
|
|
44
|
-
it '
|
45
|
-
runner = Core::Runner.new(lexicals:
|
44
|
+
it 'does not check ignored files' do
|
45
|
+
runner = Core::Runner.new(lexicals: described_class.new(ignored_files: /user/))
|
46
46
|
content = <<-EOF
|
47
47
|
class User < ActiveRecord::Base
|
48
48
|
has_many :projects
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe ConfigPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
10
|
context 'configs' do
|
11
|
-
it '
|
11
|
+
it 'parses configs' do
|
12
12
|
content = <<-EOF
|
13
13
|
module RailsBestPracticesCom
|
14
14
|
class Application < Rails::Application
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe ControllerPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares: [
|
8
|
+
let(:runner) { Core::Runner.new(prepares: [described_class.new, HelperPrepare.new]) }
|
9
9
|
|
10
10
|
context 'methods' do
|
11
|
-
it '
|
11
|
+
it 'parses controller methods' do
|
12
12
|
content = <<-EOF
|
13
13
|
class PostsController < ApplicationController
|
14
14
|
def index; end
|
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(%w[index show])
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'parses model methods with access control' do
|
24
24
|
content = <<-EOF
|
25
25
|
class PostsController < ApplicationController
|
26
26
|
def index; end
|
@@ -39,7 +39,7 @@ module RailsBestPractices
|
|
39
39
|
expect(methods.get_methods('PostsController', 'private').map(&:method_name)).to eq(['resource'])
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
42
|
+
it 'parses controller methods with module ::' do
|
43
43
|
content = <<-EOF
|
44
44
|
class Admin::Blog::PostsController < ApplicationController
|
45
45
|
def index; end
|
@@ -51,7 +51,7 @@ module RailsBestPractices
|
|
51
51
|
expect(methods.get_methods('Admin::Blog::PostsController').map(&:method_name)).to eq(%w[index show])
|
52
52
|
end
|
53
53
|
|
54
|
-
it '
|
54
|
+
it 'parses controller methods with module' do
|
55
55
|
content = <<-EOF
|
56
56
|
module Admin
|
57
57
|
module Blog
|
@@ -75,7 +75,9 @@ module RailsBestPractices
|
|
75
75
|
EOF
|
76
76
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
77
77
|
methods = Prepares.controller_methods
|
78
|
-
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
78
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
79
|
+
%w[index show new create edit update destroy]
|
80
|
+
)
|
79
81
|
end
|
80
82
|
|
81
83
|
it 'extend inherited_resources with actions' do
|
@@ -97,7 +99,9 @@ module RailsBestPractices
|
|
97
99
|
EOF
|
98
100
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
99
101
|
methods = Prepares.controller_methods
|
100
|
-
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
102
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
103
|
+
%w[index new create edit update destroy]
|
104
|
+
)
|
101
105
|
end
|
102
106
|
|
103
107
|
it 'extend inherited_resources with all actions with no arguments' do
|
@@ -108,7 +112,9 @@ module RailsBestPractices
|
|
108
112
|
EOF
|
109
113
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
110
114
|
methods = Prepares.controller_methods
|
111
|
-
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
115
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
116
|
+
%w[index show new create edit update destroy]
|
117
|
+
)
|
112
118
|
end
|
113
119
|
|
114
120
|
it 'DSL inherit_resources' do
|
@@ -119,13 +125,15 @@ module RailsBestPractices
|
|
119
125
|
EOF
|
120
126
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
121
127
|
methods = Prepares.controller_methods
|
122
|
-
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
128
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(
|
129
|
+
%w[index show new create edit update destroy]
|
130
|
+
)
|
123
131
|
end
|
124
132
|
end
|
125
133
|
end
|
126
134
|
|
127
135
|
context 'helpers' do
|
128
|
-
it '
|
136
|
+
it 'adds helper descendant' do
|
129
137
|
content = <<-EOF
|
130
138
|
module PostsHelper
|
131
139
|
end
|