rails_best_practices 1.9.1 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +10 -2
- data/README.md +33 -27
- data/lib/rails_best_practices/analyzer.rb +13 -13
- data/lib/rails_best_practices/core/check.rb +7 -6
- data/lib/rails_best_practices/core/checking_visitor.rb +1 -1
- data/lib/rails_best_practices/core/runner.rb +2 -3
- data/lib/rails_best_practices/core_ext/sexp.rb +18 -17
- data/lib/rails_best_practices/lexicals.rb +1 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +31 -0
- data/lib/rails_best_practices/prepares/config_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews.rb +2 -0
- 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 +1 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +63 -0
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +1 -1
- 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 +1 -1
- 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 +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +1 -1
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +15 -6
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +4 -2
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +8 -4
- 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 +52 -8
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +14 -2
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +8 -3
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_observer_review.rb +1 -1
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +33 -0
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +16 -6
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -6
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +8 -3
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.yml +28 -25
- data/rake_rubies.sh +1 -1
- data/spec/rails_best_practices/analyzer_spec.rb +1 -1
- data/spec/rails_best_practices/core/check_spec.rb +7 -7
- data/spec/rails_best_practices/core/checking_visitor_spec.rb +7 -7
- data/spec/rails_best_practices/core/error_spec.rb +12 -12
- data/spec/rails_best_practices/core_ext/sexp_spec.rb +103 -85
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +47 -0
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +1 -1
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +1 -1
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +4 -4
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +54 -54
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +46 -46
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +67 -0
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +8 -8
- 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 +7 -7
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/not_use_times_ago_in_words_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +26 -26
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +11 -2
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +25 -10
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +1 -1
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +28 -28
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +4 -4
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +15 -7
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/use_parenthesis_in_method_def_spec.rb +41 -0
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +3 -3
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +8 -8
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +9 -9
- data/spec/spec_helper.rb +1 -1
- metadata +36 -29
- data/.rspec.example +0 -2
- data/.rvmrc.example +0 -2
@@ -3,18 +3,18 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Prepares
|
5
5
|
describe SchemaPrepare do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(prepares: SchemaPrepare.new) }
|
7
7
|
|
8
8
|
it "should parse model attributes" do
|
9
9
|
content =<<-EOF
|
10
|
-
ActiveRecord::Schema.define(:
|
11
|
-
create_table "posts", :
|
10
|
+
ActiveRecord::Schema.define(version: 20110319172136) do
|
11
|
+
create_table "posts", force: true do |t|
|
12
12
|
t.string "title"
|
13
|
-
t.text "body", :
|
13
|
+
t.text "body", limit: 16777215
|
14
14
|
t.datetime "created_at"
|
15
15
|
t.integer "user_id"
|
16
|
-
t.integer "comments_count", :
|
17
|
-
t.boolean "published", :
|
16
|
+
t.integer "comments_count", default: 0
|
17
|
+
t.boolean "published", default: false, null: false
|
18
18
|
end
|
19
19
|
end
|
20
20
|
EOF
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe AddModelVirtualAttributeReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: AddModelVirtualAttributeReview.new) }
|
7
7
|
|
8
8
|
it "should add model virtual attribute" do
|
9
9
|
content = <<-EOF
|
@@ -18,7 +18,7 @@ module RailsBestPractices
|
|
18
18
|
EOF
|
19
19
|
runner.review('app/controllers/users_controller.rb', content)
|
20
20
|
runner.should have(1).errors
|
21
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:
|
21
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:2 - add model virtual attribute (for @user)"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should add model virtual attribute with local assignment" do
|
@@ -34,7 +34,7 @@ module RailsBestPractices
|
|
34
34
|
EOF
|
35
35
|
runner.review('app/controllers/users_controller.rb', content)
|
36
36
|
runner.should have(1).errors
|
37
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:
|
37
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:2 - add model virtual attribute (for user)"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should not add model virtual attribute with differen param" do
|
@@ -81,7 +81,7 @@ module RailsBestPractices
|
|
81
81
|
EOF
|
82
82
|
runner.review('app/controllers/users_controller.rb', content)
|
83
83
|
runner.should have(1).errors
|
84
|
-
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:
|
84
|
+
runner.errors[0].to_s.should == "app/controllers/users_controller.rb:2 - add model virtual attribute (for @user)"
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should no add model virtual attribute with two dimension params" do
|
@@ -3,19 +3,19 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe AlwaysAddDbIndexReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: AlwaysAddDbIndexReview.new) }
|
7
7
|
|
8
8
|
it "should always add db index" do
|
9
9
|
content = <<-EOF
|
10
|
-
ActiveRecord::Schema.define(:
|
11
|
-
create_table "comments", :
|
10
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
11
|
+
create_table "comments", force: true do |t|
|
12
12
|
t.string "content"
|
13
13
|
t.integer "post_id"
|
14
14
|
t.integer "user_id"
|
15
15
|
end
|
16
|
-
create_table "posts", :
|
16
|
+
create_table "posts", force: true do |t|
|
17
17
|
end
|
18
|
-
create_table "users", :
|
18
|
+
create_table "users", force: true do |t|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
EOF
|
@@ -28,8 +28,8 @@ module RailsBestPractices
|
|
28
28
|
|
29
29
|
it "should always add db index with polymorphic foreign key" do
|
30
30
|
content = <<-EOF
|
31
|
-
ActiveRecord::Schema.define(:
|
32
|
-
create_table "versions", :
|
31
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
32
|
+
create_table "versions", force: true do |t|
|
33
33
|
t.integer "versioned_id"
|
34
34
|
t.string "versioned_type"
|
35
35
|
t.string "tag"
|
@@ -44,8 +44,8 @@ module RailsBestPractices
|
|
44
44
|
|
45
45
|
it "should always add db index with polymorphic foreign key and _type is defined before _id" do
|
46
46
|
content = <<-EOF
|
47
|
-
ActiveRecord::Schema.define(:
|
48
|
-
create_table "versions", :
|
47
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
48
|
+
create_table "versions", force: true do |t|
|
49
49
|
t.string "versioned_type"
|
50
50
|
t.integer "versioned_id"
|
51
51
|
t.string "tag"
|
@@ -60,16 +60,16 @@ module RailsBestPractices
|
|
60
60
|
|
61
61
|
it "should always add db index with single index, but without polymorphic foreign key" do
|
62
62
|
content = <<-EOF
|
63
|
-
ActiveRecord::Schema.define(:
|
64
|
-
create_table "taggings", :
|
63
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
64
|
+
create_table "taggings", force: true do |t|
|
65
65
|
t.integer "tag_id"
|
66
66
|
t.integer "taggable_id"
|
67
67
|
t.string "taggable_type"
|
68
68
|
end
|
69
|
-
create_table "tags", :
|
69
|
+
create_table "tags", force: true do |t|
|
70
70
|
end
|
71
71
|
|
72
|
-
add_index "taggings", ["tag_id"], :
|
72
|
+
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
|
73
73
|
end
|
74
74
|
EOF
|
75
75
|
runner.review('db/schema.rb', content)
|
@@ -80,16 +80,16 @@ module RailsBestPractices
|
|
80
80
|
|
81
81
|
it "should always add db index with polymorphic foreign key, but without single index" do
|
82
82
|
content = <<-EOF
|
83
|
-
ActiveRecord::Schema.define(:
|
84
|
-
create_table "taggings", :
|
83
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
84
|
+
create_table "taggings", force: true do |t|
|
85
85
|
t.integer "tag_id"
|
86
86
|
t.integer "taggable_id"
|
87
87
|
t.string "taggable_type"
|
88
88
|
end
|
89
|
-
create_table "tags", :
|
89
|
+
create_table "tags", force: true do |t|
|
90
90
|
end
|
91
91
|
|
92
|
-
add_index "taggings", ["taggable_id", "taggable_type"], :
|
92
|
+
add_index "taggings", ["taggable_id", "taggable_type"], name: "index_taggings_on_taggable_id_and_taggable_type"
|
93
93
|
end
|
94
94
|
EOF
|
95
95
|
runner.review('db/schema.rb', content)
|
@@ -100,14 +100,14 @@ module RailsBestPractices
|
|
100
100
|
|
101
101
|
it "should always add db index only _id without non related _type column" do
|
102
102
|
content = <<-EOF
|
103
|
-
ActiveRecord::Schema.define(:
|
104
|
-
create_table "websites", :
|
103
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
104
|
+
create_table "websites", force: true do |t|
|
105
105
|
t.integer "user_id"
|
106
106
|
t.string "icon_file_name"
|
107
107
|
t.integer "icon_file_size"
|
108
108
|
t.string "icon_content_type"
|
109
109
|
end
|
110
|
-
create_table "users", :
|
110
|
+
create_table "users", force: true do |t|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
EOF
|
@@ -119,8 +119,8 @@ module RailsBestPractices
|
|
119
119
|
|
120
120
|
it "should not always add db index with column has no id" do
|
121
121
|
content = <<-EOF
|
122
|
-
ActiveRecord::Schema.define(:
|
123
|
-
create_table "comments", :
|
122
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
123
|
+
create_table "comments", force: true do |t|
|
124
124
|
t.string "content"
|
125
125
|
t.integer "position"
|
126
126
|
end
|
@@ -133,19 +133,19 @@ module RailsBestPractices
|
|
133
133
|
|
134
134
|
it "should not always add db index with add_index" do
|
135
135
|
content = <<-EOF
|
136
|
-
ActiveRecord::Schema.define(:
|
137
|
-
create_table "comments", :
|
136
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
137
|
+
create_table "comments", force: true do |t|
|
138
138
|
t.string "content"
|
139
139
|
t.integer "post_id"
|
140
140
|
t.integer "user_id"
|
141
141
|
end
|
142
|
-
create_table "posts", :
|
142
|
+
create_table "posts", force: true do |t|
|
143
143
|
end
|
144
|
-
create_table "users", :
|
144
|
+
create_table "users", force: true do |t|
|
145
145
|
end
|
146
146
|
|
147
|
-
add_index "comments", ["post_id"], :
|
148
|
-
add_index "comments", ["user_id"], :
|
147
|
+
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
|
148
|
+
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
|
149
149
|
end
|
150
150
|
EOF
|
151
151
|
runner.review('db/schema.rb', content)
|
@@ -155,8 +155,8 @@ module RailsBestPractices
|
|
155
155
|
|
156
156
|
it "should not always add db index with only _type column" do
|
157
157
|
content = <<-EOF
|
158
|
-
ActiveRecord::Schema.define(:
|
159
|
-
create_table "versions", :
|
158
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
159
|
+
create_table "versions", force: true do |t|
|
160
160
|
t.string "versioned_type"
|
161
161
|
end
|
162
162
|
end
|
@@ -168,14 +168,14 @@ module RailsBestPractices
|
|
168
168
|
|
169
169
|
it "should not always add db index with multi-column index" do
|
170
170
|
content = <<-EOF
|
171
|
-
ActiveRecord::Schema.define(:
|
172
|
-
create_table "versions", :
|
171
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
172
|
+
create_table "versions", force: true do |t|
|
173
173
|
t.integer "versioned_id"
|
174
174
|
t.string "versioned_type"
|
175
175
|
t.string "tag"
|
176
176
|
end
|
177
177
|
|
178
|
-
add_index "versions", ["versioned_id", "versioned_type"], :
|
178
|
+
add_index "versions", ["versioned_id", "versioned_type"], name: "index_versions_on_versioned_id_and_versioned_type"
|
179
179
|
end
|
180
180
|
EOF
|
181
181
|
runner.review('db/schema.rb', content)
|
@@ -185,14 +185,14 @@ module RailsBestPractices
|
|
185
185
|
|
186
186
|
it "should not always add db index if there is an index contains more columns" do
|
187
187
|
content = <<-EOF
|
188
|
-
ActiveRecord::Schema.define(:
|
189
|
-
create_table "taggings", :
|
188
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
189
|
+
create_table "taggings", force: true do |t|
|
190
190
|
t.integer "taggable_id"
|
191
191
|
t.string "taggable_type"
|
192
192
|
t.string "context"
|
193
193
|
end
|
194
194
|
|
195
|
-
add_index "taggings", ["taggable_id", "taggable_type", "context"], :
|
195
|
+
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
|
196
196
|
end
|
197
197
|
EOF
|
198
198
|
runner.review('db/schema.rb', content)
|
@@ -202,15 +202,15 @@ module RailsBestPractices
|
|
202
202
|
|
203
203
|
it "should not always add db index if two indexes for polymorphic association" do
|
204
204
|
content =<<-EOF
|
205
|
-
ActiveRecord::Schema.define(:
|
206
|
-
create_table "taggings", :
|
205
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
206
|
+
create_table "taggings", force: true do |t|
|
207
207
|
t.integer "tagger_id"
|
208
208
|
t.string "tagger_type"
|
209
209
|
t.datetime "created_at"
|
210
210
|
end
|
211
211
|
|
212
|
-
add_index "taggings", ["tagger_id"], :
|
213
|
-
add_index "taggings", ["tagger_type"], :
|
212
|
+
add_index "taggings", ["tagger_id"], name: "index_taggings_on_tagger_id"
|
213
|
+
add_index "taggings", ["tagger_type"], name: "index_taggings_on_tagger_type"
|
214
214
|
end
|
215
215
|
EOF
|
216
216
|
runner.review('db/schema.rb', content)
|
@@ -220,8 +220,8 @@ module RailsBestPractices
|
|
220
220
|
|
221
221
|
it "should not always add db index if table does not exist" do
|
222
222
|
content =<<-EOF
|
223
|
-
ActiveRecord::Schema.define(:
|
224
|
-
create_table "comments", :
|
223
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
224
|
+
create_table "comments", force: true do |t|
|
225
225
|
t.integer "post_id"
|
226
226
|
end
|
227
227
|
end
|
@@ -234,7 +234,7 @@ module RailsBestPractices
|
|
234
234
|
it "should always add db index if association_name is different to foreign_key" do
|
235
235
|
content =<<-EOF
|
236
236
|
class Comment < ActiveRecord::Base
|
237
|
-
belongs_to :commentor, :
|
237
|
+
belongs_to :commentor, class_name: "User"
|
238
238
|
end
|
239
239
|
EOF
|
240
240
|
runner.prepare('app/models/comment.rb', content)
|
@@ -244,11 +244,11 @@ module RailsBestPractices
|
|
244
244
|
EOF
|
245
245
|
runner.prepare('app/models/user.rb', content)
|
246
246
|
content =<<-EOF
|
247
|
-
ActiveRecord::Schema.define(:
|
248
|
-
create_table "comments", :
|
247
|
+
ActiveRecord::Schema.define(version: 20100603080629) do
|
248
|
+
create_table "comments", force: true do |t|
|
249
249
|
t.integer "commentor_id"
|
250
250
|
end
|
251
|
-
create_table "users", :
|
251
|
+
create_table "users", force: true do |t|
|
252
252
|
end
|
253
253
|
end
|
254
254
|
EOF
|
@@ -3,18 +3,18 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe DryBundlerInCapistranoReview do
|
6
|
-
let(:runner) {
|
6
|
+
let(:runner) { Core::Runner.new(reviews: DryBundlerInCapistranoReview.new) }
|
7
7
|
|
8
8
|
it "should dry bundler in capistrno" do
|
9
9
|
content = <<-EOF
|
10
10
|
namespace :bundler do
|
11
|
-
task :create_symlink, :
|
11
|
+
task :create_symlink, roles: :app do
|
12
12
|
shared_dir = File.join(shared_path, 'bundle')
|
13
13
|
release_dir = File.join(current_release, '.bundle')
|
14
14
|
run("mkdir -p \#{shared_dir} && ln -s \#{shared_dir} \#{release_dir}")
|
15
15
|
end
|
16
16
|
|
17
|
-
task :bundle_new_release, :
|
17
|
+
task :bundle_new_release, roles: :app do
|
18
18
|
bundler.create_symlink
|
19
19
|
run "cd \#{release_path} && bundle install --without development test"
|
20
20
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe HashSyntaxReview do
|
6
|
+
let(:runner) { Core::Runner.new(reviews: HashSyntaxReview.new) }
|
7
|
+
|
8
|
+
it "should find 1.8 Hash with symbol" do
|
9
|
+
content =<<-EOF
|
10
|
+
class User < ActiveRecord::Base
|
11
|
+
CONST = { :foo => :bar }
|
12
|
+
end
|
13
|
+
EOF
|
14
|
+
runner.review('app/models/user.rb', content)
|
15
|
+
runner.should have(1).errors
|
16
|
+
runner.errors[0].to_s.should == "app/models/user.rb:2 - change Hash Syntax to 1.9"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should find 1.8 Hash with symbol" do
|
20
|
+
content =<<-EOF
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
CONST = { "foo" => "bar" }
|
23
|
+
end
|
24
|
+
EOF
|
25
|
+
runner.review('app/models/user.rb', content)
|
26
|
+
runner.should have(1).errors
|
27
|
+
runner.errors[0].to_s.should == "app/models/user.rb:2 - change Hash Syntax to 1.9"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not alert on 1.9 Syntax" do
|
31
|
+
content =<<-EOF
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
CONST = { foo: :bar }
|
34
|
+
end
|
35
|
+
EOF
|
36
|
+
runner.review('app/models/user.rb', content)
|
37
|
+
runner.should have(0).errors
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should only check symbol syntax" do
|
41
|
+
runner = Core::Runner.new(reviews: HashSyntaxReview.new(only_symbol: true))
|
42
|
+
content =<<-EOF
|
43
|
+
class User < ActiveRecord::Base
|
44
|
+
SYMBOL_CONST = { :foo => :bar }
|
45
|
+
STRING_CONST = { "foo" => "bar" }
|
46
|
+
end
|
47
|
+
EOF
|
48
|
+
runner.review('app/models/user.rb', content)
|
49
|
+
runner.should have(1).errors
|
50
|
+
runner.errors[0].to_s.should == "app/models/user.rb:2 - change Hash Syntax to 1.9"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should only check string syntax" do
|
54
|
+
runner = Core::Runner.new(reviews: HashSyntaxReview.new(only_string: true))
|
55
|
+
content =<<-EOF
|
56
|
+
class User < ActiveRecord::Base
|
57
|
+
SYMBOL_CONST = { :foo => :bar }
|
58
|
+
STRING_CONST = { "foo" => "bar" }
|
59
|
+
end
|
60
|
+
EOF
|
61
|
+
runner.review('app/models/user.rb', content)
|
62
|
+
runner.should have(1).errors
|
63
|
+
runner.errors[0].to_s.should == "app/models/user.rb:3 - change Hash Syntax to 1.9"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -3,19 +3,19 @@ require 'spec_helper'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Reviews
|
5
5
|
describe IsolateSeedDataReview do
|
6
|
-
let(:runner) { Core::Runner.new(:
|
6
|
+
let(:runner) { Core::Runner.new(reviews: IsolateSeedDataReview.new) }
|
7
7
|
|
8
8
|
context "create" do
|
9
9
|
it "should isolate seed data" do
|
10
10
|
content = <<-EOF
|
11
11
|
class CreateRoles < ActiveRecord::Migration
|
12
12
|
def self.up
|
13
|
-
create_table "roles", :
|
13
|
+
create_table "roles", force: true do |t|
|
14
14
|
t.string :name
|
15
15
|
end
|
16
16
|
|
17
17
|
["admin", "author", "editor", "account"].each do |name|
|
18
|
-
Role.create!(:
|
18
|
+
Role.create!(name: name)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -35,12 +35,12 @@ module RailsBestPractices
|
|
35
35
|
content = <<-EOF
|
36
36
|
class CreateRoles < ActiveRecord::Migration
|
37
37
|
def self.up
|
38
|
-
create_table "roles", :
|
38
|
+
create_table "roles", force: true do |t|
|
39
39
|
t.string :name
|
40
40
|
end
|
41
41
|
|
42
42
|
["admin", "author", "editor", "account"].each do |name|
|
43
|
-
role = Role.new(:
|
43
|
+
role = Role.new(name: name)
|
44
44
|
role.save!
|
45
45
|
end
|
46
46
|
end
|
@@ -59,12 +59,12 @@ module RailsBestPractices
|
|
59
59
|
content = <<-EOF
|
60
60
|
class CreateRoles < ActiveRecord::Migration
|
61
61
|
def self.up
|
62
|
-
create_table "roles", :
|
62
|
+
create_table "roles", force: true do |t|
|
63
63
|
t.string :name
|
64
64
|
end
|
65
65
|
|
66
66
|
["admin", "author", "editor", "account"].each do |name|
|
67
|
-
@role = Role.new(:
|
67
|
+
@role = Role.new(name: name)
|
68
68
|
@role.save!
|
69
69
|
end
|
70
70
|
end
|
@@ -84,7 +84,7 @@ module RailsBestPractices
|
|
84
84
|
content = <<-EOF
|
85
85
|
class CreateRoles < ActiveRecord::Migration
|
86
86
|
def self.up
|
87
|
-
create_table "roles", :
|
87
|
+
create_table "roles", force: true do |t|
|
88
88
|
t.string :name
|
89
89
|
end
|
90
90
|
end
|