rails_best_practices 1.19.3 → 1.21.0
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 +10 -11
- data/Gemfile +3 -5
- data/Gemfile.lock +125 -0
- data/Guardfile +2 -0
- data/README.md +5 -1
- data/Rakefile +2 -17
- data/assets/result.html.erb +2 -0
- data/lib/rails_best_practices.rb +4 -2
- data/lib/rails_best_practices/analyzer.rb +63 -51
- 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 +63 -55
- 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 +87 -72
- data/lib/rails_best_practices/inline_disables.rb +3 -0
- data/lib/rails_best_practices/inline_disables/comment_ripper.rb +19 -0
- data/lib/rails_best_practices/inline_disables/inline_disable.rb +50 -0
- 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 +23 -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 -88
- 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 +20 -17
- 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 +14 -10
- 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 +63 -60
- 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 +48 -49
- 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/inline_disables/inline_disable_spec.rb +62 -0
- 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 +47 -32
- 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 +21 -14
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe DefaultScopeIsEvilReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'detects default_scope with -> syntax' do
|
11
11
|
content = <<-EOF
|
12
12
|
class User < ActiveRecord::Base
|
13
13
|
default_scope -> { order('created_at desc') }
|
@@ -18,7 +18,7 @@ module RailsBestPractices
|
|
18
18
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:2 - default_scope is evil')
|
19
19
|
end
|
20
20
|
|
21
|
-
it '
|
21
|
+
it 'detects default_scope with old syntax' do
|
22
22
|
content = <<-EOF
|
23
23
|
class User < ActiveRecord::Base
|
24
24
|
default_scope order('created_at desc')
|
@@ -29,7 +29,7 @@ module RailsBestPractices
|
|
29
29
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:2 - default_scope is evil')
|
30
30
|
end
|
31
31
|
|
32
|
-
it '
|
32
|
+
it 'does not detect default_scope' do
|
33
33
|
content = <<-EOF
|
34
34
|
class User < ActiveRecord::Base
|
35
35
|
scope :default, -> { order('created_at desc') }
|
@@ -39,8 +39,8 @@ module RailsBestPractices
|
|
39
39
|
expect(runner.errors.size).to eq(0)
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
43
|
-
runner = Core::Runner.new(reviews:
|
42
|
+
it 'does not check ignored files' do
|
43
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: /user/))
|
44
44
|
content = <<-EOF
|
45
45
|
class User < ActiveRecord::Base
|
46
46
|
default_scope -> { order('created_at desc') }
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe DryBundlerInCapistranoReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'dries bundler in capistrno' do
|
11
11
|
content = <<-EOF
|
12
12
|
namespace :bundler do
|
13
13
|
task :create_symlink, roles: :app do
|
@@ -29,7 +29,7 @@ module RailsBestPractices
|
|
29
29
|
expect(runner.errors[0].to_s).to eq('config/deploy.rb:1 - dry bundler in capistrano')
|
30
30
|
end
|
31
31
|
|
32
|
-
it '
|
32
|
+
it 'does not dry bundler in capistrano' do
|
33
33
|
content = <<-EOF
|
34
34
|
require 'bundler/capistrano'
|
35
35
|
EOF
|
@@ -37,8 +37,8 @@ module RailsBestPractices
|
|
37
37
|
expect(runner.errors.size).to eq(0)
|
38
38
|
end
|
39
39
|
|
40
|
-
it '
|
41
|
-
runner = Core::Runner.new(reviews:
|
40
|
+
it 'does not check ignored files' do
|
41
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: /deploy\.rb/))
|
42
42
|
content = <<-EOF
|
43
43
|
namespace :bundler do
|
44
44
|
task :create_symlink, roles: :app do
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe HashSyntaxReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'finds 1.8 Hash with symbol' do
|
11
11
|
content = <<-EOF
|
12
12
|
class User < ActiveRecord::Base
|
13
13
|
CONST = { :foo => :bar }
|
@@ -18,7 +18,7 @@ module RailsBestPractices
|
|
18
18
|
expect(runner.errors[0].to_s).to eq('app/models/user.rb:2 - change Hash Syntax to 1.9')
|
19
19
|
end
|
20
20
|
|
21
|
-
it '
|
21
|
+
it 'does not find 1.8 Hash with string' do
|
22
22
|
content = <<-EOF
|
23
23
|
class User < ActiveRecord::Base
|
24
24
|
CONST = { "foo" => "bar" }
|
@@ -28,7 +28,7 @@ module RailsBestPractices
|
|
28
28
|
expect(runner.errors.size).to eq(0)
|
29
29
|
end
|
30
30
|
|
31
|
-
it '
|
31
|
+
it 'does not alert on 1.9 Syntax' do
|
32
32
|
content = <<-EOF
|
33
33
|
class User < ActiveRecord::Base
|
34
34
|
CONST = { foo: :bar }
|
@@ -38,17 +38,17 @@ module RailsBestPractices
|
|
38
38
|
expect(runner.errors.size).to eq(0)
|
39
39
|
end
|
40
40
|
|
41
|
-
it '
|
42
|
-
content =
|
43
|
-
%div{ class: "foo1" }
|
44
|
-
.div{ class: "foo2" }
|
45
|
-
#div{ class: "foo3" }
|
41
|
+
it 'ignores haml_out' do
|
42
|
+
content = <<~EOF
|
43
|
+
%div{ class: "foo1" }
|
44
|
+
.div{ class: "foo2" }
|
45
|
+
#div{ class: "foo3" }
|
46
46
|
EOF
|
47
47
|
runner.review('app/views/files/show.html.haml', content)
|
48
48
|
expect(runner.errors.size).to eq(0)
|
49
49
|
end
|
50
50
|
|
51
|
-
it '
|
51
|
+
it 'does not consider hash with array key' do
|
52
52
|
content = <<-EOF
|
53
53
|
transition [:unverified, :verified] => :deleted
|
54
54
|
EOF
|
@@ -56,7 +56,7 @@ module RailsBestPractices
|
|
56
56
|
expect(runner.errors.size).to eq(0)
|
57
57
|
end
|
58
58
|
|
59
|
-
it '
|
59
|
+
it 'does not consider hash with charaters not valid for symbol' do
|
60
60
|
content = <<-EOF
|
61
61
|
receiver.stub(:` => 'Error')
|
62
62
|
EOF
|
@@ -64,8 +64,8 @@ module RailsBestPractices
|
|
64
64
|
expect(runner.errors.size).to eq(0)
|
65
65
|
end
|
66
66
|
|
67
|
-
it '
|
68
|
-
runner = Core::Runner.new(reviews:
|
67
|
+
it 'does not check ignored files' do
|
68
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: /user/))
|
69
69
|
content = <<-EOF
|
70
70
|
class User < ActiveRecord::Base
|
71
71
|
CONST = { :foo => :bar }
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe IsolateSeedDataReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
10
|
context 'create' do
|
11
|
-
it '
|
11
|
+
it 'isolates seed data' do
|
12
12
|
content = <<-EOF
|
13
13
|
class CreateRoles < ActiveRecord::Migration
|
14
14
|
def self.up
|
@@ -33,7 +33,7 @@ module RailsBestPractices
|
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'new and save' do
|
36
|
-
it '
|
36
|
+
it 'isolates seed data for local variable' do
|
37
37
|
content = <<-EOF
|
38
38
|
class CreateRoles < ActiveRecord::Migration
|
39
39
|
def self.up
|
@@ -57,7 +57,7 @@ module RailsBestPractices
|
|
57
57
|
expect(runner.errors[0].to_s).to eq('db/migrate/20090818130258_create_roles.rb:9 - isolate seed data')
|
58
58
|
end
|
59
59
|
|
60
|
-
it '
|
60
|
+
it 'isolates seed data for instance variable' do
|
61
61
|
content = <<-EOF
|
62
62
|
class CreateRoles < ActiveRecord::Migration
|
63
63
|
def self.up
|
@@ -82,7 +82,7 @@ module RailsBestPractices
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
it '
|
85
|
+
it 'does not isolate seed data without data insert' do
|
86
86
|
content = <<-EOF
|
87
87
|
class CreateRoles < ActiveRecord::Migration
|
88
88
|
def self.up
|
@@ -100,8 +100,8 @@ module RailsBestPractices
|
|
100
100
|
expect(runner.errors.size).to eq(0)
|
101
101
|
end
|
102
102
|
|
103
|
-
it '
|
104
|
-
runner = Core::Runner.new(reviews:
|
103
|
+
it 'does not check ignored files' do
|
104
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: /create_roles/))
|
105
105
|
content = <<-EOF
|
106
106
|
class CreateRoles < ActiveRecord::Migration
|
107
107
|
def self.up
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe KeepFindersOnTheirOwnModelReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'keeps finders on thier own model' do
|
11
11
|
content = <<-EOF
|
12
12
|
class Post < ActiveRecord::Base
|
13
13
|
has_many :comments
|
@@ -23,7 +23,7 @@ module RailsBestPractices
|
|
23
23
|
expect(runner.errors[0].to_s).to eq('app/models/post.rb:5 - keep finders on their own model')
|
24
24
|
end
|
25
25
|
|
26
|
-
it '
|
26
|
+
it 'keeps finders on thier own model with all method' do
|
27
27
|
content = <<-EOF
|
28
28
|
class Post < ActiveRecord::Base
|
29
29
|
has_many :comments
|
@@ -39,7 +39,7 @@ module RailsBestPractices
|
|
39
39
|
expect(runner.errors[0].to_s).to eq('app/models/post.rb:5 - keep finders on their own model')
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
42
|
+
it 'does not keep finders on thier own model with self finder' do
|
43
43
|
content = <<-EOF
|
44
44
|
class Post < ActiveRecord::Base
|
45
45
|
has_many :comments
|
@@ -54,7 +54,7 @@ module RailsBestPractices
|
|
54
54
|
expect(runner.errors.size).to eq(0)
|
55
55
|
end
|
56
56
|
|
57
|
-
it '
|
57
|
+
it 'does not keep finders on thier own model with own finder' do
|
58
58
|
content = <<-EOF
|
59
59
|
class Post < ActiveRecord::Base
|
60
60
|
has_many :comments
|
@@ -69,7 +69,7 @@ module RailsBestPractices
|
|
69
69
|
expect(runner.errors.size).to eq(0)
|
70
70
|
end
|
71
71
|
|
72
|
-
it '
|
72
|
+
it 'does not keep finders on their own model without finder' do
|
73
73
|
content = <<-EOF
|
74
74
|
class Post < ActiveRecord::Base
|
75
75
|
has_many :comments
|
@@ -83,7 +83,7 @@ module RailsBestPractices
|
|
83
83
|
expect(runner.errors.size).to eq(0)
|
84
84
|
end
|
85
85
|
|
86
|
-
it '
|
86
|
+
it 'does not keep finders on their own model with ruby Array#find' do
|
87
87
|
content = <<-EOF
|
88
88
|
class Post < ActiveRecord::Base
|
89
89
|
has_many :comments
|
@@ -97,8 +97,8 @@ module RailsBestPractices
|
|
97
97
|
expect(runner.errors.size).to eq(0)
|
98
98
|
end
|
99
99
|
|
100
|
-
it '
|
101
|
-
runner = Core::Runner.new(reviews:
|
100
|
+
it 'does not check ignored files' do
|
101
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: %r{app/models/post\.rb}))
|
102
102
|
content = <<-EOF
|
103
103
|
class Post < ActiveRecord::Base
|
104
104
|
has_many :comments
|
@@ -5,10 +5,14 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe LawOfDemeterReview do
|
8
|
-
let(:runner)
|
8
|
+
let(:runner) do
|
9
|
+
Core::Runner.new(
|
10
|
+
prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new], reviews: described_class.new
|
11
|
+
)
|
12
|
+
end
|
9
13
|
|
10
14
|
describe 'belongs_to' do
|
11
|
-
before
|
15
|
+
before do
|
12
16
|
content = <<-EOF
|
13
17
|
class Invoice < ActiveRecord::Base
|
14
18
|
belongs_to :user
|
@@ -28,7 +32,7 @@ module RailsBestPractices
|
|
28
32
|
runner.prepare('db/schema.rb', content)
|
29
33
|
end
|
30
34
|
|
31
|
-
it '
|
35
|
+
it 'laws of demeter with erb' do
|
32
36
|
content = <<-EOF
|
33
37
|
<%= @invoice.user.name %>
|
34
38
|
<%= @invoice.user.address %>
|
@@ -39,29 +43,29 @@ module RailsBestPractices
|
|
39
43
|
expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.erb:1 - law of demeter')
|
40
44
|
end
|
41
45
|
|
42
|
-
it '
|
43
|
-
content =
|
44
|
-
= @invoice.user.name
|
45
|
-
= @invoice.user.address
|
46
|
-
= @invoice.user.cellphone
|
46
|
+
it 'laws of demeter with haml' do
|
47
|
+
content = <<~EOF
|
48
|
+
= @invoice.user.name
|
49
|
+
= @invoice.user.address
|
50
|
+
= @invoice.user.cellphone
|
47
51
|
EOF
|
48
52
|
runner.review('app/views/invoices/show.html.haml', content)
|
49
53
|
expect(runner.errors.size).to eq(3)
|
50
54
|
expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.haml:1 - law of demeter')
|
51
55
|
end
|
52
56
|
|
53
|
-
it '
|
54
|
-
content =
|
55
|
-
= @invoice.user.name
|
56
|
-
= @invoice.user.address
|
57
|
-
= @invoice.user.cellphone
|
57
|
+
it 'laws of demeter with slim' do
|
58
|
+
content = <<~EOF
|
59
|
+
= @invoice.user.name
|
60
|
+
= @invoice.user.address
|
61
|
+
= @invoice.user.cellphone
|
58
62
|
EOF
|
59
63
|
runner.review('app/views/invoices/show.html.slim', content)
|
60
64
|
expect(runner.errors.size).to eq(3)
|
61
65
|
expect(runner.errors[0].to_s).to eq('app/views/invoices/show.html.slim:1 - law of demeter')
|
62
66
|
end
|
63
67
|
|
64
|
-
it '
|
68
|
+
it 'noes law of demeter' do
|
65
69
|
content = <<-EOF
|
66
70
|
<%= @invoice.user_name %>
|
67
71
|
<%= @invoice.user_address %>
|
@@ -73,7 +77,7 @@ module RailsBestPractices
|
|
73
77
|
end
|
74
78
|
|
75
79
|
describe 'has_one' do
|
76
|
-
before
|
80
|
+
before do
|
77
81
|
content = <<-EOF
|
78
82
|
class Invoice < ActiveRecord::Base
|
79
83
|
has_one :price
|
@@ -92,7 +96,7 @@ module RailsBestPractices
|
|
92
96
|
runner.prepare('db/schema.rb', content)
|
93
97
|
end
|
94
98
|
|
95
|
-
it '
|
99
|
+
it 'laws of demeter' do
|
96
100
|
content = <<-EOF
|
97
101
|
<%= @invoice.price.currency %>
|
98
102
|
<%= @invoice.price.number %>
|
@@ -104,7 +108,7 @@ module RailsBestPractices
|
|
104
108
|
end
|
105
109
|
|
106
110
|
context 'polymorphic association' do
|
107
|
-
before
|
111
|
+
before do
|
108
112
|
content = <<-EOF
|
109
113
|
class Comment < ActiveRecord::Base
|
110
114
|
belongs_to :commentable, polymorphic: true
|
@@ -129,7 +133,7 @@ module RailsBestPractices
|
|
129
133
|
runner.prepare('db/schema.rb', content)
|
130
134
|
end
|
131
135
|
|
132
|
-
it '
|
136
|
+
it 'laws of demeter' do
|
133
137
|
content = <<-EOF
|
134
138
|
<%= @comment.commentable.title %>
|
135
139
|
EOF
|
@@ -139,7 +143,7 @@ module RailsBestPractices
|
|
139
143
|
end
|
140
144
|
end
|
141
145
|
|
142
|
-
it '
|
146
|
+
it 'noes law of demeter with method call' do
|
143
147
|
content = <<-EOF
|
144
148
|
class Question < ActiveRecord::Base
|
145
149
|
has_many :answers, dependent: :destroy
|
@@ -163,9 +167,12 @@ module RailsBestPractices
|
|
163
167
|
expect(runner.errors.size).to eq(0)
|
164
168
|
end
|
165
169
|
|
166
|
-
it '
|
167
|
-
runner =
|
168
|
-
|
170
|
+
it 'does not check ignored files' do
|
171
|
+
runner =
|
172
|
+
Core::Runner.new(
|
173
|
+
prepares: [Prepares::ModelPrepare.new, Prepares::SchemaPrepare.new],
|
174
|
+
reviews: described_class.new(ignored_files: %r{app/views/invoices})
|
175
|
+
)
|
169
176
|
content = <<-EOF
|
170
177
|
<%= @invoice.user.name %>
|
171
178
|
<%= @invoice.user.address %>
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe MoveCodeIntoControllerReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'moves code into controller for method call' do
|
11
11
|
content = <<-EOF
|
12
12
|
<% Post.find(:all).each do |post| %>
|
13
13
|
<%=h post.title %>
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - move code into controller')
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'moves code into controller for assign' do
|
23
23
|
content = <<-EOF
|
24
24
|
<% @posts = Post.all %>
|
25
25
|
<% @posts.each do |post| %>
|
@@ -32,7 +32,7 @@ module RailsBestPractices
|
|
32
32
|
expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - move code into controller')
|
33
33
|
end
|
34
34
|
|
35
|
-
it '
|
35
|
+
it 'does not move code into controller' do
|
36
36
|
content = <<-EOF
|
37
37
|
<% @posts.each do |post| %>
|
38
38
|
<%=h post.title %>
|
@@ -43,8 +43,8 @@ module RailsBestPractices
|
|
43
43
|
expect(runner.errors.size).to eq(0)
|
44
44
|
end
|
45
45
|
|
46
|
-
it '
|
47
|
-
runner = Core::Runner.new(reviews:
|
46
|
+
it 'does not check ignored files' do
|
47
|
+
runner = Core::Runner.new(reviews: described_class.new(ignored_files: %r{app/views/post}))
|
48
48
|
content = <<-EOF
|
49
49
|
<% Post.find(:all).each do |post| %>
|
50
50
|
<%=h post.title %>
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Reviews
|
7
7
|
describe MoveCodeIntoHelperReview do
|
8
|
-
let(:runner) { Core::Runner.new(reviews:
|
8
|
+
let(:runner) { Core::Runner.new(reviews: described_class.new('array_count' => 2)) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'moves code into helper' do
|
11
11
|
content = <<-EOF
|
12
12
|
<%= select_tag :state, options_for_select( [[t(:draft), "draft"],
|
13
13
|
[t(:published), "published"]],
|
@@ -15,10 +15,12 @@ module RailsBestPractices
|
|
15
15
|
EOF
|
16
16
|
runner.review('app/views/posts/show.html.erb', content)
|
17
17
|
expect(runner.errors.size).to eq(1)
|
18
|
-
expect(runner.errors[0].to_s).to eq(
|
18
|
+
expect(runner.errors[0].to_s).to eq(
|
19
|
+
'app/views/posts/show.html.erb:1 - move code into helper (array_count >= 2)'
|
20
|
+
)
|
19
21
|
end
|
20
22
|
|
21
|
-
it '
|
23
|
+
it 'does not move code into helper with simple arguments' do
|
22
24
|
content = <<-EOF
|
23
25
|
<%= select_tag :state, options_for_select( Post.STATES ) %>
|
24
26
|
EOF
|
@@ -26,8 +28,11 @@ module RailsBestPractices
|
|
26
28
|
expect(runner.errors.size).to eq(0)
|
27
29
|
end
|
28
30
|
|
29
|
-
it '
|
30
|
-
runner =
|
31
|
+
it 'does not check ignored files' do
|
32
|
+
runner =
|
33
|
+
Core::Runner.new(
|
34
|
+
reviews: MoveCodeIntoControllerReview.new('array_count' => 2, 'ignored_files' => %r{app/views/post})
|
35
|
+
)
|
31
36
|
content = <<-EOF
|
32
37
|
<%= select_tag :state, options_for_select( [[t(:draft), "draft"],
|
33
38
|
[t(:published), "published"]],
|