rails_best_practices 1.19.0 → 1.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +2 -2
- data/Rakefile +8 -8
- data/bin/rails_best_practices +1 -1
- data/lib/rails_best_practices/analyzer.rb +45 -45
- data/lib/rails_best_practices/command.rb +40 -40
- data/lib/rails_best_practices/core/check.rb +15 -15
- data/lib/rails_best_practices/core/klasses.rb +1 -1
- data/lib/rails_best_practices/core/methods.rb +7 -7
- data/lib/rails_best_practices/core/model_associations.rb +3 -3
- data/lib/rails_best_practices/core/modules.rb +1 -1
- data/lib/rails_best_practices/core/routes.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +3 -3
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -4
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -2
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -2
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +8 -8
- data/lib/rails_best_practices/prepares/helper_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +4 -4
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +19 -19
- data/lib/rails_best_practices/prepares/route_prepare.rb +34 -34
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +2 -2
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +7 -7
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +1 -1
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +3 -3
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +3 -3
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +1 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +6 -6
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +2 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -3
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -2
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +2 -2
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +1 -1
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -2
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +4 -4
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +3 -3
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +9 -9
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -11
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +1 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +8 -7
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +2 -2
- data/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb +2 -2
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +22 -22
- data/lib/rails_best_practices/reviews/review.rb +2 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +3 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +4 -4
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_observer_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +2 -2
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +4 -4
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +24 -24
- data/spec/rails_best_practices/analyzer_spec.rb +43 -43
- data/spec/rails_best_practices/core/check_spec.rb +2 -2
- data/spec/rails_best_practices/core/checks_loader_spec.rb +4 -4
- data/spec/rails_best_practices/core/error_spec.rb +16 -16
- data/spec/rails_best_practices/core/except_methods_spec.rb +15 -15
- data/spec/rails_best_practices/core/gems_spec.rb +9 -9
- data/spec/rails_best_practices/core/klasses_spec.rb +17 -17
- data/spec/rails_best_practices/core/methods_spec.rb +28 -28
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -10
- data/spec/rails_best_practices/core/model_attributes_spec.rb +10 -10
- data/spec/rails_best_practices/core/modules_spec.rb +10 -10
- data/spec/rails_best_practices/core/routes_spec.rb +10 -10
- data/spec/rails_best_practices/core/runner_spec.rb +4 -4
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +7 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +26 -26
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +5 -5
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +108 -108
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +77 -77
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +27 -27
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +17 -17
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +16 -16
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +10 -10
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +11 -11
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +16 -16
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -33
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +106 -106
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -15
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +29 -29
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +23 -23
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -1
- metadata +7 -7
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe ConfigPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: ConfigPrepare.new) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'configs' do
|
9
|
+
it 'should parse configs' do
|
10
10
|
content =<<-EOF
|
11
11
|
module RailsBestPracticesCom
|
12
12
|
class Application < Rails::Application
|
@@ -16,7 +16,7 @@ module RailsBestPractices
|
|
16
16
|
EOF
|
17
17
|
runner.prepare('config/application.rb', content)
|
18
18
|
configs = Prepares.configs
|
19
|
-
expect(configs[
|
19
|
+
expect(configs['config.active_record.whitelist_attributes']).to eq('true')
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe ControllerPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: [ControllerPrepare.new, HelperPrepare.new]) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'methods' do
|
9
|
+
it 'should parse controller methods' do
|
10
10
|
content =<<-EOF
|
11
11
|
class PostsController < ApplicationController
|
12
12
|
def index; end
|
@@ -15,10 +15,10 @@ module RailsBestPractices
|
|
15
15
|
EOF
|
16
16
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
17
17
|
methods = Prepares.controller_methods
|
18
|
-
expect(methods.get_methods(
|
18
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show'])
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'should parse model methods with access control' do
|
22
22
|
content =<<-EOF
|
23
23
|
class PostsController < ApplicationController
|
24
24
|
def index; end
|
@@ -31,13 +31,13 @@ module RailsBestPractices
|
|
31
31
|
EOF
|
32
32
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
33
33
|
methods = Prepares.controller_methods
|
34
|
-
expect(methods.get_methods(
|
35
|
-
expect(methods.get_methods(
|
36
|
-
expect(methods.get_methods(
|
37
|
-
expect(methods.get_methods(
|
34
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show', 'resources', 'resource'])
|
35
|
+
expect(methods.get_methods('PostsController', 'public').map(&:method_name)).to eq(['index', 'show'])
|
36
|
+
expect(methods.get_methods('PostsController', 'protected').map(&:method_name)).to eq(['resources'])
|
37
|
+
expect(methods.get_methods('PostsController', 'private').map(&:method_name)).to eq(['resource'])
|
38
38
|
end
|
39
39
|
|
40
|
-
it
|
40
|
+
it 'should parse controller methods with module ::' do
|
41
41
|
content =<<-EOF
|
42
42
|
class Admin::Blog::PostsController < ApplicationController
|
43
43
|
def index; end
|
@@ -46,10 +46,10 @@ module RailsBestPractices
|
|
46
46
|
EOF
|
47
47
|
runner.prepare('app/controllers/admin/posts_controller.rb', content)
|
48
48
|
methods = Prepares.controller_methods
|
49
|
-
expect(methods.get_methods(
|
49
|
+
expect(methods.get_methods('Admin::Blog::PostsController').map(&:method_name)).to eq(['index', 'show'])
|
50
50
|
end
|
51
51
|
|
52
|
-
it
|
52
|
+
it 'should parse controller methods with module' do
|
53
53
|
content =<<-EOF
|
54
54
|
module Admin
|
55
55
|
module Blog
|
@@ -62,21 +62,21 @@ module RailsBestPractices
|
|
62
62
|
EOF
|
63
63
|
runner.prepare('app/controllers/admin/posts_controller.rb', content)
|
64
64
|
methods = Prepares.controller_methods
|
65
|
-
expect(methods.get_methods(
|
65
|
+
expect(methods.get_methods('Admin::Blog::PostsController').map(&:method_name)).to eq(['index', 'show'])
|
66
66
|
end
|
67
67
|
|
68
|
-
context
|
69
|
-
it
|
68
|
+
context 'inherited_resources' do
|
69
|
+
it 'extend inherited_resources' do
|
70
70
|
content =<<-EOF
|
71
71
|
class PostsController < InheritedResources::Base
|
72
72
|
end
|
73
73
|
EOF
|
74
74
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
75
75
|
methods = Prepares.controller_methods
|
76
|
-
expect(methods.get_methods(
|
76
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show', 'new', 'create', 'edit', 'update', 'destroy'])
|
77
77
|
end
|
78
78
|
|
79
|
-
it
|
79
|
+
it 'extend inherited_resources with actions' do
|
80
80
|
content =<<-EOF
|
81
81
|
class PostsController < InheritedResources::Base
|
82
82
|
actions :index, :show
|
@@ -84,10 +84,10 @@ module RailsBestPractices
|
|
84
84
|
EOF
|
85
85
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
86
86
|
methods = Prepares.controller_methods
|
87
|
-
expect(methods.get_methods(
|
87
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show'])
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
90
|
+
it 'extend inherited_resources with all actions' do
|
91
91
|
content =<<-EOF
|
92
92
|
class PostsController < InheritedResources::Base
|
93
93
|
actions :all, except: [:show]
|
@@ -95,10 +95,10 @@ module RailsBestPractices
|
|
95
95
|
EOF
|
96
96
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
97
97
|
methods = Prepares.controller_methods
|
98
|
-
expect(methods.get_methods(
|
98
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'new', 'create', 'edit', 'update', 'destroy'])
|
99
99
|
end
|
100
100
|
|
101
|
-
it
|
101
|
+
it 'extend inherited_resources with all actions with no arguments' do
|
102
102
|
content =<<-EOF
|
103
103
|
class PostsController < InheritedResources::Base
|
104
104
|
actions :all
|
@@ -106,10 +106,10 @@ module RailsBestPractices
|
|
106
106
|
EOF
|
107
107
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
108
108
|
methods = Prepares.controller_methods
|
109
|
-
expect(methods.get_methods(
|
109
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show', 'new', 'create', 'edit', 'update', 'destroy'])
|
110
110
|
end
|
111
111
|
|
112
|
-
it
|
112
|
+
it 'DSL inherit_resources' do
|
113
113
|
content =<<-EOF
|
114
114
|
class PostsController
|
115
115
|
inherit_resources
|
@@ -117,13 +117,13 @@ module RailsBestPractices
|
|
117
117
|
EOF
|
118
118
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
119
119
|
methods = Prepares.controller_methods
|
120
|
-
expect(methods.get_methods(
|
120
|
+
expect(methods.get_methods('PostsController').map(&:method_name)).to eq(['index', 'show', 'new', 'create', 'edit', 'update', 'destroy'])
|
121
121
|
end
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
-
context
|
126
|
-
it
|
125
|
+
context 'helpers' do
|
126
|
+
it 'should add helper descendant' do
|
127
127
|
content =<<-EOF
|
128
128
|
module PostsHelper
|
129
129
|
end
|
@@ -136,7 +136,7 @@ module RailsBestPractices
|
|
136
136
|
EOF
|
137
137
|
runner.prepare('app/controllers/posts_controller.rb', content)
|
138
138
|
helpers = Prepares.helpers
|
139
|
-
expect(helpers.first.descendants).to eq([
|
139
|
+
expect(helpers.first.descendants).to eq(['PostsController'])
|
140
140
|
end
|
141
141
|
end
|
142
142
|
end
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe GemfilePrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: GemfilePrepare.new) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'gemfile' do
|
9
|
+
it 'should parse gems' do
|
10
10
|
content =<<-EOF
|
11
11
|
GEM
|
12
12
|
remote: https://rubygems.org/
|
@@ -26,7 +26,7 @@ PLATFORMS
|
|
26
26
|
EOF
|
27
27
|
runner.prepare('Gemfile.lock', content)
|
28
28
|
gems = Prepares.gems
|
29
|
-
expect(gems.map(&:to_s)).to eq([
|
29
|
+
expect(gems.map(&:to_s)).to eq(['rails (3.2.13)', 'mysql2 (0.3.12b6)'])
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe HelperPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: HelperPrepare.new) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'methods' do
|
9
|
+
it 'should parse helper methods' do
|
10
10
|
content =<<-EOF
|
11
11
|
module PostsHelper
|
12
12
|
def used; end
|
@@ -15,10 +15,10 @@ module RailsBestPractices
|
|
15
15
|
EOF
|
16
16
|
runner.prepare('app/helpers/posts_helper.rb', content)
|
17
17
|
methods = Prepares.helper_methods
|
18
|
-
expect(methods.get_methods(
|
18
|
+
expect(methods.get_methods('PostsHelper').map(&:method_name)).to eq(['used', 'unused'])
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'should parse helpers' do
|
22
22
|
content =<<-EOF
|
23
23
|
module PostsHelper
|
24
24
|
end
|
@@ -35,9 +35,9 @@ module RailsBestPractices
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
EOF
|
38
|
-
runner.prepare(
|
38
|
+
runner.prepare('app/helpers/base_helper.rb', content)
|
39
39
|
helpers = Prepares.helpers
|
40
|
-
expect(helpers.map(&:to_s)).to eq([
|
40
|
+
expect(helpers.map(&:to_s)).to eq(['PostsHelper', 'Admin::UsersHelper', 'Admin', 'Admin::BaseHelper'])
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -5,8 +5,8 @@ module RailsBestPractices
|
|
5
5
|
describe InitializerPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: InitializerPrepare.new) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'initializers' do
|
9
|
+
it 'should set include_forbidden_attributes_protection config' do
|
10
10
|
content =<<-EOF
|
11
11
|
class AR
|
12
12
|
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
@@ -14,17 +14,17 @@ module RailsBestPractices
|
|
14
14
|
EOF
|
15
15
|
runner.prepare('config/initializers/ar.rb', content)
|
16
16
|
configs = Prepares.configs
|
17
|
-
expect(configs[
|
17
|
+
expect(configs['railsbp.include_forbidden_attributes_protection']).to eq('true')
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
20
|
+
it 'should not set include_forbidden_attributes_protection config' do
|
21
21
|
content =<<-EOF
|
22
22
|
class AR
|
23
23
|
end
|
24
24
|
EOF
|
25
25
|
runner.prepare('config/initializers/ar.rb', content)
|
26
26
|
configs = Prepares.configs
|
27
|
-
expect(configs[
|
27
|
+
expect(configs['railsbp.include_forbidden_attributes_protection']).to be_nil
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -5,13 +5,13 @@ module RailsBestPractices
|
|
5
5
|
describe MailerPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: MailerPrepare.new) }
|
7
7
|
|
8
|
-
it
|
8
|
+
it 'should parse mailer names' do
|
9
9
|
content =<<-EOF
|
10
10
|
class ProjectMailer < ActionMailer::Base
|
11
11
|
end
|
12
12
|
EOF
|
13
13
|
runner.prepare('app/mailers/project_mailer.rb', content)
|
14
|
-
expect(Prepares.mailers.map(&:to_s)).to eq([
|
14
|
+
expect(Prepares.mailers.map(&:to_s)).to eq(['ProjectMailer'])
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -5,32 +5,32 @@ module RailsBestPractices
|
|
5
5
|
describe ModelPrepare do
|
6
6
|
let(:runner) { Core::Runner.new(prepares: ModelPrepare.new) }
|
7
7
|
|
8
|
-
context
|
9
|
-
it
|
8
|
+
context 'models' do
|
9
|
+
it 'class_name with modules ::' do
|
10
10
|
content =<<-EOF
|
11
11
|
class Blog::Post < ActiveRecord::Base
|
12
12
|
end
|
13
13
|
EOF
|
14
|
-
runner.prepare(
|
14
|
+
runner.prepare('app/models/admin/post.rb', content)
|
15
15
|
models = Prepares.models
|
16
|
-
expect(models.map(&:to_s)).to eq([
|
16
|
+
expect(models.map(&:to_s)).to eq(['Blog::Post'])
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
19
|
+
it 'class_name with modules' do
|
20
20
|
content =<<-EOF
|
21
21
|
module Blog
|
22
22
|
class Post < ActiveRecord::Base
|
23
23
|
end
|
24
24
|
end
|
25
25
|
EOF
|
26
|
-
runner.prepare(
|
26
|
+
runner.prepare('app/models/admin/post.rb', content)
|
27
27
|
models = Prepares.models
|
28
|
-
expect(models.map(&:to_s)).to eq([
|
28
|
+
expect(models.map(&:to_s)).to eq(['Blog::Post'])
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
context
|
33
|
-
it
|
32
|
+
context 'associations' do
|
33
|
+
it 'should parse model associations' do
|
34
34
|
content =<<-EOF
|
35
35
|
class Project < ActiveRecord::Base
|
36
36
|
belongs_to :portfolio
|
@@ -41,177 +41,177 @@ module RailsBestPractices
|
|
41
41
|
EOF
|
42
42
|
runner.prepare('app/models/project.rb', content)
|
43
43
|
model_associations = Prepares.model_associations
|
44
|
-
expect(model_associations.get_association(
|
45
|
-
expect(model_associations.get_association(
|
46
|
-
expect(model_associations.get_association(
|
47
|
-
expect(model_associations.get_association(
|
44
|
+
expect(model_associations.get_association('Project', 'portfolio')).to eq({'meta' => 'belongs_to', 'class_name' => 'Portfolio'})
|
45
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq({'meta' => 'has_one', 'class_name' => 'ProjectManager'})
|
46
|
+
expect(model_associations.get_association('Project', 'milestones')).to eq({'meta' => 'has_many', 'class_name' => 'Milestone'})
|
47
|
+
expect(model_associations.get_association('Project', 'categories')).to eq({'meta' => 'has_and_belongs_to_many', 'class_name' => 'Category'})
|
48
48
|
end
|
49
49
|
|
50
|
-
context
|
51
|
-
it
|
50
|
+
context 'with class_name option' do
|
51
|
+
it 'should parse belongs_to' do
|
52
52
|
content =<<-EOF
|
53
53
|
class Post < ActiveRecord::Base
|
54
54
|
belongs_to :author, "class_name" => "Person"
|
55
55
|
end
|
56
56
|
EOF
|
57
|
-
runner.prepare(
|
57
|
+
runner.prepare('app/models/post.rb', content)
|
58
58
|
model_associations = Prepares.model_associations
|
59
|
-
expect(model_associations.get_association(
|
59
|
+
expect(model_associations.get_association('Post', 'author')).to eq({'meta' => 'belongs_to', 'class_name' => 'Person'})
|
60
60
|
end
|
61
61
|
|
62
|
-
it
|
62
|
+
it 'should parse has_one' do
|
63
63
|
content =<<-EOF
|
64
64
|
class Project < ActiveRecord::Base
|
65
65
|
has_one :project_manager, "class_name" => "Person"
|
66
66
|
end
|
67
67
|
EOF
|
68
|
-
runner.prepare(
|
68
|
+
runner.prepare('app/models/post.rb', content)
|
69
69
|
model_associations = Prepares.model_associations
|
70
|
-
expect(model_associations.get_association(
|
70
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq({'meta' => 'has_one', 'class_name' => 'Person'})
|
71
71
|
end
|
72
72
|
|
73
|
-
it
|
73
|
+
it 'should parse has_many' do
|
74
74
|
content =<<-EOF
|
75
75
|
class Project < ActiveRecord::Base
|
76
76
|
has_many :people, "class_name" => "Person"
|
77
77
|
end
|
78
78
|
EOF
|
79
|
-
runner.prepare(
|
79
|
+
runner.prepare('app/models/project.rb', content)
|
80
80
|
model_associations = Prepares.model_associations
|
81
|
-
expect(model_associations.get_association(
|
81
|
+
expect(model_associations.get_association('Project', 'people')).to eq({'meta' => 'has_many', 'class_name' => 'Person'})
|
82
82
|
end
|
83
83
|
|
84
|
-
it
|
84
|
+
it 'should parse has_and_belongs_to_many' do
|
85
85
|
content =<<-EOF
|
86
86
|
class Citizen < ActiveRecord::Base
|
87
87
|
has_and_belongs_to_many :nations, "class_name" => "Country"
|
88
88
|
end
|
89
89
|
EOF
|
90
|
-
runner.prepare(
|
90
|
+
runner.prepare('app/models/citizen.rb', content)
|
91
91
|
model_associations = Prepares.model_associations
|
92
|
-
expect(model_associations.get_association(
|
92
|
+
expect(model_associations.get_association('Citizen', 'nations')).to eq({'meta' => 'has_and_belongs_to_many', 'class_name' => 'Country'})
|
93
93
|
end
|
94
94
|
|
95
|
-
context
|
96
|
-
it
|
95
|
+
context 'namespace' do
|
96
|
+
it 'should parse with namespace' do
|
97
97
|
content =<<-EOF
|
98
98
|
class Community < ActiveRecord::Base
|
99
99
|
has_many :members
|
100
100
|
end
|
101
101
|
EOF
|
102
|
-
runner.prepare(
|
102
|
+
runner.prepare('app/models/community.rb', content)
|
103
103
|
content =<<-EOF
|
104
104
|
class Community::Member < ActiveRecord::Base
|
105
105
|
belongs_to :community
|
106
106
|
end
|
107
107
|
EOF
|
108
|
-
runner.prepare(
|
108
|
+
runner.prepare('app/models/community/member.rb', content)
|
109
109
|
runner.after_prepare
|
110
110
|
model_associations = Prepares.model_associations
|
111
|
-
expect(model_associations.get_association(
|
112
|
-
expect(model_associations.get_association(
|
111
|
+
expect(model_associations.get_association('Community', 'members')).to eq({'meta' => 'has_many', 'class_name' => 'Community::Member'})
|
112
|
+
expect(model_associations.get_association('Community::Member', 'community')).to eq({'meta' => 'belongs_to', 'class_name' => 'Community'})
|
113
113
|
end
|
114
114
|
|
115
|
-
it
|
115
|
+
it 'should parse without namespace' do
|
116
116
|
content =<<-EOF
|
117
117
|
class Community::Member::Rating < ActiveRecord::Base
|
118
118
|
belongs_to :member
|
119
119
|
end
|
120
120
|
EOF
|
121
|
-
runner.prepare(
|
121
|
+
runner.prepare('app/models/community/member/rating.rb', content)
|
122
122
|
content =<<-EOF
|
123
123
|
class Community::Member < ActiveRecord::Base
|
124
124
|
has_many :ratings
|
125
125
|
end
|
126
126
|
EOF
|
127
|
-
runner.prepare(
|
127
|
+
runner.prepare('app/models/community/member.rb', content)
|
128
128
|
runner.after_prepare
|
129
129
|
model_associations = Prepares.model_associations
|
130
|
-
expect(model_associations.get_association(
|
131
|
-
expect(model_associations.get_association(
|
130
|
+
expect(model_associations.get_association('Community::Member::Rating', 'member')).to eq({'meta' => 'belongs_to', 'class_name' => 'Community::Member'})
|
131
|
+
expect(model_associations.get_association('Community::Member', 'ratings')).to eq({'meta' => 'has_many', 'class_name' => 'Community::Member::Rating'})
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|
135
135
|
|
136
|
-
context
|
137
|
-
it
|
136
|
+
context 'mongoid embeds' do
|
137
|
+
it 'should parse embeds_many' do
|
138
138
|
content =<<-EOF
|
139
139
|
class Person
|
140
140
|
include Mongoid::Document
|
141
141
|
embeds_many :addresses
|
142
142
|
end
|
143
143
|
EOF
|
144
|
-
runner.prepare(
|
144
|
+
runner.prepare('app/models/person.rb', content)
|
145
145
|
model_associations = Prepares.model_associations
|
146
|
-
expect(model_associations.get_association(
|
146
|
+
expect(model_associations.get_association('Person', 'addresses')).to eq({'meta' => 'embeds_many', 'class_name' => 'Address'})
|
147
147
|
end
|
148
148
|
|
149
|
-
it
|
149
|
+
it 'should parse embeds_one' do
|
150
150
|
content =<<-EOF
|
151
151
|
class Lush
|
152
152
|
include Mongoid::Document
|
153
153
|
embeds_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
|
154
154
|
end
|
155
155
|
EOF
|
156
|
-
runner.prepare(
|
156
|
+
runner.prepare('app/models/lush.rb', content)
|
157
157
|
model_associations = Prepares.model_associations
|
158
|
-
expect(model_associations.get_association(
|
158
|
+
expect(model_associations.get_association('Lush', 'whiskey')).to eq({'meta' => 'embeds_one', 'class_name' => 'Drink'})
|
159
159
|
end
|
160
160
|
|
161
|
-
it
|
161
|
+
it 'should parse embedded_in' do
|
162
162
|
content =<<-EOF
|
163
163
|
class Drink
|
164
164
|
include Mongoid::Document
|
165
165
|
embedded_in :alcoholic, class_name: "Lush", inverse_of: :whiskey
|
166
166
|
end
|
167
167
|
EOF
|
168
|
-
runner.prepare(
|
168
|
+
runner.prepare('app/models/drink.rb', content)
|
169
169
|
model_associations = Prepares.model_associations
|
170
|
-
expect(model_associations.get_association(
|
170
|
+
expect(model_associations.get_association('Drink', 'alcoholic')).to eq({'meta' => 'embedded_in', 'class_name' => 'Lush'})
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
context
|
175
|
-
it
|
174
|
+
context 'mongomapper many/one' do
|
175
|
+
it 'should parse one' do
|
176
176
|
content =<<-EOF
|
177
177
|
class Employee
|
178
178
|
include MongoMapper::Document
|
179
179
|
one :desk
|
180
180
|
end
|
181
181
|
EOF
|
182
|
-
runner.prepare(
|
182
|
+
runner.prepare('app/models/employee.rb', content)
|
183
183
|
model_associations = Prepares.model_associations
|
184
|
-
expect(model_associations.get_association(
|
184
|
+
expect(model_associations.get_association('Employee', 'desk')).to eq({'meta' => 'one', 'class_name' => 'Desk'})
|
185
185
|
end
|
186
186
|
|
187
|
-
it
|
187
|
+
it 'should parse many' do
|
188
188
|
content =<<-EOF
|
189
189
|
class Tree
|
190
190
|
include MongoMapper::Document
|
191
191
|
many :birds
|
192
192
|
end
|
193
193
|
EOF
|
194
|
-
runner.prepare(
|
194
|
+
runner.prepare('app/models/tree.rb', content)
|
195
195
|
model_associations = Prepares.model_associations
|
196
|
-
expect(model_associations.get_association(
|
196
|
+
expect(model_associations.get_association('Tree', 'birds')).to eq({'meta' => 'many', 'class_name' => 'Bird'})
|
197
197
|
end
|
198
198
|
end
|
199
199
|
end
|
200
200
|
|
201
|
-
context
|
202
|
-
it
|
201
|
+
context 'methods' do
|
202
|
+
it 'should parse model methods' do
|
203
203
|
content =<<-EOF
|
204
204
|
class Post < ActiveRecord::Base
|
205
205
|
def save; end
|
206
206
|
def find; end
|
207
207
|
end
|
208
208
|
EOF
|
209
|
-
runner.prepare(
|
209
|
+
runner.prepare('app/models/post.rb', content)
|
210
210
|
methods = Prepares.model_methods
|
211
|
-
expect(methods.get_methods(
|
211
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['save', 'find'])
|
212
212
|
end
|
213
213
|
|
214
|
-
it
|
214
|
+
it 'should parse model methods with access control' do
|
215
215
|
content =<<-EOF
|
216
216
|
class Post < ActiveRecord::Base
|
217
217
|
def save; end
|
@@ -222,27 +222,27 @@ module RailsBestPractices
|
|
222
222
|
def find_by_sql; end
|
223
223
|
end
|
224
224
|
EOF
|
225
|
-
runner.prepare(
|
225
|
+
runner.prepare('app/models/post.rb', content)
|
226
226
|
methods = Prepares.model_methods
|
227
|
-
expect(methods.get_methods(
|
228
|
-
expect(methods.get_methods(
|
229
|
-
expect(methods.get_methods(
|
230
|
-
expect(methods.get_methods(
|
227
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['save', 'find', 'create_or_update', 'find_by_sql'])
|
228
|
+
expect(methods.get_methods('Post', 'public').map(&:method_name)).to eq(['save', 'find'])
|
229
|
+
expect(methods.get_methods('Post', 'protected').map(&:method_name)).to eq(['create_or_update'])
|
230
|
+
expect(methods.get_methods('Post', 'private').map(&:method_name)).to eq(['find_by_sql'])
|
231
231
|
end
|
232
232
|
|
233
|
-
it
|
233
|
+
it 'should parse model methods with module ::' do
|
234
234
|
content =<<-EOF
|
235
235
|
class Admin::Blog::Post < ActiveRecord::Base
|
236
236
|
def save; end
|
237
237
|
def find; end
|
238
238
|
end
|
239
239
|
EOF
|
240
|
-
runner.prepare(
|
240
|
+
runner.prepare('app/models/admin/blog/post.rb', content)
|
241
241
|
methods = Prepares.model_methods
|
242
|
-
expect(methods.get_methods(
|
242
|
+
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(['save', 'find'])
|
243
243
|
end
|
244
244
|
|
245
|
-
it
|
245
|
+
it 'should parse model methods with module' do
|
246
246
|
content =<<-EOF
|
247
247
|
module Admin
|
248
248
|
module Blog
|
@@ -253,90 +253,90 @@ module RailsBestPractices
|
|
253
253
|
end
|
254
254
|
end
|
255
255
|
EOF
|
256
|
-
runner.prepare(
|
256
|
+
runner.prepare('app/models/admin/blog/post.rb', content)
|
257
257
|
methods = Prepares.model_methods
|
258
|
-
expect(methods.get_methods(
|
258
|
+
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(['save', 'find'])
|
259
259
|
end
|
260
260
|
|
261
|
-
it
|
261
|
+
it 'should not add methods from module' do
|
262
262
|
content =<<-EOF
|
263
263
|
class Model < ActiveRecord::Base
|
264
264
|
end
|
265
265
|
EOF
|
266
|
-
runner.prepare(
|
266
|
+
runner.prepare('app/models/model.rb', content)
|
267
267
|
content =<<-EOF
|
268
268
|
module Mixin
|
269
269
|
def mixed_method
|
270
270
|
end
|
271
271
|
end
|
272
272
|
EOF
|
273
|
-
runner.prepare(
|
273
|
+
runner.prepare('app/models/mixins/mixin.rb', content)
|
274
274
|
methods = Prepares.model_methods
|
275
275
|
expect(methods.get_methods('Model')).to be_empty
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
context
|
280
|
-
it
|
279
|
+
context 'scope' do
|
280
|
+
it 'should treat named_scope as method' do
|
281
281
|
content =<<-EOF
|
282
282
|
class Post < ActiveRecord::Base
|
283
283
|
named_scope :active, conditions: {active: true}
|
284
284
|
end
|
285
285
|
EOF
|
286
|
-
runner.prepare(
|
286
|
+
runner.prepare('app/models/post.rb', content)
|
287
287
|
methods = Prepares.model_methods
|
288
|
-
expect(methods.get_methods(
|
288
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['active'])
|
289
289
|
end
|
290
290
|
|
291
|
-
it
|
291
|
+
it 'should treat scope as method' do
|
292
292
|
content =<<-EOF
|
293
293
|
class Post < ActiveRecord::Base
|
294
294
|
scope :active, where(active: true)
|
295
295
|
end
|
296
296
|
EOF
|
297
|
-
runner.prepare(
|
297
|
+
runner.prepare('app/models/post.rb', content)
|
298
298
|
methods = Prepares.model_methods
|
299
|
-
expect(methods.get_methods(
|
299
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['active'])
|
300
300
|
end
|
301
301
|
end
|
302
302
|
|
303
|
-
context
|
304
|
-
it
|
303
|
+
context 'alias' do
|
304
|
+
it 'should treat alias as method' do
|
305
305
|
content =<<-EOF
|
306
306
|
class Post < ActiveRecord::Base
|
307
307
|
alias :new :old
|
308
308
|
end
|
309
309
|
EOF
|
310
|
-
runner.prepare(
|
310
|
+
runner.prepare('app/models/post.rb', content)
|
311
311
|
methods = Prepares.model_methods
|
312
|
-
expect(methods.get_methods(
|
312
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['new'])
|
313
313
|
end
|
314
314
|
|
315
|
-
it
|
315
|
+
it 'should treat alias_method as method' do
|
316
316
|
content =<<-EOF
|
317
317
|
class Post < ActiveRecord::Base
|
318
318
|
alias_method :new, :old
|
319
319
|
end
|
320
320
|
EOF
|
321
|
-
runner.prepare(
|
321
|
+
runner.prepare('app/models/post.rb', content)
|
322
322
|
methods = Prepares.model_methods
|
323
|
-
expect(methods.get_methods(
|
323
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['new'])
|
324
324
|
end
|
325
325
|
|
326
|
-
it
|
326
|
+
it 'should treat alias_method_chain as method' do
|
327
327
|
content =<<-EOF
|
328
328
|
class Post < ActiveRecord::Base
|
329
329
|
alias_method_chain :method, :feature
|
330
330
|
end
|
331
331
|
EOF
|
332
|
-
runner.prepare(
|
332
|
+
runner.prepare('app/models/post.rb', content)
|
333
333
|
methods = Prepares.model_methods
|
334
|
-
expect(methods.get_methods(
|
334
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(['method_with_feature', 'method'])
|
335
335
|
end
|
336
336
|
end
|
337
337
|
|
338
|
-
context
|
339
|
-
it
|
338
|
+
context 'attributes' do
|
339
|
+
it 'should parse mongoid field' do
|
340
340
|
content =<<-EOF
|
341
341
|
class Post
|
342
342
|
include Mongoid::Document
|
@@ -347,16 +347,16 @@ module RailsBestPractices
|
|
347
347
|
field :active, type: Boolean
|
348
348
|
end
|
349
349
|
EOF
|
350
|
-
runner.prepare(
|
350
|
+
runner.prepare('app/models/post.rb', content)
|
351
351
|
model_attributes = Prepares.model_attributes
|
352
|
-
expect(model_attributes.get_attribute_type(
|
353
|
-
expect(model_attributes.get_attribute_type(
|
354
|
-
expect(model_attributes.get_attribute_type(
|
355
|
-
expect(model_attributes.get_attribute_type(
|
356
|
-
expect(model_attributes.get_attribute_type(
|
352
|
+
expect(model_attributes.get_attribute_type('Post', 'title')).to eq('String')
|
353
|
+
expect(model_attributes.get_attribute_type('Post', 'tags')).to eq('Array')
|
354
|
+
expect(model_attributes.get_attribute_type('Post', 'comments_count')).to eq('Integer')
|
355
|
+
expect(model_attributes.get_attribute_type('Post', 'deleted_at')).to eq('DateTime')
|
356
|
+
expect(model_attributes.get_attribute_type('Post', 'active')).to eq('Boolean')
|
357
357
|
end
|
358
358
|
|
359
|
-
it
|
359
|
+
it 'should parse mongomapper field' do
|
360
360
|
content =<<-EOF
|
361
361
|
class Post
|
362
362
|
include MongoMapper::Document
|
@@ -368,19 +368,19 @@ module RailsBestPractices
|
|
368
368
|
key :fav_colors, Array
|
369
369
|
end
|
370
370
|
EOF
|
371
|
-
runner.prepare(
|
371
|
+
runner.prepare('app/models/post.rb', content)
|
372
372
|
model_attributes = Prepares.model_attributes
|
373
|
-
expect(model_attributes.get_attribute_type(
|
374
|
-
expect(model_attributes.get_attribute_type(
|
375
|
-
expect(model_attributes.get_attribute_type(
|
376
|
-
expect(model_attributes.get_attribute_type(
|
377
|
-
expect(model_attributes.get_attribute_type(
|
378
|
-
expect(model_attributes.get_attribute_type(
|
373
|
+
expect(model_attributes.get_attribute_type('Post', 'first_name')).to eq('String')
|
374
|
+
expect(model_attributes.get_attribute_type('Post', 'last_name')).to eq('String')
|
375
|
+
expect(model_attributes.get_attribute_type('Post', 'age')).to eq('Integer')
|
376
|
+
expect(model_attributes.get_attribute_type('Post', 'born_at')).to eq('Time')
|
377
|
+
expect(model_attributes.get_attribute_type('Post', 'active')).to eq('Boolean')
|
378
|
+
expect(model_attributes.get_attribute_type('Post', 'fav_colors')).to eq('Array')
|
379
379
|
end
|
380
380
|
end
|
381
381
|
|
382
|
-
context
|
383
|
-
it
|
382
|
+
context 'no error' do
|
383
|
+
it 'should raised for finder_sql option' do
|
384
384
|
content =<<-EOF
|
385
385
|
class EventSubscription < ActiveRecord::Base
|
386
386
|
has_many :event_notification_template, finder_sql: ?
|