rails_best_practices 1.9.0 → 1.9.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.
- 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,118 +1,122 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe UseSayWithTimeInMigrationsReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => UseSayWithTimeInMigrationsReview.new) }
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
it "should use say with time in migrations" do
|
9
|
+
content =<<-EOF
|
10
|
+
def self.up
|
11
|
+
User.find_each do |user|
|
12
|
+
user.first_name, user.last_name = user.full_name.split(' ')
|
13
|
+
user.save
|
14
|
+
end
|
15
|
+
end
|
16
|
+
EOF
|
17
|
+
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
18
|
+
runner.should have(1).errors
|
19
|
+
runner.errors[0].to_s.should == "db/migrate/20101010080658_update_users.rb:2 - use say with time in migrations"
|
12
20
|
end
|
13
|
-
end
|
14
|
-
EOF
|
15
|
-
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
16
|
-
runner.should have(1).errors
|
17
|
-
runner.errors[0].to_s.should == "db/migrate/20101010080658_update_users.rb:2 - use say with time in migrations"
|
18
|
-
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
it "should use say with time in migrations with create_table" do
|
23
|
+
content =<<-EOF
|
24
|
+
def self.up
|
25
|
+
create_table :users do |t|
|
26
|
+
t.string :login
|
27
|
+
t.timestamps
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
User.find_each do |user|
|
31
|
+
user.first_name, user.last_name = user.full_name.split(' ')
|
32
|
+
user.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
EOF
|
36
|
+
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
37
|
+
runner.should have(1).errors
|
38
|
+
runner.errors[0].to_s.should == "db/migrate/20101010080658_update_users.rb:7 - use say with time in migrations"
|
31
39
|
end
|
32
|
-
end
|
33
|
-
EOF
|
34
|
-
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
35
|
-
runner.should have(1).errors
|
36
|
-
runner.errors[0].to_s.should == "db/migrate/20101010080658_update_users.rb:7 - use say with time in migrations"
|
37
|
-
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
it "should not use say with time in migrations" do
|
42
|
+
content =<<-EOF
|
43
|
+
def self.up
|
44
|
+
say_with_time("Initialize first_name and last_name for users") do
|
45
|
+
User.find_each do |user|
|
46
|
+
user.first_name, user.last_name = user.full_name.split(' ')
|
47
|
+
user.save
|
48
|
+
say(user.id + " was updated.")
|
49
|
+
end
|
50
|
+
end
|
47
51
|
end
|
52
|
+
EOF
|
53
|
+
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
54
|
+
runner.should have(0).errors
|
48
55
|
end
|
49
|
-
end
|
50
|
-
EOF
|
51
|
-
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
52
|
-
runner.should have(0).errors
|
53
|
-
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
it "should not use say with time in migrations when not first code line" do
|
58
|
+
content =<<-EOF
|
59
|
+
def self.up
|
60
|
+
User.find_each do |user|
|
61
|
+
say_with_time 'Updating user with latest data' do
|
62
|
+
user.do_time_consuming_stuff
|
63
|
+
end
|
64
|
+
end
|
61
65
|
end
|
66
|
+
EOF
|
67
|
+
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
68
|
+
runner.should have(0).errors
|
62
69
|
end
|
63
|
-
end
|
64
|
-
EOF
|
65
|
-
runner.review('db/migrate/20101010080658_update_users.rb', content)
|
66
|
-
runner.should have(0).errors
|
67
|
-
end
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
it "should not use say with time when default migration message" do
|
72
|
+
content =<<-EOF
|
73
|
+
def self.up
|
74
|
+
create_table :users do |t|
|
75
|
+
t.string :login
|
76
|
+
t.string :email
|
77
|
+
t.timestamps
|
78
|
+
end
|
79
|
+
end
|
80
|
+
EOF
|
81
|
+
runner.review('db/migrate/20101010080658_create_users.rb', content)
|
82
|
+
runner.should have(0).errors
|
76
83
|
end
|
77
|
-
end
|
78
|
-
EOF
|
79
|
-
runner.review('db/migrate/20101010080658_create_users.rb', content)
|
80
|
-
runner.should have(0).errors
|
81
|
-
end
|
82
84
|
|
83
|
-
|
84
|
-
|
85
|
-
|
85
|
+
it "should not raise an error" do
|
86
|
+
content =<<-EOF
|
87
|
+
class AddAdmin < ActiveRecord::Migration
|
86
88
|
|
87
|
-
|
88
|
-
|
89
|
+
class Person < ActiveRecord::Base
|
90
|
+
end
|
89
91
|
|
90
|
-
|
91
|
-
|
92
|
+
class Blog < ActiveRecord::Base
|
93
|
+
end
|
92
94
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
95
|
+
def self.up
|
96
|
+
add_column :people, :admin, :boolean, :default => false, :null => false
|
97
|
+
add_column :people, :deactivated, :boolean,
|
98
|
+
:default => false, :null => false
|
97
99
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
100
|
+
key = Crypto::Key.from_file("\#{RAILS_ROOT}/rsa_key.pub")
|
101
|
+
person = Person.new(:email => "admin@example.com",
|
102
|
+
:name => "admin",
|
103
|
+
:crypted_password => key.encrypt("admin"),
|
104
|
+
:description => "")
|
105
|
+
person.admin = true
|
106
|
+
person.save!
|
107
|
+
Blog.create(:person_id => person.id)
|
108
|
+
end
|
107
109
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
110
|
+
def self.down
|
111
|
+
remove_column :people, :deactivated
|
112
|
+
Person.delete(Person.find_by_name("admin"))
|
113
|
+
remove_column :people, :admin
|
114
|
+
end
|
115
|
+
end
|
116
|
+
EOF
|
117
|
+
runner.review('db/migrate/20101010080658_create_users.rb', content)
|
118
|
+
runner.should have(3).errors
|
112
119
|
end
|
113
120
|
end
|
114
|
-
EOF
|
115
|
-
runner.review('db/migrate/20101010080658_create_users.rb', content)
|
116
|
-
runner.should have(3).errors
|
117
121
|
end
|
118
122
|
end
|
@@ -1,184 +1,179 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
3
|
+
module RailsBestPractices
|
4
|
+
module Reviews
|
5
|
+
describe UseScopeAccessReview do
|
6
|
+
let(:runner) { Core::Runner.new(:reviews => UseScopeAccessReview.new) }
|
7
|
+
|
8
|
+
context "if" do
|
9
|
+
it "shoud use scope access" do
|
10
|
+
content = <<-EOF
|
11
|
+
class PostsController < ApplicationController
|
12
|
+
def edit
|
13
|
+
@post = Post.find(params[:id])
|
14
|
+
|
15
|
+
if @post.user != current_user
|
16
|
+
flash[:warning] = 'Access Denied'
|
17
|
+
redirect_to posts_url
|
18
|
+
end
|
19
|
+
end
|
17
20
|
end
|
21
|
+
EOF
|
22
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
23
|
+
runner.should have(1).errors
|
24
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
18
25
|
end
|
19
|
-
end
|
20
|
-
EOF
|
21
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
22
|
-
runner.should have(1).errors
|
23
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
it "shoud use scope access by comparing with id" do
|
28
|
+
content = <<-EOF
|
29
|
+
class PostsController < ApplicationController
|
30
|
+
def edit
|
31
|
+
@post = Post.find(params[:id])
|
32
|
+
|
33
|
+
if @post.user_id != current_user.id
|
34
|
+
flash[:warning] = 'Access Denied'
|
35
|
+
redirect_to posts_url
|
36
|
+
end
|
37
|
+
end
|
36
38
|
end
|
39
|
+
EOF
|
40
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
41
|
+
runner.should have(1).errors
|
42
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
37
43
|
end
|
38
|
-
end
|
39
|
-
EOF
|
40
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
41
|
-
runner.should have(1).errors
|
42
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
43
|
-
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
45
|
+
it "shoud use scope access with current_user ==" do
|
46
|
+
content = <<-EOF
|
47
|
+
class PostsController < ApplicationController
|
48
|
+
def edit
|
49
|
+
@post = Post.find(params[:id])
|
50
|
+
|
51
|
+
if current_user != @post.user
|
52
|
+
flash[:warning] = 'Access Denied'
|
53
|
+
redirect_to posts_url
|
54
|
+
end
|
55
|
+
end
|
55
56
|
end
|
57
|
+
EOF
|
58
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
59
|
+
runner.should have(1).errors
|
60
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
56
61
|
end
|
57
|
-
end
|
58
|
-
EOF
|
59
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
60
|
-
runner.should have(1).errors
|
61
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
62
|
-
end
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
63
|
+
it "shoud use scope access by current_user.id ==" do
|
64
|
+
content = <<-EOF
|
65
|
+
class PostsController < ApplicationController
|
66
|
+
def edit
|
67
|
+
@post = Post.find(params[:id])
|
68
|
+
|
69
|
+
if current_user.id != @post.user_id
|
70
|
+
flash[:warning] = 'Access Denied'
|
71
|
+
redirect_to posts_url
|
72
|
+
end
|
73
|
+
end
|
74
74
|
end
|
75
|
+
EOF
|
76
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
77
|
+
runner.should have(1).errors
|
78
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
75
79
|
end
|
76
80
|
end
|
77
|
-
EOF
|
78
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
79
|
-
runner.should have(1).errors
|
80
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
81
|
-
end
|
82
|
-
end
|
83
81
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
82
|
+
context "unless" do
|
83
|
+
it "shoud use scope access" do
|
84
|
+
content = <<-EOF
|
85
|
+
class PostsController < ApplicationController
|
86
|
+
def edit
|
87
|
+
@post = Post.find(params[:id])
|
88
|
+
|
89
|
+
unless @post.user == current_user
|
90
|
+
flash[:warning] = 'Access Denied'
|
91
|
+
redirect_to posts_url
|
92
|
+
end
|
93
|
+
end
|
95
94
|
end
|
95
|
+
EOF
|
96
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
97
|
+
runner.should have(1).errors
|
98
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
96
99
|
end
|
97
|
-
end
|
98
|
-
EOF
|
99
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
100
|
-
runner.should have(1).errors
|
101
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
102
|
-
end
|
103
|
-
|
104
|
-
it "shoud use scope access by comparing with id" do
|
105
|
-
content = <<-EOF
|
106
|
-
class PostsController < ApplicationController
|
107
100
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
101
|
+
it "shoud use scope access by comparing with id" do
|
102
|
+
content = <<-EOF
|
103
|
+
class PostsController < ApplicationController
|
104
|
+
def edit
|
105
|
+
@post = Post.find(params[:id])
|
106
|
+
|
107
|
+
unless @post.user_id == current_user.id
|
108
|
+
flash[:warning] = 'Access Denied'
|
109
|
+
redirect_to posts_url
|
110
|
+
end
|
111
|
+
end
|
114
112
|
end
|
113
|
+
EOF
|
114
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
115
|
+
runner.should have(1).errors
|
116
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
115
117
|
end
|
116
|
-
end
|
117
|
-
EOF
|
118
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
119
|
-
runner.should have(1).errors
|
120
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
121
|
-
end
|
122
|
-
|
123
|
-
it "shoud use scope access with current_user ==" do
|
124
|
-
content = <<-EOF
|
125
|
-
class PostsController < ApplicationController
|
126
118
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
119
|
+
it "shoud use scope access with current_user ==" do
|
120
|
+
content = <<-EOF
|
121
|
+
class PostsController < ApplicationController
|
122
|
+
def edit
|
123
|
+
@post = Post.find(params[:id])
|
124
|
+
|
125
|
+
unless current_user == @post.user
|
126
|
+
flash[:warning] = 'Access Denied'
|
127
|
+
redirect_to posts_url
|
128
|
+
end
|
129
|
+
end
|
133
130
|
end
|
131
|
+
EOF
|
132
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
133
|
+
runner.should have(1).errors
|
134
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
134
135
|
end
|
135
|
-
end
|
136
|
-
EOF
|
137
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
138
|
-
runner.should have(1).errors
|
139
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
140
|
-
end
|
141
|
-
|
142
|
-
it "shoud use scope access by current_user.id ==" do
|
143
|
-
content = <<-EOF
|
144
|
-
class PostsController < ApplicationController
|
145
136
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
137
|
+
it "shoud use scope access by current_user.id ==" do
|
138
|
+
content = <<-EOF
|
139
|
+
class PostsController < ApplicationController
|
140
|
+
def edit
|
141
|
+
@post = Post.find(params[:id])
|
142
|
+
|
143
|
+
unless current_user.id == @post.user_id
|
144
|
+
flash[:warning] = 'Access Denied'
|
145
|
+
redirect_to posts_url
|
146
|
+
end
|
147
|
+
end
|
152
148
|
end
|
149
|
+
EOF
|
150
|
+
runner.review('app/controllers/posts_controller.rb', content)
|
151
|
+
runner.should have(1).errors
|
152
|
+
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
153
153
|
end
|
154
|
-
end
|
155
|
-
EOF
|
156
|
-
runner.review('app/controllers/posts_controller.rb', content)
|
157
|
-
runner.should have(1).errors
|
158
|
-
runner.errors[0].to_s.should == "app/controllers/posts_controller.rb:6 - use scope access"
|
159
|
-
end
|
160
154
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
155
|
+
it "should no error in use_scope_access_review" do
|
156
|
+
content = <<-EOF
|
157
|
+
class CommentsController < ApplicationController
|
158
|
+
def add_comment
|
159
|
+
@current_user = User.find_by_id(session[:user_id])
|
160
|
+
@id = params[:post_id]
|
161
|
+
@error = ""
|
162
|
+
if (@text = params[:text]) == ""
|
163
|
+
@error = "Please enter a comment!"
|
164
|
+
else
|
165
|
+
@comment = Comment.create_object(@text, @id, @current_user.id)
|
166
|
+
end
|
167
|
+
unless @comment
|
168
|
+
@error = "Comment could not be saved."
|
169
|
+
end
|
170
|
+
end
|
176
171
|
end
|
172
|
+
EOF
|
173
|
+
runner.review('app/controllers/comments_controller.rb', content)
|
174
|
+
runner.should have(0).errors
|
177
175
|
end
|
178
176
|
end
|
179
|
-
EOF
|
180
|
-
runner.review('app/controllers/comments_controller.rb', content)
|
181
|
-
runner.should have(0).errors
|
182
177
|
end
|
183
178
|
end
|
184
179
|
end
|