rails_best_practices 1.9.0 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -1
- data/Gemfile +0 -3
- data/Gemfile.lock +10 -9
- data/README.md +2 -0
- data/lib/rails_best_practices/analyzer.rb +40 -21
- data/lib/rails_best_practices/core.rb +1 -0
- data/lib/rails_best_practices/core/check.rb +12 -0
- data/lib/rails_best_practices/core/configs.rb +7 -0
- data/lib/rails_best_practices/core/error.rb +2 -1
- data/lib/rails_best_practices/core/routes.rb +11 -5
- data/lib/rails_best_practices/core/runner.rb +6 -2
- data/lib/rails_best_practices/core_ext/sexp.rb +3 -1
- data/lib/rails_best_practices/prepares.rb +5 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +21 -0
- data/lib/rails_best_practices/prepares/route_prepare.rb +24 -7
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +11 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +7 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +1 -0
- data/spec/rails_best_practices/analyzer_spec.rb +52 -32
- data/spec/rails_best_practices/core/check_spec.rb +64 -45
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +65 -64
- data/spec/rails_best_practices/core/configs_spec.rb +7 -0
- data/spec/rails_best_practices/core/controllers_spec.rb +4 -2
- data/spec/rails_best_practices/core/error_spec.rb +28 -22
- data/spec/rails_best_practices/core/helpers_spec.rb +4 -2
- data/spec/rails_best_practices/core/klasses_spec.rb +8 -6
- data/spec/rails_best_practices/core/mailers_spec.rb +4 -2
- data/spec/rails_best_practices/core/methods_spec.rb +37 -35
- data/spec/rails_best_practices/core/model_associations_spec.rb +17 -15
- data/spec/rails_best_practices/core/model_attributes_spec.rb +17 -15
- data/spec/rails_best_practices/core/models_spec.rb +4 -2
- data/spec/rails_best_practices/core/modules_spec.rb +21 -19
- data/spec/rails_best_practices/core/nil_spec.rb +23 -21
- data/spec/rails_best_practices/core/routes_spec.rb +21 -14
- data/spec/rails_best_practices/core/runner_spec.rb +17 -15
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +32 -7
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +35 -31
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +35 -31
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +24 -0
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +122 -123
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +36 -32
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +12 -8
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +326 -322
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +619 -573
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +25 -21
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +85 -87
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +224 -222
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +30 -26
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +79 -75
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +138 -140
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +38 -34
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +22 -18
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +62 -63
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +51 -50
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +140 -136
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +48 -44
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +42 -38
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +151 -149
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +86 -55
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +23 -19
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +290 -286
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +76 -72
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +601 -597
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +55 -54
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +34 -30
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +288 -289
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +59 -55
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +73 -69
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +89 -98
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +52 -52
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +192 -190
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +114 -121
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +189 -190
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +97 -93
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +146 -151
- data/spec/spec_helper.rb +11 -20
- metadata +42 -26
- data/.watchr.example +0 -65
@@ -1,41 +1,45 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe HelperPrepare do
|
6
|
+
let(:runner) { Core::Runner.new(:prepares => HelperPrepare.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
context "methods" do
|
9
|
+
it "should parse helper methods" do
|
10
|
+
content =<<-EOF
|
11
|
+
module PostsHelper
|
12
|
+
def used; end
|
13
|
+
def unused; end
|
14
|
+
end
|
15
|
+
EOF
|
16
|
+
runner.prepare('app/helpers/posts_helper.rb', content)
|
17
|
+
methods = Prepares.helper_methods
|
18
|
+
methods.get_methods("PostsHelper").map(&:method_name).should == ["used", "unused"]
|
19
|
+
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
21
|
+
it "should parse helpers" do
|
22
|
+
content =<<-EOF
|
23
|
+
module PostsHelper
|
24
|
+
end
|
25
|
+
EOF
|
26
|
+
runner.prepare('app/helpers/posts_helper.rb', content)
|
27
|
+
content =<<-EOF
|
28
|
+
module Admin::UsersHelper
|
29
|
+
end
|
30
|
+
EOF
|
31
|
+
runner.prepare('app/helpers/users_helper.rb', content)
|
32
|
+
content =<<-EOF
|
33
|
+
module Admin
|
34
|
+
module BaseHelper
|
35
|
+
end
|
36
|
+
end
|
37
|
+
EOF
|
38
|
+
runner.prepare("app/helpers/base_helper.rb", content)
|
39
|
+
helpers = Prepares.helpers
|
40
|
+
helpers.map(&:to_s).should == ["PostsHelper", "Admin::UsersHelper", "Admin", "Admin::BaseHelper"]
|
33
41
|
end
|
34
42
|
end
|
35
|
-
EOF
|
36
|
-
runner.prepare("app/helpers/base_helper.rb", content)
|
37
|
-
helpers = RailsBestPractices::Prepares.helpers
|
38
|
-
helpers.map(&:to_s).should == ["PostsHelper", "Admin::UsersHelper", "Admin", "Admin::BaseHelper"]
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -1,14 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe MailerPrepare do
|
6
|
+
let(:runner) { Core::Runner.new(:prepares => MailerPrepare.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
it "should parse mailer names" do
|
9
|
+
content =<<-EOF
|
10
|
+
class ProjectMailer < ActionMailer::Base
|
11
|
+
end
|
12
|
+
EOF
|
13
|
+
runner.prepare('app/mailers/project_mailer.rb', content)
|
14
|
+
Prepares.mailers.map(&:to_s).should == ["ProjectMailer"]
|
15
|
+
end
|
9
16
|
end
|
10
|
-
EOF
|
11
|
-
runner.prepare('app/mailers/project_mailer.rb', content)
|
12
|
-
RailsBestPractices::Prepares.mailers.map(&:to_s).should == ["ProjectMailer"]
|
13
17
|
end
|
14
18
|
end
|
@@ -1,378 +1,382 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Prepares
|
5
|
+
describe ModelPrepare do
|
6
|
+
let(:runner) { Core::Runner.new(:prepares => ModelPrepare.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
context "models" do
|
11
|
-
it "class_name with modules ::" do
|
12
|
-
content =<<-EOF
|
13
|
-
class Blog::Post < ActiveRecord::Base
|
8
|
+
before :each do
|
9
|
+
runner.whiny = true
|
14
10
|
end
|
15
|
-
EOF
|
16
|
-
runner.prepare("app/models/admin/post.rb", content)
|
17
|
-
models = RailsBestPractices::Prepares.models
|
18
|
-
models.map(&:to_s).should == ["Blog::Post"]
|
19
|
-
end
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
12
|
+
context "models" do
|
13
|
+
it "class_name with modules ::" do
|
14
|
+
content =<<-EOF
|
15
|
+
class Blog::Post < ActiveRecord::Base
|
16
|
+
end
|
17
|
+
EOF
|
18
|
+
runner.prepare("app/models/admin/post.rb", content)
|
19
|
+
models = Prepares.models
|
20
|
+
models.map(&:to_s).should == ["Blog::Post"]
|
25
21
|
end
|
26
|
-
end
|
27
|
-
EOF
|
28
|
-
runner.prepare("app/models/admin/post.rb", content)
|
29
|
-
models = RailsBestPractices::Prepares.models
|
30
|
-
models.map(&:to_s).should == ["Blog::Post"]
|
31
|
-
end
|
32
|
-
end
|
33
22
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
23
|
+
it "class_name with modules" do
|
24
|
+
content =<<-EOF
|
25
|
+
module Blog
|
26
|
+
class Post < ActiveRecord::Base
|
27
|
+
end
|
28
|
+
end
|
29
|
+
EOF
|
30
|
+
runner.prepare("app/models/admin/post.rb", content)
|
31
|
+
models = Prepares.models
|
32
|
+
models.map(&:to_s).should == ["Blog::Post"]
|
33
|
+
end
|
42
34
|
end
|
43
|
-
EOF
|
44
|
-
runner.prepare('app/models/project.rb', content)
|
45
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
46
|
-
model_associations.get_association("Project", "portfolio").should == {"meta" => "belongs_to", "class_name" => "Portfolio"}
|
47
|
-
model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "ProjectManager"}
|
48
|
-
model_associations.get_association("Project", "milestones").should == {"meta" => "has_many", "class_name" => "Milestone"}
|
49
|
-
model_associations.get_association("Project", "categories").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Category"}
|
50
|
-
end
|
51
35
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
36
|
+
context "associations" do
|
37
|
+
it "should parse model associations" do
|
38
|
+
content =<<-EOF
|
39
|
+
class Project < ActiveRecord::Base
|
40
|
+
belongs_to :portfolio
|
41
|
+
has_one :project_manager
|
42
|
+
has_many :milestones
|
43
|
+
has_and_belongs_to_many :categories
|
44
|
+
end
|
45
|
+
EOF
|
46
|
+
runner.prepare('app/models/project.rb', content)
|
47
|
+
model_associations = Prepares.model_associations
|
48
|
+
model_associations.get_association("Project", "portfolio").should == {"meta" => "belongs_to", "class_name" => "Portfolio"}
|
49
|
+
model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "ProjectManager"}
|
50
|
+
model_associations.get_association("Project", "milestones").should == {"meta" => "has_many", "class_name" => "Milestone"}
|
51
|
+
model_associations.get_association("Project", "categories").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Category"}
|
57
52
|
end
|
58
|
-
EOF
|
59
|
-
runner.prepare("app/models/post.rb", content)
|
60
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
61
|
-
model_associations.get_association("Post", "author").should == {"meta" => "belongs_to", "class_name" => "Person"}
|
62
|
-
end
|
63
53
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
54
|
+
context "with class_name option" do
|
55
|
+
it "should parse belongs_to" do
|
56
|
+
content =<<-EOF
|
57
|
+
class Post < ActiveRecord::Base
|
58
|
+
belongs_to :author, "class_name" => "Person"
|
59
|
+
end
|
60
|
+
EOF
|
61
|
+
runner.prepare("app/models/post.rb", content)
|
62
|
+
model_associations = Prepares.model_associations
|
63
|
+
model_associations.get_association("Post", "author").should == {"meta" => "belongs_to", "class_name" => "Person"}
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should parse has_one" do
|
67
|
+
content =<<-EOF
|
68
|
+
class Project < ActiveRecord::Base
|
69
|
+
has_one :project_manager, "class_name" => "Person"
|
70
|
+
end
|
71
|
+
EOF
|
72
|
+
runner.prepare("app/models/post.rb", content)
|
73
|
+
model_associations = Prepares.model_associations
|
74
|
+
model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "Person"}
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should parse has_many" do
|
78
|
+
content =<<-EOF
|
79
|
+
class Project < ActiveRecord::Base
|
80
|
+
has_many :people, "class_name" => "Person"
|
81
|
+
end
|
82
|
+
EOF
|
83
|
+
runner.prepare("app/models/project.rb", content)
|
84
|
+
model_associations = Prepares.model_associations
|
85
|
+
model_associations.get_association("Project", "people").should == {"meta" => "has_many", "class_name" => "Person"}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should parse has_and_belongs_to_many" do
|
89
|
+
content =<<-EOF
|
90
|
+
class Citizen < ActiveRecord::Base
|
91
|
+
has_and_belongs_to_many :nations, "class_name" => "Country"
|
92
|
+
end
|
93
|
+
EOF
|
94
|
+
runner.prepare("app/models/citizen.rb", content)
|
95
|
+
model_associations = Prepares.model_associations
|
96
|
+
model_associations.get_association("Citizen", "nations").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Country"}
|
97
|
+
end
|
98
|
+
|
99
|
+
context "namespace" do
|
100
|
+
it "should parse with namespace" do
|
101
|
+
content =<<-EOF
|
102
|
+
class Community < ActiveRecord::Base
|
103
|
+
has_many :members
|
104
|
+
end
|
105
|
+
EOF
|
106
|
+
runner.prepare("app/models/community.rb", content)
|
107
|
+
content =<<-EOF
|
108
|
+
class Community::Member < ActiveRecord::Base
|
109
|
+
belongs_to :community
|
110
|
+
end
|
111
|
+
EOF
|
112
|
+
runner.prepare("app/models/community/member.rb", content)
|
113
|
+
runner.after_prepare
|
114
|
+
model_associations = Prepares.model_associations
|
115
|
+
model_associations.get_association("Community", "members").should == {"meta" => "has_many", "class_name" => "Community::Member"}
|
116
|
+
model_associations.get_association("Community::Member", "community").should == {"meta" => "belongs_to", "class_name" => "Community"}
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should parse without namespace" do
|
120
|
+
content =<<-EOF
|
121
|
+
class Community::Member::Rating < ActiveRecord::Base
|
122
|
+
belongs_to :member
|
123
|
+
end
|
124
|
+
EOF
|
125
|
+
runner.prepare("app/models/community/member/rating.rb", content)
|
126
|
+
content =<<-EOF
|
127
|
+
class Community::Member < ActiveRecord::Base
|
128
|
+
has_many :ratings
|
129
|
+
end
|
130
|
+
EOF
|
131
|
+
runner.prepare("app/models/community/member.rb", content)
|
132
|
+
runner.after_prepare
|
133
|
+
model_associations = Prepares.model_associations
|
134
|
+
model_associations.get_association("Community::Member::Rating", "member").should == {"meta" => "belongs_to", "class_name" => "Community::Member"}
|
135
|
+
model_associations.get_association("Community::Member", "ratings").should == {"meta" => "has_many", "class_name" => "Community::Member::Rating"}
|
136
|
+
end
|
137
|
+
end
|
68
138
|
end
|
69
|
-
EOF
|
70
|
-
runner.prepare("app/models/post.rb", content)
|
71
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
72
|
-
model_associations.get_association("Project", "project_manager").should == {"meta" => "has_one", "class_name" => "Person"}
|
73
|
-
end
|
74
139
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
140
|
+
context "mongoid embeds" do
|
141
|
+
it "should parse embeds_many" do
|
142
|
+
content =<<-EOF
|
143
|
+
class Person
|
144
|
+
include Mongoid::Document
|
145
|
+
embeds_many :addresses
|
146
|
+
end
|
147
|
+
EOF
|
148
|
+
runner.prepare("app/models/person.rb", content)
|
149
|
+
model_associations = Prepares.model_associations
|
150
|
+
model_associations.get_association("Person", "addresses").should == {"meta" => "embeds_many", "class_name" => "Address"}
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should parse embeds_one" do
|
154
|
+
content =<<-EOF
|
155
|
+
class Lush
|
156
|
+
include Mongoid::Document
|
157
|
+
embeds_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
|
158
|
+
end
|
159
|
+
EOF
|
160
|
+
runner.prepare("app/models/lush.rb", content)
|
161
|
+
model_associations = Prepares.model_associations
|
162
|
+
model_associations.get_association("Lush", "whiskey").should == {"meta" => "embeds_one", "class_name" => "Drink"}
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should parse embedded_in" do
|
166
|
+
content =<<-EOF
|
167
|
+
class Drink
|
168
|
+
include Mongoid::Document
|
169
|
+
embedded_in :alcoholic, class_name: "Lush", inverse_of: :whiskey
|
170
|
+
end
|
171
|
+
EOF
|
172
|
+
runner.prepare("app/models/drink.rb", content)
|
173
|
+
model_associations = Prepares.model_associations
|
174
|
+
model_associations.get_association("Drink", "alcoholic").should == {"meta" => "embedded_in", "class_name" => "Lush"}
|
175
|
+
end
|
79
176
|
end
|
80
|
-
EOF
|
81
|
-
runner.prepare("app/models/project.rb", content)
|
82
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
83
|
-
model_associations.get_association("Project", "people").should == {"meta" => "has_many", "class_name" => "Person"}
|
84
|
-
end
|
85
177
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
178
|
+
context "mongomapper many/one" do
|
179
|
+
it "should parse one" do
|
180
|
+
content =<<-EOF
|
181
|
+
class Employee
|
182
|
+
include MongoMapper::Document
|
183
|
+
one :desk
|
184
|
+
end
|
185
|
+
EOF
|
186
|
+
runner.prepare("app/models/employee.rb", content)
|
187
|
+
model_associations = Prepares.model_associations
|
188
|
+
model_associations.get_association("Employee", "desk").should == {"meta" => "one", "class_name" => "Desk"}
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should parse many" do
|
192
|
+
content =<<-EOF
|
193
|
+
class Tree
|
194
|
+
include MongoMapper::Document
|
195
|
+
many :birds
|
196
|
+
end
|
197
|
+
EOF
|
198
|
+
runner.prepare("app/models/tree.rb", content)
|
199
|
+
model_associations = Prepares.model_associations
|
200
|
+
model_associations.get_association("Tree", "birds").should == {"meta" => "many", "class_name" => "Bird"}
|
201
|
+
end
|
90
202
|
end
|
91
|
-
EOF
|
92
|
-
runner.prepare("app/models/citizen.rb", content)
|
93
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
94
|
-
model_associations.get_association("Citizen", "nations").should == {"meta" => "has_and_belongs_to_many", "class_name" => "Country"}
|
95
203
|
end
|
96
204
|
|
97
|
-
context "
|
98
|
-
it "should parse
|
205
|
+
context "methods" do
|
206
|
+
it "should parse model methods" do
|
99
207
|
content =<<-EOF
|
100
|
-
class
|
101
|
-
|
208
|
+
class Post < ActiveRecord::Base
|
209
|
+
def save; end
|
210
|
+
def find; end
|
102
211
|
end
|
103
212
|
EOF
|
104
|
-
runner.prepare("app/models/
|
213
|
+
runner.prepare("app/models/post.rb", content)
|
214
|
+
methods = Prepares.model_methods
|
215
|
+
methods.get_methods("Post").map(&:method_name).should == ["save", "find"]
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should parse model methods with access control" do
|
105
219
|
content =<<-EOF
|
106
|
-
class
|
107
|
-
|
220
|
+
class Post < ActiveRecord::Base
|
221
|
+
def save; end
|
222
|
+
def find; end
|
223
|
+
protected
|
224
|
+
def create_or_update; end
|
225
|
+
private
|
226
|
+
def find_by_sql; end
|
108
227
|
end
|
109
228
|
EOF
|
110
|
-
runner.prepare("app/models/
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
229
|
+
runner.prepare("app/models/post.rb", content)
|
230
|
+
methods = Prepares.model_methods
|
231
|
+
methods.get_methods("Post").map(&:method_name).should == ["save", "find", "create_or_update", "find_by_sql"]
|
232
|
+
methods.get_methods("Post", "public").map(&:method_name).should == ["save", "find"]
|
233
|
+
methods.get_methods("Post", "protected").map(&:method_name).should == ["create_or_update"]
|
234
|
+
methods.get_methods("Post", "private").map(&:method_name).should == ["find_by_sql"]
|
115
235
|
end
|
116
236
|
|
117
|
-
it "should parse
|
237
|
+
it "should parse model methods with module ::" do
|
118
238
|
content =<<-EOF
|
119
|
-
class
|
120
|
-
|
239
|
+
class Admin::Blog::Post < ActiveRecord::Base
|
240
|
+
def save; end
|
241
|
+
def find; end
|
121
242
|
end
|
122
243
|
EOF
|
123
|
-
runner.prepare("app/models/
|
244
|
+
runner.prepare("app/models/admin/blog/post.rb", content)
|
245
|
+
methods = Prepares.model_methods
|
246
|
+
methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should parse model methods with module" do
|
124
250
|
content =<<-EOF
|
125
|
-
|
126
|
-
|
251
|
+
module Admin
|
252
|
+
module Blog
|
253
|
+
class Post < ActiveRecord::Base
|
254
|
+
def save; end
|
255
|
+
def find; end
|
256
|
+
end
|
257
|
+
end
|
127
258
|
end
|
128
259
|
EOF
|
129
|
-
runner.prepare("app/models/
|
130
|
-
|
131
|
-
|
132
|
-
model_associations.get_association("Community::Member::Rating", "member").should == {"meta" => "belongs_to", "class_name" => "Community::Member"}
|
133
|
-
model_associations.get_association("Community::Member", "ratings").should == {"meta" => "has_many", "class_name" => "Community::Member::Rating"}
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
context "mongoid embeds" do
|
139
|
-
it "should parse embeds_many" do
|
140
|
-
content =<<-EOF
|
141
|
-
class Person
|
142
|
-
include Mongoid::Document
|
143
|
-
embeds_many :addresses
|
260
|
+
runner.prepare("app/models/admin/blog/post.rb", content)
|
261
|
+
methods = Prepares.model_methods
|
262
|
+
methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
|
144
263
|
end
|
145
|
-
EOF
|
146
|
-
runner.prepare("app/models/person.rb", content)
|
147
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
148
|
-
model_associations.get_association("Person", "addresses").should == {"meta" => "embeds_many", "class_name" => "Address"}
|
149
264
|
end
|
150
265
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
266
|
+
context "scope" do
|
267
|
+
it "should treat named_scope as method" do
|
268
|
+
content =<<-EOF
|
269
|
+
class Post < ActiveRecord::Base
|
270
|
+
named_scope :active, :conditions => {:active => true}
|
271
|
+
end
|
272
|
+
EOF
|
273
|
+
runner.prepare("app/models/post.rb", content)
|
274
|
+
methods = Prepares.model_methods
|
275
|
+
methods.get_methods("Post").map(&:method_name).should == ["active"]
|
156
276
|
end
|
157
|
-
EOF
|
158
|
-
runner.prepare("app/models/lush.rb", content)
|
159
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
160
|
-
model_associations.get_association("Lush", "whiskey").should == {"meta" => "embeds_one", "class_name" => "Drink"}
|
161
|
-
end
|
162
277
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
278
|
+
it "should treat scope as method" do
|
279
|
+
content =<<-EOF
|
280
|
+
class Post < ActiveRecord::Base
|
281
|
+
scope :active, where(:active => true)
|
282
|
+
end
|
283
|
+
EOF
|
284
|
+
runner.prepare("app/models/post.rb", content)
|
285
|
+
methods = Prepares.model_methods
|
286
|
+
methods.get_methods("Post").map(&:method_name).should == ["active"]
|
168
287
|
end
|
169
|
-
EOF
|
170
|
-
runner.prepare("app/models/drink.rb", content)
|
171
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
172
|
-
model_associations.get_association("Drink", "alcoholic").should == {"meta" => "embedded_in", "class_name" => "Lush"}
|
173
288
|
end
|
174
|
-
end
|
175
289
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
290
|
+
context "alias" do
|
291
|
+
it "should treat alias as method" do
|
292
|
+
content =<<-EOF
|
293
|
+
class Post < ActiveRecord::Base
|
294
|
+
alias :new :old
|
295
|
+
end
|
296
|
+
EOF
|
297
|
+
runner.prepare("app/models/post.rb", content)
|
298
|
+
methods = Prepares.model_methods
|
299
|
+
methods.get_methods("Post").map(&:method_name).should == ["new"]
|
182
300
|
end
|
183
|
-
EOF
|
184
|
-
runner.prepare("app/models/employee.rb", content)
|
185
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
186
|
-
model_associations.get_association("Employee", "desk").should == {"meta" => "one", "class_name" => "Desk"}
|
187
|
-
end
|
188
301
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
302
|
+
it "should treat alias_method as method" do
|
303
|
+
content =<<-EOF
|
304
|
+
class Post < ActiveRecord::Base
|
305
|
+
alias_method :new, :old
|
306
|
+
end
|
307
|
+
EOF
|
308
|
+
runner.prepare("app/models/post.rb", content)
|
309
|
+
methods = Prepares.model_methods
|
310
|
+
methods.get_methods("Post").map(&:method_name).should == ["new"]
|
194
311
|
end
|
195
|
-
EOF
|
196
|
-
runner.prepare("app/models/tree.rb", content)
|
197
|
-
model_associations = RailsBestPractices::Prepares.model_associations
|
198
|
-
model_associations.get_association("Tree", "birds").should == {"meta" => "many", "class_name" => "Bird"}
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
312
|
|
203
|
-
|
204
|
-
|
205
|
-
content =<<-EOF
|
206
|
-
class Post < ActiveRecord::Base
|
207
|
-
def save; end
|
208
|
-
def find; end
|
209
|
-
end
|
210
|
-
EOF
|
211
|
-
runner.prepare("app/models/post.rb", content)
|
212
|
-
methods = RailsBestPractices::Prepares.model_methods
|
213
|
-
methods.get_methods("Post").map(&:method_name).should == ["save", "find"]
|
214
|
-
end
|
215
|
-
|
216
|
-
it "should parse model methods with access control" do
|
217
|
-
content =<<-EOF
|
218
|
-
class Post < ActiveRecord::Base
|
219
|
-
def save; end
|
220
|
-
def find; end
|
221
|
-
protected
|
222
|
-
def create_or_update; end
|
223
|
-
private
|
224
|
-
def find_by_sql; end
|
225
|
-
end
|
226
|
-
EOF
|
227
|
-
runner.prepare("app/models/post.rb", content)
|
228
|
-
methods = RailsBestPractices::Prepares.model_methods
|
229
|
-
methods.get_methods("Post").map(&:method_name).should == ["save", "find", "create_or_update", "find_by_sql"]
|
230
|
-
methods.get_methods("Post", "public").map(&:method_name).should == ["save", "find"]
|
231
|
-
methods.get_methods("Post", "protected").map(&:method_name).should == ["create_or_update"]
|
232
|
-
methods.get_methods("Post", "private").map(&:method_name).should == ["find_by_sql"]
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should parse model methods with module ::" do
|
236
|
-
content =<<-EOF
|
237
|
-
class Admin::Blog::Post < ActiveRecord::Base
|
238
|
-
def save; end
|
239
|
-
def find; end
|
240
|
-
end
|
241
|
-
EOF
|
242
|
-
runner.prepare("app/models/admin/blog/post.rb", content)
|
243
|
-
methods = RailsBestPractices::Prepares.model_methods
|
244
|
-
methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
|
245
|
-
end
|
246
|
-
|
247
|
-
it "should parse model methods with module" do
|
248
|
-
content =<<-EOF
|
249
|
-
module Admin
|
250
|
-
module Blog
|
313
|
+
it "should treat alias_method_chain as method" do
|
314
|
+
content =<<-EOF
|
251
315
|
class Post < ActiveRecord::Base
|
252
|
-
|
253
|
-
def find; end
|
316
|
+
alias_method_chain :method, :feature
|
254
317
|
end
|
318
|
+
EOF
|
319
|
+
runner.prepare("app/models/post.rb", content)
|
320
|
+
methods = Prepares.model_methods
|
321
|
+
methods.get_methods("Post").map(&:method_name).should == ["method_with_feature", "method"]
|
255
322
|
end
|
256
323
|
end
|
257
|
-
EOF
|
258
|
-
runner.prepare("app/models/admin/blog/post.rb", content)
|
259
|
-
methods = RailsBestPractices::Prepares.model_methods
|
260
|
-
methods.get_methods("Admin::Blog::Post").map(&:method_name).should == ["save", "find"]
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
context "scope" do
|
265
|
-
it "should treat named_scope as method" do
|
266
|
-
content =<<-EOF
|
267
|
-
class Post < ActiveRecord::Base
|
268
|
-
named_scope :active, :conditions => {:active => true}
|
269
|
-
end
|
270
|
-
EOF
|
271
|
-
runner.prepare("app/models/post.rb", content)
|
272
|
-
methods = RailsBestPractices::Prepares.model_methods
|
273
|
-
methods.get_methods("Post").map(&:method_name).should == ["active"]
|
274
|
-
end
|
275
|
-
|
276
|
-
it "should treat scope as method" do
|
277
|
-
content =<<-EOF
|
278
|
-
class Post < ActiveRecord::Base
|
279
|
-
scope :active, where(:active => true)
|
280
|
-
end
|
281
|
-
EOF
|
282
|
-
runner.prepare("app/models/post.rb", content)
|
283
|
-
methods = RailsBestPractices::Prepares.model_methods
|
284
|
-
methods.get_methods("Post").map(&:method_name).should == ["active"]
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
context "alias" do
|
289
|
-
it "should treat alias as method" do
|
290
|
-
content =<<-EOF
|
291
|
-
class Post < ActiveRecord::Base
|
292
|
-
alias :new :old
|
293
|
-
end
|
294
|
-
EOF
|
295
|
-
runner.prepare("app/models/post.rb", content)
|
296
|
-
methods = RailsBestPractices::Prepares.model_methods
|
297
|
-
methods.get_methods("Post").map(&:method_name).should == ["new"]
|
298
|
-
end
|
299
324
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
context "attributes" do
|
324
|
-
it "should parse mongoid field" do
|
325
|
-
content =<<-EOF
|
326
|
-
class Post
|
327
|
-
include Mongoid::Document
|
328
|
-
field :title
|
329
|
-
field :tags, type: Array
|
330
|
-
field :comments_count, type: Integer
|
331
|
-
field :deleted_at, type: DateTime
|
332
|
-
field :active, type: Boolean
|
333
|
-
end
|
334
|
-
EOF
|
335
|
-
runner.prepare("app/models/post.rb", content)
|
336
|
-
model_attributes = RailsBestPractices::Prepares.model_attributes
|
337
|
-
model_attributes.get_attribute_type("Post", "title").should == "String"
|
338
|
-
model_attributes.get_attribute_type("Post", "tags").should == "Array"
|
339
|
-
model_attributes.get_attribute_type("Post", "comments_count").should == "Integer"
|
340
|
-
model_attributes.get_attribute_type("Post", "deleted_at").should == "DateTime"
|
341
|
-
model_attributes.get_attribute_type("Post", "active").should == "Boolean"
|
342
|
-
end
|
325
|
+
context "attributes" do
|
326
|
+
it "should parse mongoid field" do
|
327
|
+
content =<<-EOF
|
328
|
+
class Post
|
329
|
+
include Mongoid::Document
|
330
|
+
field :title
|
331
|
+
field :tags, type: Array
|
332
|
+
field :comments_count, type: Integer
|
333
|
+
field :deleted_at, type: DateTime
|
334
|
+
field :active, type: Boolean
|
335
|
+
end
|
336
|
+
EOF
|
337
|
+
runner.prepare("app/models/post.rb", content)
|
338
|
+
model_attributes = Prepares.model_attributes
|
339
|
+
model_attributes.get_attribute_type("Post", "title").should == "String"
|
340
|
+
model_attributes.get_attribute_type("Post", "tags").should == "Array"
|
341
|
+
model_attributes.get_attribute_type("Post", "comments_count").should == "Integer"
|
342
|
+
model_attributes.get_attribute_type("Post", "deleted_at").should == "DateTime"
|
343
|
+
model_attributes.get_attribute_type("Post", "active").should == "Boolean"
|
344
|
+
end
|
343
345
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
346
|
+
it "should parse mongomapper field" do
|
347
|
+
content =<<-EOF
|
348
|
+
class Post
|
349
|
+
include MongoMapper::Document
|
350
|
+
key :first_name, String
|
351
|
+
key :last_name, String
|
352
|
+
key :age, Integer
|
353
|
+
key :born_at, Time
|
354
|
+
key :active, Boolean
|
355
|
+
key :fav_colors, Array
|
356
|
+
end
|
357
|
+
EOF
|
358
|
+
runner.prepare("app/models/post.rb", content)
|
359
|
+
model_attributes = Prepares.model_attributes
|
360
|
+
model_attributes.get_attribute_type("Post", "first_name").should == "String"
|
361
|
+
model_attributes.get_attribute_type("Post", "last_name").should == "String"
|
362
|
+
model_attributes.get_attribute_type("Post", "age").should == "Integer"
|
363
|
+
model_attributes.get_attribute_type("Post", "born_at").should == "Time"
|
364
|
+
model_attributes.get_attribute_type("Post", "active").should == "Boolean"
|
365
|
+
model_attributes.get_attribute_type("Post", "fav_colors").should == "Array"
|
366
|
+
end
|
354
367
|
end
|
355
|
-
EOF
|
356
|
-
runner.prepare("app/models/post.rb", content)
|
357
|
-
model_attributes = RailsBestPractices::Prepares.model_attributes
|
358
|
-
model_attributes.get_attribute_type("Post", "first_name").should == "String"
|
359
|
-
model_attributes.get_attribute_type("Post", "last_name").should == "String"
|
360
|
-
model_attributes.get_attribute_type("Post", "age").should == "Integer"
|
361
|
-
model_attributes.get_attribute_type("Post", "born_at").should == "Time"
|
362
|
-
model_attributes.get_attribute_type("Post", "active").should == "Boolean"
|
363
|
-
model_attributes.get_attribute_type("Post", "fav_colors").should == "Array"
|
364
|
-
end
|
365
|
-
end
|
366
368
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
369
|
+
context "no error" do
|
370
|
+
it "should raised for finder_sql option" do
|
371
|
+
content =<<-EOF
|
372
|
+
class EventSubscription < ActiveRecord::Base
|
373
|
+
has_many :event_notification_template, :finder_sql => ?
|
374
|
+
end
|
375
|
+
EOF
|
376
|
+
content.sub!('?', '\'SELECT event_notification_templates.* from event_notification_templates where event_type_id=#{event_type_id} and delivery_method_id=#{delivery_method_id}\'')
|
377
|
+
lambda { runner.prepare('app/models/event_subscription.rb', content) }.should_not raise_error
|
378
|
+
end
|
372
379
|
end
|
373
|
-
EOF
|
374
|
-
content.sub!('?', '\'SELECT event_notification_templates.* from event_notification_templates where event_type_id=#{event_type_id} and delivery_method_id=#{delivery_method_id}\'')
|
375
|
-
lambda { runner.prepare('app/models/event_subscription.rb', content) }.should_not raise_error
|
376
380
|
end
|
377
381
|
end
|
378
382
|
end
|