rails_best_practices 1.20.0 → 1.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/Gemfile.lock +30 -26
- data/Guardfile +2 -0
- data/Rakefile +2 -0
- data/lib/rails_best_practices.rb +1 -2
- data/lib/rails_best_practices/analyzer.rb +56 -46
- data/lib/rails_best_practices/core/check.rb +39 -32
- data/lib/rails_best_practices/core/checks_loader.rb +8 -6
- 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 +21 -16
- data/lib/rails_best_practices/core/model_associations.rb +9 -4
- 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 +29 -33
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +17 -6
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +15 -3
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -0
- data/lib/rails_best_practices/prepares/model_prepare.rb +52 -12
- data/lib/rails_best_practices/prepares/route_prepare.rb +16 -10
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +15 -13
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +34 -29
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +14 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +19 -8
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +5 -5
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +4 -4
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +7 -8
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +6 -6
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +6 -7
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +7 -8
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +12 -10
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +5 -5
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +5 -2
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +6 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +6 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +29 -9
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +3 -3
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +17 -15
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +1 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_observer_review.rb +9 -9
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +26 -26
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +8 -7
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +17 -15
- 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 +35 -36
- 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 -21
- 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 +11 -10
- 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 +2 -2
- 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 +138 -77
- 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 +9 -9
- 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 +21 -14
- 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 +26 -16
- 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 -19
- 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 +44 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +17 -12
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +46 -44
- 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 +16 -10
- 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 +21 -17
- 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 +9 -7
- 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 +10 -8
- metadata +7 -7
@@ -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,8 +5,8 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Lexicals
|
7
7
|
describe LongLineCheck do
|
8
|
-
it '
|
9
|
-
runner = Core::Runner.new(lexicals:
|
8
|
+
it 'finds long lines' do
|
9
|
+
runner = Core::Runner.new(lexicals: described_class.new)
|
10
10
|
content = <<~EOF
|
11
11
|
class User < ActiveRecord::Base
|
12
12
|
# 81 Chars
|
@@ -20,8 +20,8 @@ module RailsBestPractices
|
|
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:
|
23
|
+
it 'finds long lines with own max size' do
|
24
|
+
runner = Core::Runner.new(lexicals: described_class.new('max_line_length' => 90))
|
25
25
|
content = <<~EOF
|
26
26
|
class User < ActiveRecord::Base
|
27
27
|
# 91 Chars
|
@@ -35,16 +35,17 @@ module RailsBestPractices
|
|
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:
|
47
|
+
it 'does not check ignored files' do
|
48
|
+
runner = Core::Runner.new(lexicals: described_class.new(max_line_length: 80, ignored_files: /user/))
|
48
49
|
content = <<~EOF
|
49
50
|
class User < ActiveRecord::Base
|
50
51
|
# 81 Chars
|
@@ -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
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe GemfilePrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
10
|
context 'gemfile' do
|
11
|
-
it '
|
11
|
+
it 'parses gems' do
|
12
12
|
content = <<~EOF
|
13
13
|
GEM
|
14
14
|
remote: https://rubygems.org/
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe HelperPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
10
|
context 'methods' do
|
11
|
-
it '
|
11
|
+
it 'parses helper methods' do
|
12
12
|
content = <<-EOF
|
13
13
|
module PostsHelper
|
14
14
|
def used; end
|
@@ -20,7 +20,7 @@ module RailsBestPractices
|
|
20
20
|
expect(methods.get_methods('PostsHelper').map(&:method_name)).to eq(%w[used unused])
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'parses helpers' do
|
24
24
|
content = <<-EOF
|
25
25
|
module PostsHelper
|
26
26
|
end
|
@@ -5,10 +5,10 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe InitializerPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
10
|
context 'initializers' do
|
11
|
-
it '
|
11
|
+
it 'sets include_forbidden_attributes_protection config' do
|
12
12
|
content = <<-EOF
|
13
13
|
class AR
|
14
14
|
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
@@ -19,7 +19,7 @@ module RailsBestPractices
|
|
19
19
|
expect(configs['railsbp.include_forbidden_attributes_protection']).to eq('true')
|
20
20
|
end
|
21
21
|
|
22
|
-
it '
|
22
|
+
it 'does not set include_forbidden_attributes_protection config' do
|
23
23
|
content = <<-EOF
|
24
24
|
class AR
|
25
25
|
end
|
@@ -5,9 +5,9 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe MailerPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'parses mailer names' do
|
11
11
|
content = <<-EOF
|
12
12
|
class ProjectMailer < ActionMailer::Base
|
13
13
|
end
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
5
5
|
module RailsBestPractices
|
6
6
|
module Prepares
|
7
7
|
describe ModelPrepare do
|
8
|
-
let(:runner) { Core::Runner.new(prepares:
|
8
|
+
let(:runner) { Core::Runner.new(prepares: described_class.new) }
|
9
9
|
|
10
10
|
context 'models' do
|
11
11
|
it 'class_name with modules ::' do
|
@@ -32,7 +32,7 @@ module RailsBestPractices
|
|
32
32
|
end
|
33
33
|
|
34
34
|
context 'associations' do
|
35
|
-
it '
|
35
|
+
it 'parses model associations' do
|
36
36
|
content = <<-EOF
|
37
37
|
class Project < ActiveRecord::Base
|
38
38
|
belongs_to :portfolio
|
@@ -43,14 +43,22 @@ module RailsBestPractices
|
|
43
43
|
EOF
|
44
44
|
runner.prepare('app/models/project.rb', content)
|
45
45
|
model_associations = Prepares.model_associations
|
46
|
-
expect(model_associations.get_association('Project', 'portfolio')).to eq(
|
47
|
-
|
48
|
-
|
49
|
-
expect(model_associations.get_association('Project', '
|
46
|
+
expect(model_associations.get_association('Project', 'portfolio')).to eq(
|
47
|
+
'meta' => 'belongs_to', 'class_name' => 'Portfolio'
|
48
|
+
)
|
49
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
50
|
+
'meta' => 'has_one', 'class_name' => 'ProjectManager'
|
51
|
+
)
|
52
|
+
expect(model_associations.get_association('Project', 'milestones')).to eq(
|
53
|
+
'meta' => 'has_many', 'class_name' => 'Milestone'
|
54
|
+
)
|
55
|
+
expect(model_associations.get_association('Project', 'categories')).to eq(
|
56
|
+
'meta' => 'has_and_belongs_to_many', 'class_name' => 'Category'
|
57
|
+
)
|
50
58
|
end
|
51
59
|
|
52
60
|
context 'with class_name option' do
|
53
|
-
it '
|
61
|
+
it 'parses belongs_to' do
|
54
62
|
content = <<-EOF
|
55
63
|
class Post < ActiveRecord::Base
|
56
64
|
belongs_to :author, "class_name" => "Person"
|
@@ -58,10 +66,12 @@ module RailsBestPractices
|
|
58
66
|
EOF
|
59
67
|
runner.prepare('app/models/post.rb', content)
|
60
68
|
model_associations = Prepares.model_associations
|
61
|
-
expect(model_associations.get_association('Post', 'author')).to eq(
|
69
|
+
expect(model_associations.get_association('Post', 'author')).to eq(
|
70
|
+
'meta' => 'belongs_to', 'class_name' => 'Person'
|
71
|
+
)
|
62
72
|
end
|
63
73
|
|
64
|
-
it '
|
74
|
+
it 'parses has_one' do
|
65
75
|
content = <<-EOF
|
66
76
|
class Project < ActiveRecord::Base
|
67
77
|
has_one :project_manager, "class_name" => "Person"
|
@@ -69,10 +79,12 @@ module RailsBestPractices
|
|
69
79
|
EOF
|
70
80
|
runner.prepare('app/models/post.rb', content)
|
71
81
|
model_associations = Prepares.model_associations
|
72
|
-
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
82
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
83
|
+
'meta' => 'has_one', 'class_name' => 'Person'
|
84
|
+
)
|
73
85
|
end
|
74
86
|
|
75
|
-
it '
|
87
|
+
it 'parses has_many' do
|
76
88
|
content = <<-EOF
|
77
89
|
class Project < ActiveRecord::Base
|
78
90
|
has_many :people, "class_name" => "Person"
|
@@ -80,10 +92,12 @@ module RailsBestPractices
|
|
80
92
|
EOF
|
81
93
|
runner.prepare('app/models/project.rb', content)
|
82
94
|
model_associations = Prepares.model_associations
|
83
|
-
expect(model_associations.get_association('Project', 'people')).to eq(
|
95
|
+
expect(model_associations.get_association('Project', 'people')).to eq(
|
96
|
+
'meta' => 'has_many', 'class_name' => 'Person'
|
97
|
+
)
|
84
98
|
end
|
85
99
|
|
86
|
-
it '
|
100
|
+
it 'parses has_and_belongs_to_many' do
|
87
101
|
content = <<-EOF
|
88
102
|
class Citizen < ActiveRecord::Base
|
89
103
|
has_and_belongs_to_many :nations, "class_name" => "Country"
|
@@ -91,11 +105,13 @@ module RailsBestPractices
|
|
91
105
|
EOF
|
92
106
|
runner.prepare('app/models/citizen.rb', content)
|
93
107
|
model_associations = Prepares.model_associations
|
94
|
-
expect(model_associations.get_association('Citizen', 'nations')).to eq(
|
108
|
+
expect(model_associations.get_association('Citizen', 'nations')).to eq(
|
109
|
+
'meta' => 'has_and_belongs_to_many', 'class_name' => 'Country'
|
110
|
+
)
|
95
111
|
end
|
96
112
|
|
97
113
|
context 'namespace' do
|
98
|
-
it '
|
114
|
+
it 'parses with namespace' do
|
99
115
|
content = <<-EOF
|
100
116
|
class Community < ActiveRecord::Base
|
101
117
|
has_many :members
|
@@ -110,11 +126,15 @@ module RailsBestPractices
|
|
110
126
|
runner.prepare('app/models/community/member.rb', content)
|
111
127
|
runner.after_prepare
|
112
128
|
model_associations = Prepares.model_associations
|
113
|
-
expect(model_associations.get_association('Community', 'members')).to eq(
|
114
|
-
|
129
|
+
expect(model_associations.get_association('Community', 'members')).to eq(
|
130
|
+
'meta' => 'has_many', 'class_name' => 'Community::Member'
|
131
|
+
)
|
132
|
+
expect(model_associations.get_association('Community::Member', 'community')).to eq(
|
133
|
+
'meta' => 'belongs_to', 'class_name' => 'Community'
|
134
|
+
)
|
115
135
|
end
|
116
136
|
|
117
|
-
it '
|
137
|
+
it 'parses without namespace' do
|
118
138
|
content = <<-EOF
|
119
139
|
class Community::Member::Rating < ActiveRecord::Base
|
120
140
|
belongs_to :member
|
@@ -129,14 +149,18 @@ module RailsBestPractices
|
|
129
149
|
runner.prepare('app/models/community/member.rb', content)
|
130
150
|
runner.after_prepare
|
131
151
|
model_associations = Prepares.model_associations
|
132
|
-
expect(model_associations.get_association('Community::Member::Rating', 'member')).to eq(
|
133
|
-
|
152
|
+
expect(model_associations.get_association('Community::Member::Rating', 'member')).to eq(
|
153
|
+
'meta' => 'belongs_to', 'class_name' => 'Community::Member'
|
154
|
+
)
|
155
|
+
expect(model_associations.get_association('Community::Member', 'ratings')).to eq(
|
156
|
+
'meta' => 'has_many', 'class_name' => 'Community::Member::Rating'
|
157
|
+
)
|
134
158
|
end
|
135
159
|
end
|
136
160
|
end
|
137
161
|
|
138
162
|
context 'mongoid embeds' do
|
139
|
-
it '
|
163
|
+
it 'parses embeds_many' do
|
140
164
|
content = <<-EOF
|
141
165
|
class Person
|
142
166
|
include Mongoid::Document
|
@@ -145,10 +169,12 @@ module RailsBestPractices
|
|
145
169
|
EOF
|
146
170
|
runner.prepare('app/models/person.rb', content)
|
147
171
|
model_associations = Prepares.model_associations
|
148
|
-
expect(model_associations.get_association('Person', 'addresses')).to eq(
|
172
|
+
expect(model_associations.get_association('Person', 'addresses')).to eq(
|
173
|
+
'meta' => 'embeds_many', 'class_name' => 'Address'
|
174
|
+
)
|
149
175
|
end
|
150
176
|
|
151
|
-
it '
|
177
|
+
it 'parses embeds_one' do
|
152
178
|
content = <<-EOF
|
153
179
|
class Lush
|
154
180
|
include Mongoid::Document
|
@@ -157,10 +183,12 @@ module RailsBestPractices
|
|
157
183
|
EOF
|
158
184
|
runner.prepare('app/models/lush.rb', content)
|
159
185
|
model_associations = Prepares.model_associations
|
160
|
-
expect(model_associations.get_association('Lush', 'whiskey')).to eq(
|
186
|
+
expect(model_associations.get_association('Lush', 'whiskey')).to eq(
|
187
|
+
'meta' => 'embeds_one', 'class_name' => 'Drink'
|
188
|
+
)
|
161
189
|
end
|
162
190
|
|
163
|
-
it '
|
191
|
+
it 'parses embedded_in' do
|
164
192
|
content = <<-EOF
|
165
193
|
class Drink
|
166
194
|
include Mongoid::Document
|
@@ -169,12 +197,14 @@ module RailsBestPractices
|
|
169
197
|
EOF
|
170
198
|
runner.prepare('app/models/drink.rb', content)
|
171
199
|
model_associations = Prepares.model_associations
|
172
|
-
expect(model_associations.get_association('Drink', 'alcoholic')).to eq(
|
200
|
+
expect(model_associations.get_association('Drink', 'alcoholic')).to eq(
|
201
|
+
'meta' => 'embedded_in', 'class_name' => 'Lush'
|
202
|
+
)
|
173
203
|
end
|
174
204
|
end
|
175
205
|
|
176
206
|
context 'mongomapper many/one' do
|
177
|
-
it '
|
207
|
+
it 'parses one' do
|
178
208
|
content = <<-EOF
|
179
209
|
class Employee
|
180
210
|
include MongoMapper::Document
|
@@ -183,10 +213,12 @@ module RailsBestPractices
|
|
183
213
|
EOF
|
184
214
|
runner.prepare('app/models/employee.rb', content)
|
185
215
|
model_associations = Prepares.model_associations
|
186
|
-
expect(model_associations.get_association('Employee', 'desk')).to eq(
|
216
|
+
expect(model_associations.get_association('Employee', 'desk')).to eq(
|
217
|
+
'meta' => 'one', 'class_name' => 'Desk'
|
218
|
+
)
|
187
219
|
end
|
188
220
|
|
189
|
-
it '
|
221
|
+
it 'parses many' do
|
190
222
|
content = <<-EOF
|
191
223
|
class Tree
|
192
224
|
include MongoMapper::Document
|
@@ -201,7 +233,7 @@ module RailsBestPractices
|
|
201
233
|
end
|
202
234
|
|
203
235
|
context 'methods' do
|
204
|
-
it '
|
236
|
+
it 'parses model methods' do
|
205
237
|
content = <<-EOF
|
206
238
|
class Post < ActiveRecord::Base
|
207
239
|
def save; end
|
@@ -213,7 +245,7 @@ module RailsBestPractices
|
|
213
245
|
expect(methods.get_methods('Post').map(&:method_name)).to eq(%w[save find])
|
214
246
|
end
|
215
247
|
|
216
|
-
it '
|
248
|
+
it 'parses model methods with access control' do
|
217
249
|
content = <<-EOF
|
218
250
|
class Post < ActiveRecord::Base
|
219
251
|
def save; end
|
@@ -232,7 +264,7 @@ module RailsBestPractices
|
|
232
264
|
expect(methods.get_methods('Post', 'private').map(&:method_name)).to eq(['find_by_sql'])
|
233
265
|
end
|
234
266
|
|
235
|
-
it '
|
267
|
+
it 'parses model methods with module ::' do
|
236
268
|
content = <<-EOF
|
237
269
|
class Admin::Blog::Post < ActiveRecord::Base
|
238
270
|
def save; end
|
@@ -244,7 +276,7 @@ module RailsBestPractices
|
|
244
276
|
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(%w[save find])
|
245
277
|
end
|
246
278
|
|
247
|
-
it '
|
279
|
+
it 'parses model methods with module' do
|
248
280
|
content = <<-EOF
|
249
281
|
module Admin
|
250
282
|
module Blog
|
@@ -260,7 +292,7 @@ module RailsBestPractices
|
|
260
292
|
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(%w[save find])
|
261
293
|
end
|
262
294
|
|
263
|
-
it '
|
295
|
+
it 'does not add methods from module' do
|
264
296
|
content = <<-EOF
|
265
297
|
class Model < ActiveRecord::Base
|
266
298
|
end
|
@@ -279,7 +311,7 @@ module RailsBestPractices
|
|
279
311
|
end
|
280
312
|
|
281
313
|
context 'scope' do
|
282
|
-
it '
|
314
|
+
it 'treats named_scope as method' do
|
283
315
|
content = <<-EOF
|
284
316
|
class Post < ActiveRecord::Base
|
285
317
|
named_scope :active, conditions: {active: true}
|
@@ -290,7 +322,7 @@ module RailsBestPractices
|
|
290
322
|
expect(methods.get_methods('Post').map(&:method_name)).to eq(['active'])
|
291
323
|
end
|
292
324
|
|
293
|
-
it '
|
325
|
+
it 'treats scope as method' do
|
294
326
|
content = <<-EOF
|
295
327
|
class Post < ActiveRecord::Base
|
296
328
|
scope :active, where(active: true)
|
@@ -303,7 +335,7 @@ module RailsBestPractices
|
|
303
335
|
end
|
304
336
|
|
305
337
|
context 'alias' do
|
306
|
-
it '
|
338
|
+
it 'treats alias as method' do
|
307
339
|
content = <<-EOF
|
308
340
|
class Post < ActiveRecord::Base
|
309
341
|
alias :new :old
|
@@ -314,7 +346,7 @@ module RailsBestPractices
|
|
314
346
|
expect(methods.get_methods('Post').map(&:method_name)).to eq(['new'])
|
315
347
|
end
|
316
348
|
|
317
|
-
it '
|
349
|
+
it 'treats alias_method as method' do
|
318
350
|
content = <<-EOF
|
319
351
|
class Post < ActiveRecord::Base
|
320
352
|
alias_method :new, :old
|
@@ -325,7 +357,7 @@ module RailsBestPractices
|
|
325
357
|
expect(methods.get_methods('Post').map(&:method_name)).to eq(['new'])
|
326
358
|
end
|
327
359
|
|
328
|
-
it '
|
360
|
+
it 'treats alias_method_chain as method' do
|
329
361
|
content = <<-EOF
|
330
362
|
class Post < ActiveRecord::Base
|
331
363
|
alias_method_chain :method, :feature
|
@@ -338,7 +370,7 @@ module RailsBestPractices
|
|
338
370
|
end
|
339
371
|
|
340
372
|
context 'attributes' do
|
341
|
-
it '
|
373
|
+
it 'parses mongoid field' do
|
342
374
|
content = <<-EOF
|
343
375
|
class Post
|
344
376
|
include Mongoid::Document
|
@@ -358,7 +390,7 @@ module RailsBestPractices
|
|
358
390
|
expect(model_attributes.get_attribute_type('Post', 'active')).to eq('Boolean')
|
359
391
|
end
|
360
392
|
|
361
|
-
it '
|
393
|
+
it 'parses mongomapper field' do
|
362
394
|
content = <<-EOF
|
363
395
|
class Post
|
364
396
|
include MongoMapper::Document
|
@@ -382,13 +414,17 @@ module RailsBestPractices
|
|
382
414
|
end
|
383
415
|
|
384
416
|
context 'no error' do
|
385
|
-
it '
|
417
|
+
it 'raiseds for finder_sql option' do
|
386
418
|
content = <<-EOF
|
387
419
|
class EventSubscription < ActiveRecord::Base
|
388
420
|
has_many :event_notification_template, finder_sql: ?
|
389
421
|
end
|
390
422
|
EOF
|
391
|
-
content =
|
423
|
+
content =
|
424
|
+
content.sub(
|
425
|
+
'?',
|
426
|
+
'\'SELECT event_notification_templates.* from event_notification_templates where event_type_id=#{event_type_id} and delivery_method_id=#{delivery_method_id}\''
|
427
|
+
)
|
392
428
|
expect { runner.prepare('app/models/event_subscription.rb', content) }.not_to raise_error
|
393
429
|
end
|
394
430
|
end
|