rails_best_practices 1.19.1 → 1.19.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Guardfile +1 -1
- data/README.md +1 -0
- data/Rakefile +2 -2
- data/bin/rails_best_practices +2 -1
- data/lib/rails_best_practices.rb +2 -1
- data/lib/rails_best_practices/analyzer.rb +13 -12
- data/lib/rails_best_practices/colorize.rb +2 -0
- data/lib/rails_best_practices/command.rb +6 -5
- data/lib/rails_best_practices/core.rb +2 -1
- data/lib/rails_best_practices/core/check.rb +19 -17
- data/lib/rails_best_practices/core/checks_loader.rb +3 -2
- data/lib/rails_best_practices/core/configs.rb +2 -1
- data/lib/rails_best_practices/core/controllers.rb +4 -1
- data/lib/rails_best_practices/core/error.rb +3 -2
- data/lib/rails_best_practices/core/gems.rb +4 -3
- data/lib/rails_best_practices/core/helpers.rb +4 -1
- data/lib/rails_best_practices/core/klasses.rb +3 -2
- data/lib/rails_best_practices/core/mailers.rb +2 -1
- data/lib/rails_best_practices/core/methods.rb +9 -10
- data/lib/rails_best_practices/core/model_associations.rb +6 -5
- data/lib/rails_best_practices/core/model_attributes.rb +2 -1
- data/lib/rails_best_practices/core/models.rb +2 -1
- data/lib/rails_best_practices/core/modules.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +2 -1
- data/lib/rails_best_practices/core/runner.rb +7 -6
- data/lib/rails_best_practices/core_ext/erubis.rb +4 -5
- data/lib/rails_best_practices/lexicals.rb +2 -1
- data/lib/rails_best_practices/lexicals/long_line_check.rb +2 -1
- data/lib/rails_best_practices/lexicals/remove_tab_check.rb +2 -1
- data/lib/rails_best_practices/lexicals/remove_trailing_whitespace_check.rb +2 -1
- data/lib/rails_best_practices/prepares.rb +2 -1
- data/lib/rails_best_practices/prepares/config_prepare.rb +3 -2
- data/lib/rails_best_practices/prepares/controller_prepare.rb +9 -8
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/helper_prepare.rb +4 -3
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +3 -2
- data/lib/rails_best_practices/prepares/model_prepare.rb +12 -12
- data/lib/rails_best_practices/prepares/route_prepare.rb +8 -7
- data/lib/rails_best_practices/prepares/schema_prepare.rb +3 -2
- data/lib/rails_best_practices/reviews.rb +2 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +7 -4
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +8 -7
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +4 -3
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +5 -4
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +2 -1
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +2 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +2 -1
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +3 -2
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +2 -1
- data/lib/rails_best_practices/reviews/move_code_into_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +3 -2
- data/lib/rails_best_practices/reviews/move_model_logic_into_model_review.rb +3 -2
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +5 -4
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +2 -1
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +2 -1
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +2 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +3 -2
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +4 -3
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +2 -1
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +11 -10
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +4 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +6 -5
- 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 -1
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +8 -8
- data/lib/rails_best_practices/reviews/review.rb +4 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -1
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -2
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +5 -4
- 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 +5 -5
- data/lib/rails_best_practices/reviews/use_observer_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +4 -3
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +4 -3
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
- data/lib/rails_best_practices/version.rb +3 -2
- data/rails_best_practices.gemspec +11 -10
- data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +3 -2
- data/spec/rails_best_practices/analyzer_spec.rb +6 -8
- data/spec/rails_best_practices/core/check_spec.rb +2 -0
- data/spec/rails_best_practices/core/checks_loader_spec.rb +2 -0
- data/spec/rails_best_practices/core/configs_spec.rb +2 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +2 -0
- data/spec/rails_best_practices/core/error_spec.rb +2 -0
- data/spec/rails_best_practices/core/except_methods_spec.rb +2 -0
- data/spec/rails_best_practices/core/gems_spec.rb +2 -0
- data/spec/rails_best_practices/core/helpers_spec.rb +2 -0
- data/spec/rails_best_practices/core/klasses_spec.rb +2 -0
- data/spec/rails_best_practices/core/mailers_spec.rb +2 -0
- data/spec/rails_best_practices/core/methods_spec.rb +6 -4
- data/spec/rails_best_practices/core/model_associations_spec.rb +4 -2
- data/spec/rails_best_practices/core/model_attributes_spec.rb +2 -0
- data/spec/rails_best_practices/core/models_spec.rb +2 -0
- data/spec/rails_best_practices/core/modules_spec.rb +2 -0
- data/spec/rails_best_practices/core/routes_spec.rb +4 -2
- data/spec/rails_best_practices/core/runner_spec.rb +2 -0
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +3 -1
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +8 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +8 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +9 -7
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +23 -21
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +7 -5
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +4 -2
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +56 -54
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +39 -37
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +12 -10
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +20 -18
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +11 -9
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +21 -19
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -4
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +33 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +13 -11
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +65 -63
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +24 -22
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -11
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +7 -5
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -1
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +2 -0
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- metadata +26 -26
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module RailsBestPractices
|
@@ -7,7 +9,7 @@ module RailsBestPractices
|
|
7
9
|
|
8
10
|
context 'gemfile' do
|
9
11
|
it 'should parse gems' do
|
10
|
-
content
|
12
|
+
content = <<-EOF
|
11
13
|
GEM
|
12
14
|
remote: https://rubygems.org/
|
13
15
|
specs:
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module RailsBestPractices
|
@@ -7,7 +9,7 @@ module RailsBestPractices
|
|
7
9
|
|
8
10
|
context 'methods' do
|
9
11
|
it 'should parse helper methods' do
|
10
|
-
content
|
12
|
+
content = <<-EOF
|
11
13
|
module PostsHelper
|
12
14
|
def used; end
|
13
15
|
def unused; end
|
@@ -15,21 +17,21 @@ module RailsBestPractices
|
|
15
17
|
EOF
|
16
18
|
runner.prepare('app/helpers/posts_helper.rb', content)
|
17
19
|
methods = Prepares.helper_methods
|
18
|
-
expect(methods.get_methods('PostsHelper').map(&:method_name)).to eq([
|
20
|
+
expect(methods.get_methods('PostsHelper').map(&:method_name)).to eq(%w[used unused])
|
19
21
|
end
|
20
22
|
|
21
23
|
it 'should parse helpers' do
|
22
|
-
content
|
24
|
+
content = <<-EOF
|
23
25
|
module PostsHelper
|
24
26
|
end
|
25
27
|
EOF
|
26
28
|
runner.prepare('app/helpers/posts_helper.rb', content)
|
27
|
-
content
|
29
|
+
content = <<-EOF
|
28
30
|
module Admin::UsersHelper
|
29
31
|
end
|
30
32
|
EOF
|
31
33
|
runner.prepare('app/helpers/users_helper.rb', content)
|
32
|
-
content
|
34
|
+
content = <<-EOF
|
33
35
|
module Admin
|
34
36
|
module BaseHelper
|
35
37
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module RailsBestPractices
|
@@ -7,7 +9,7 @@ module RailsBestPractices
|
|
7
9
|
|
8
10
|
context 'initializers' do
|
9
11
|
it 'should set include_forbidden_attributes_protection config' do
|
10
|
-
content
|
12
|
+
content = <<-EOF
|
11
13
|
class AR
|
12
14
|
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
|
13
15
|
end
|
@@ -18,7 +20,7 @@ module RailsBestPractices
|
|
18
20
|
end
|
19
21
|
|
20
22
|
it 'should not set include_forbidden_attributes_protection config' do
|
21
|
-
content
|
23
|
+
content = <<-EOF
|
22
24
|
class AR
|
23
25
|
end
|
24
26
|
EOF
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module RailsBestPractices
|
@@ -6,7 +8,7 @@ module RailsBestPractices
|
|
6
8
|
let(:runner) { Core::Runner.new(prepares: MailerPrepare.new) }
|
7
9
|
|
8
10
|
it 'should parse mailer names' do
|
9
|
-
content
|
11
|
+
content = <<-EOF
|
10
12
|
class ProjectMailer < ActionMailer::Base
|
11
13
|
end
|
12
14
|
EOF
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module RailsBestPractices
|
@@ -7,7 +9,7 @@ module RailsBestPractices
|
|
7
9
|
|
8
10
|
context 'models' do
|
9
11
|
it 'class_name with modules ::' do
|
10
|
-
content
|
12
|
+
content = <<-EOF
|
11
13
|
class Blog::Post < ActiveRecord::Base
|
12
14
|
end
|
13
15
|
EOF
|
@@ -17,7 +19,7 @@ module RailsBestPractices
|
|
17
19
|
end
|
18
20
|
|
19
21
|
it 'class_name with modules' do
|
20
|
-
content
|
22
|
+
content = <<-EOF
|
21
23
|
module Blog
|
22
24
|
class Post < ActiveRecord::Base
|
23
25
|
end
|
@@ -31,7 +33,7 @@ module RailsBestPractices
|
|
31
33
|
|
32
34
|
context 'associations' do
|
33
35
|
it 'should parse model associations' do
|
34
|
-
content
|
36
|
+
content = <<-EOF
|
35
37
|
class Project < ActiveRecord::Base
|
36
38
|
belongs_to :portfolio
|
37
39
|
has_one :project_manager
|
@@ -41,66 +43,66 @@ module RailsBestPractices
|
|
41
43
|
EOF
|
42
44
|
runner.prepare('app/models/project.rb', content)
|
43
45
|
model_associations = Prepares.model_associations
|
44
|
-
expect(model_associations.get_association('Project', 'portfolio')).to eq(
|
45
|
-
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
46
|
-
expect(model_associations.get_association('Project', 'milestones')).to eq(
|
47
|
-
expect(model_associations.get_association('Project', 'categories')).to eq(
|
46
|
+
expect(model_associations.get_association('Project', 'portfolio')).to eq('meta' => 'belongs_to', 'class_name' => 'Portfolio')
|
47
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq('meta' => 'has_one', 'class_name' => 'ProjectManager')
|
48
|
+
expect(model_associations.get_association('Project', 'milestones')).to eq('meta' => 'has_many', 'class_name' => 'Milestone')
|
49
|
+
expect(model_associations.get_association('Project', 'categories')).to eq('meta' => 'has_and_belongs_to_many', 'class_name' => 'Category')
|
48
50
|
end
|
49
51
|
|
50
52
|
context 'with class_name option' do
|
51
53
|
it 'should parse belongs_to' do
|
52
|
-
content
|
54
|
+
content = <<-EOF
|
53
55
|
class Post < ActiveRecord::Base
|
54
56
|
belongs_to :author, "class_name" => "Person"
|
55
57
|
end
|
56
58
|
EOF
|
57
59
|
runner.prepare('app/models/post.rb', content)
|
58
60
|
model_associations = Prepares.model_associations
|
59
|
-
expect(model_associations.get_association('Post', 'author')).to eq(
|
61
|
+
expect(model_associations.get_association('Post', 'author')).to eq('meta' => 'belongs_to', 'class_name' => 'Person')
|
60
62
|
end
|
61
63
|
|
62
64
|
it 'should parse has_one' do
|
63
|
-
content
|
65
|
+
content = <<-EOF
|
64
66
|
class Project < ActiveRecord::Base
|
65
67
|
has_one :project_manager, "class_name" => "Person"
|
66
68
|
end
|
67
69
|
EOF
|
68
70
|
runner.prepare('app/models/post.rb', content)
|
69
71
|
model_associations = Prepares.model_associations
|
70
|
-
expect(model_associations.get_association('Project', 'project_manager')).to eq(
|
72
|
+
expect(model_associations.get_association('Project', 'project_manager')).to eq('meta' => 'has_one', 'class_name' => 'Person')
|
71
73
|
end
|
72
74
|
|
73
75
|
it 'should parse has_many' do
|
74
|
-
content
|
76
|
+
content = <<-EOF
|
75
77
|
class Project < ActiveRecord::Base
|
76
78
|
has_many :people, "class_name" => "Person"
|
77
79
|
end
|
78
80
|
EOF
|
79
81
|
runner.prepare('app/models/project.rb', content)
|
80
82
|
model_associations = Prepares.model_associations
|
81
|
-
expect(model_associations.get_association('Project', 'people')).to eq(
|
83
|
+
expect(model_associations.get_association('Project', 'people')).to eq('meta' => 'has_many', 'class_name' => 'Person')
|
82
84
|
end
|
83
85
|
|
84
86
|
it 'should parse has_and_belongs_to_many' do
|
85
|
-
content
|
87
|
+
content = <<-EOF
|
86
88
|
class Citizen < ActiveRecord::Base
|
87
89
|
has_and_belongs_to_many :nations, "class_name" => "Country"
|
88
90
|
end
|
89
91
|
EOF
|
90
92
|
runner.prepare('app/models/citizen.rb', content)
|
91
93
|
model_associations = Prepares.model_associations
|
92
|
-
expect(model_associations.get_association('Citizen', 'nations')).to eq(
|
94
|
+
expect(model_associations.get_association('Citizen', 'nations')).to eq('meta' => 'has_and_belongs_to_many', 'class_name' => 'Country')
|
93
95
|
end
|
94
96
|
|
95
97
|
context 'namespace' do
|
96
98
|
it 'should parse with namespace' do
|
97
|
-
content
|
99
|
+
content = <<-EOF
|
98
100
|
class Community < ActiveRecord::Base
|
99
101
|
has_many :members
|
100
102
|
end
|
101
103
|
EOF
|
102
104
|
runner.prepare('app/models/community.rb', content)
|
103
|
-
content
|
105
|
+
content = <<-EOF
|
104
106
|
class Community::Member < ActiveRecord::Base
|
105
107
|
belongs_to :community
|
106
108
|
end
|
@@ -108,18 +110,18 @@ module RailsBestPractices
|
|
108
110
|
runner.prepare('app/models/community/member.rb', content)
|
109
111
|
runner.after_prepare
|
110
112
|
model_associations = Prepares.model_associations
|
111
|
-
expect(model_associations.get_association('Community', 'members')).to eq(
|
112
|
-
expect(model_associations.get_association('Community::Member', 'community')).to eq(
|
113
|
+
expect(model_associations.get_association('Community', 'members')).to eq('meta' => 'has_many', 'class_name' => 'Community::Member')
|
114
|
+
expect(model_associations.get_association('Community::Member', 'community')).to eq('meta' => 'belongs_to', 'class_name' => 'Community')
|
113
115
|
end
|
114
116
|
|
115
117
|
it 'should parse without namespace' do
|
116
|
-
content
|
118
|
+
content = <<-EOF
|
117
119
|
class Community::Member::Rating < ActiveRecord::Base
|
118
120
|
belongs_to :member
|
119
121
|
end
|
120
122
|
EOF
|
121
123
|
runner.prepare('app/models/community/member/rating.rb', content)
|
122
|
-
content
|
124
|
+
content = <<-EOF
|
123
125
|
class Community::Member < ActiveRecord::Base
|
124
126
|
has_many :ratings
|
125
127
|
end
|
@@ -127,15 +129,15 @@ module RailsBestPractices
|
|
127
129
|
runner.prepare('app/models/community/member.rb', content)
|
128
130
|
runner.after_prepare
|
129
131
|
model_associations = Prepares.model_associations
|
130
|
-
expect(model_associations.get_association('Community::Member::Rating', 'member')).to eq(
|
131
|
-
expect(model_associations.get_association('Community::Member', 'ratings')).to eq(
|
132
|
+
expect(model_associations.get_association('Community::Member::Rating', 'member')).to eq('meta' => 'belongs_to', 'class_name' => 'Community::Member')
|
133
|
+
expect(model_associations.get_association('Community::Member', 'ratings')).to eq('meta' => 'has_many', 'class_name' => 'Community::Member::Rating')
|
132
134
|
end
|
133
135
|
end
|
134
136
|
end
|
135
137
|
|
136
138
|
context 'mongoid embeds' do
|
137
139
|
it 'should parse embeds_many' do
|
138
|
-
content
|
140
|
+
content = <<-EOF
|
139
141
|
class Person
|
140
142
|
include Mongoid::Document
|
141
143
|
embeds_many :addresses
|
@@ -143,11 +145,11 @@ module RailsBestPractices
|
|
143
145
|
EOF
|
144
146
|
runner.prepare('app/models/person.rb', content)
|
145
147
|
model_associations = Prepares.model_associations
|
146
|
-
expect(model_associations.get_association('Person', 'addresses')).to eq(
|
148
|
+
expect(model_associations.get_association('Person', 'addresses')).to eq('meta' => 'embeds_many', 'class_name' => 'Address')
|
147
149
|
end
|
148
150
|
|
149
151
|
it 'should parse embeds_one' do
|
150
|
-
content
|
152
|
+
content = <<-EOF
|
151
153
|
class Lush
|
152
154
|
include Mongoid::Document
|
153
155
|
embeds_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
|
@@ -155,11 +157,11 @@ module RailsBestPractices
|
|
155
157
|
EOF
|
156
158
|
runner.prepare('app/models/lush.rb', content)
|
157
159
|
model_associations = Prepares.model_associations
|
158
|
-
expect(model_associations.get_association('Lush', 'whiskey')).to eq(
|
160
|
+
expect(model_associations.get_association('Lush', 'whiskey')).to eq('meta' => 'embeds_one', 'class_name' => 'Drink')
|
159
161
|
end
|
160
162
|
|
161
163
|
it 'should parse embedded_in' do
|
162
|
-
content
|
164
|
+
content = <<-EOF
|
163
165
|
class Drink
|
164
166
|
include Mongoid::Document
|
165
167
|
embedded_in :alcoholic, class_name: "Lush", inverse_of: :whiskey
|
@@ -167,13 +169,13 @@ module RailsBestPractices
|
|
167
169
|
EOF
|
168
170
|
runner.prepare('app/models/drink.rb', content)
|
169
171
|
model_associations = Prepares.model_associations
|
170
|
-
expect(model_associations.get_association('Drink', 'alcoholic')).to eq(
|
172
|
+
expect(model_associations.get_association('Drink', 'alcoholic')).to eq('meta' => 'embedded_in', 'class_name' => 'Lush')
|
171
173
|
end
|
172
174
|
end
|
173
175
|
|
174
176
|
context 'mongomapper many/one' do
|
175
177
|
it 'should parse one' do
|
176
|
-
content
|
178
|
+
content = <<-EOF
|
177
179
|
class Employee
|
178
180
|
include MongoMapper::Document
|
179
181
|
one :desk
|
@@ -181,11 +183,11 @@ module RailsBestPractices
|
|
181
183
|
EOF
|
182
184
|
runner.prepare('app/models/employee.rb', content)
|
183
185
|
model_associations = Prepares.model_associations
|
184
|
-
expect(model_associations.get_association('Employee', 'desk')).to eq(
|
186
|
+
expect(model_associations.get_association('Employee', 'desk')).to eq('meta' => 'one', 'class_name' => 'Desk')
|
185
187
|
end
|
186
188
|
|
187
189
|
it 'should parse many' do
|
188
|
-
content
|
190
|
+
content = <<-EOF
|
189
191
|
class Tree
|
190
192
|
include MongoMapper::Document
|
191
193
|
many :birds
|
@@ -193,14 +195,14 @@ module RailsBestPractices
|
|
193
195
|
EOF
|
194
196
|
runner.prepare('app/models/tree.rb', content)
|
195
197
|
model_associations = Prepares.model_associations
|
196
|
-
expect(model_associations.get_association('Tree', 'birds')).to eq(
|
198
|
+
expect(model_associations.get_association('Tree', 'birds')).to eq('meta' => 'many', 'class_name' => 'Bird')
|
197
199
|
end
|
198
200
|
end
|
199
201
|
end
|
200
202
|
|
201
203
|
context 'methods' do
|
202
204
|
it 'should parse model methods' do
|
203
|
-
content
|
205
|
+
content = <<-EOF
|
204
206
|
class Post < ActiveRecord::Base
|
205
207
|
def save; end
|
206
208
|
def find; end
|
@@ -208,11 +210,11 @@ module RailsBestPractices
|
|
208
210
|
EOF
|
209
211
|
runner.prepare('app/models/post.rb', content)
|
210
212
|
methods = Prepares.model_methods
|
211
|
-
expect(methods.get_methods('Post').map(&:method_name)).to eq([
|
213
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(%w[save find])
|
212
214
|
end
|
213
215
|
|
214
216
|
it 'should parse model methods with access control' do
|
215
|
-
content
|
217
|
+
content = <<-EOF
|
216
218
|
class Post < ActiveRecord::Base
|
217
219
|
def save; end
|
218
220
|
def find; end
|
@@ -224,14 +226,14 @@ module RailsBestPractices
|
|
224
226
|
EOF
|
225
227
|
runner.prepare('app/models/post.rb', content)
|
226
228
|
methods = Prepares.model_methods
|
227
|
-
expect(methods.get_methods('Post').map(&:method_name)).to eq([
|
228
|
-
expect(methods.get_methods('Post', 'public').map(&:method_name)).to eq([
|
229
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(%w[save find create_or_update find_by_sql])
|
230
|
+
expect(methods.get_methods('Post', 'public').map(&:method_name)).to eq(%w[save find])
|
229
231
|
expect(methods.get_methods('Post', 'protected').map(&:method_name)).to eq(['create_or_update'])
|
230
232
|
expect(methods.get_methods('Post', 'private').map(&:method_name)).to eq(['find_by_sql'])
|
231
233
|
end
|
232
234
|
|
233
235
|
it 'should parse model methods with module ::' do
|
234
|
-
content
|
236
|
+
content = <<-EOF
|
235
237
|
class Admin::Blog::Post < ActiveRecord::Base
|
236
238
|
def save; end
|
237
239
|
def find; end
|
@@ -239,11 +241,11 @@ module RailsBestPractices
|
|
239
241
|
EOF
|
240
242
|
runner.prepare('app/models/admin/blog/post.rb', content)
|
241
243
|
methods = Prepares.model_methods
|
242
|
-
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq([
|
244
|
+
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(%w[save find])
|
243
245
|
end
|
244
246
|
|
245
247
|
it 'should parse model methods with module' do
|
246
|
-
content
|
248
|
+
content = <<-EOF
|
247
249
|
module Admin
|
248
250
|
module Blog
|
249
251
|
class Post < ActiveRecord::Base
|
@@ -255,16 +257,16 @@ module RailsBestPractices
|
|
255
257
|
EOF
|
256
258
|
runner.prepare('app/models/admin/blog/post.rb', content)
|
257
259
|
methods = Prepares.model_methods
|
258
|
-
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq([
|
260
|
+
expect(methods.get_methods('Admin::Blog::Post').map(&:method_name)).to eq(%w[save find])
|
259
261
|
end
|
260
262
|
|
261
263
|
it 'should not add methods from module' do
|
262
|
-
content
|
264
|
+
content = <<-EOF
|
263
265
|
class Model < ActiveRecord::Base
|
264
266
|
end
|
265
267
|
EOF
|
266
268
|
runner.prepare('app/models/model.rb', content)
|
267
|
-
content
|
269
|
+
content = <<-EOF
|
268
270
|
module Mixin
|
269
271
|
def mixed_method
|
270
272
|
end
|
@@ -278,7 +280,7 @@ module RailsBestPractices
|
|
278
280
|
|
279
281
|
context 'scope' do
|
280
282
|
it 'should treat named_scope as method' do
|
281
|
-
content
|
283
|
+
content = <<-EOF
|
282
284
|
class Post < ActiveRecord::Base
|
283
285
|
named_scope :active, conditions: {active: true}
|
284
286
|
end
|
@@ -289,7 +291,7 @@ module RailsBestPractices
|
|
289
291
|
end
|
290
292
|
|
291
293
|
it 'should treat scope as method' do
|
292
|
-
content
|
294
|
+
content = <<-EOF
|
293
295
|
class Post < ActiveRecord::Base
|
294
296
|
scope :active, where(active: true)
|
295
297
|
end
|
@@ -302,7 +304,7 @@ module RailsBestPractices
|
|
302
304
|
|
303
305
|
context 'alias' do
|
304
306
|
it 'should treat alias as method' do
|
305
|
-
content
|
307
|
+
content = <<-EOF
|
306
308
|
class Post < ActiveRecord::Base
|
307
309
|
alias :new :old
|
308
310
|
end
|
@@ -313,7 +315,7 @@ module RailsBestPractices
|
|
313
315
|
end
|
314
316
|
|
315
317
|
it 'should treat alias_method as method' do
|
316
|
-
content
|
318
|
+
content = <<-EOF
|
317
319
|
class Post < ActiveRecord::Base
|
318
320
|
alias_method :new, :old
|
319
321
|
end
|
@@ -324,20 +326,20 @@ module RailsBestPractices
|
|
324
326
|
end
|
325
327
|
|
326
328
|
it 'should treat alias_method_chain as method' do
|
327
|
-
content
|
329
|
+
content = <<-EOF
|
328
330
|
class Post < ActiveRecord::Base
|
329
331
|
alias_method_chain :method, :feature
|
330
332
|
end
|
331
333
|
EOF
|
332
334
|
runner.prepare('app/models/post.rb', content)
|
333
335
|
methods = Prepares.model_methods
|
334
|
-
expect(methods.get_methods('Post').map(&:method_name)).to eq([
|
336
|
+
expect(methods.get_methods('Post').map(&:method_name)).to eq(%w[method_with_feature method])
|
335
337
|
end
|
336
338
|
end
|
337
339
|
|
338
340
|
context 'attributes' do
|
339
341
|
it 'should parse mongoid field' do
|
340
|
-
content
|
342
|
+
content = <<-EOF
|
341
343
|
class Post
|
342
344
|
include Mongoid::Document
|
343
345
|
field :title
|
@@ -357,7 +359,7 @@ module RailsBestPractices
|
|
357
359
|
end
|
358
360
|
|
359
361
|
it 'should parse mongomapper field' do
|
360
|
-
content
|
362
|
+
content = <<-EOF
|
361
363
|
class Post
|
362
364
|
include MongoMapper::Document
|
363
365
|
key :first_name, String
|
@@ -381,12 +383,12 @@ module RailsBestPractices
|
|
381
383
|
|
382
384
|
context 'no error' do
|
383
385
|
it 'should raised for finder_sql option' do
|
384
|
-
content
|
386
|
+
content = <<-EOF
|
385
387
|
class EventSubscription < ActiveRecord::Base
|
386
388
|
has_many :event_notification_template, finder_sql: ?
|
387
389
|
end
|
388
390
|
EOF
|
389
|
-
content.sub
|
391
|
+
content = content.sub('?', '\'SELECT event_notification_templates.* from event_notification_templates where event_type_id=#{event_type_id} and delivery_method_id=#{delivery_method_id}\'')
|
390
392
|
expect { runner.prepare('app/models/event_subscription.rb', content) }.not_to raise_error
|
391
393
|
end
|
392
394
|
end
|