rails_best_practices 0.4.0 → 0.4.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/{LICENSE → MIT_LICENSE} +1 -1
- data/README.textile +27 -18
- data/lib/rails_best_practices/command.rb +8 -5
- data/lib/rails_best_practices/version.rb +4 -0
- metadata +115 -82
- data/.gitignore +0 -2
- data/Rakefile +0 -33
- data/VERSION +0 -1
- data/identifier +0 -0
- data/rails_best_practices.gemspec +0 -126
- data/spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb +0 -113
- data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +0 -73
- data/spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb +0 -76
- data/spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb +0 -103
- data/spec/rails_best_practices/checks/law_of_demeter_check_spec.rb +0 -76
- data/spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb +0 -33
- data/spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb +0 -29
- data/spec/rails_best_practices/checks/move_code_into_model_check_spec.rb +0 -55
- data/spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb +0 -82
- data/spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb +0 -49
- data/spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb +0 -70
- data/spec/rails_best_practices/checks/not_use_default_route_check_spec.rb +0 -63
- data/spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb +0 -145
- data/spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb +0 -76
- data/spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb +0 -36
- data/spec/rails_best_practices/checks/use_before_filter_check_spec.rb +0 -85
- data/spec/rails_best_practices/checks/use_model_association_check_spec.rb +0 -71
- data/spec/rails_best_practices/checks/use_observer_check_spec.rb +0 -67
- data/spec/rails_best_practices/checks/use_scope_access_check_spec.rb +0 -193
- data/spec/spec.opts +0 -8
- data/spec/spec_helper.rb +0 -5
@@ -1,113 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::AddModelVirtualAttributeCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AddModelVirtualAttributeCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should add model virtual attribute" do
|
9
|
-
content = <<-EOF
|
10
|
-
class UsersController < ApplicationController
|
11
|
-
|
12
|
-
def create
|
13
|
-
@user = User.new(params[:user])
|
14
|
-
@user.first_name = params[:full_name].split(' ', 2).first
|
15
|
-
@user.last_name = params[:full_name].split(' ', 2).last
|
16
|
-
@user.save
|
17
|
-
end
|
18
|
-
end
|
19
|
-
EOF
|
20
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
21
|
-
errors = @runner.errors
|
22
|
-
errors.should_not be_empty
|
23
|
-
errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should add model virtual attribute with local assignment" do
|
27
|
-
content = <<-EOF
|
28
|
-
class UsersController < ApplicationController
|
29
|
-
|
30
|
-
def create
|
31
|
-
user = User.new(params[:user])
|
32
|
-
user.first_name = params[:full_name].split(' ', 2).first
|
33
|
-
user.last_name = params[:full_name].split(' ', 2).last
|
34
|
-
user.save
|
35
|
-
end
|
36
|
-
end
|
37
|
-
EOF
|
38
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
39
|
-
errors = @runner.errors
|
40
|
-
errors.should_not be_empty
|
41
|
-
errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for user)"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should not add model virtual attribute with differen param" do
|
45
|
-
content = <<-EOF
|
46
|
-
class UsersController < ApplicationController
|
47
|
-
|
48
|
-
def create
|
49
|
-
@user = User.new(params[:user])
|
50
|
-
@user.first_name = params[:first_name]
|
51
|
-
@user.last_name = params[:last_name]
|
52
|
-
@user.save
|
53
|
-
end
|
54
|
-
end
|
55
|
-
EOF
|
56
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
57
|
-
errors = @runner.errors
|
58
|
-
errors.should be_empty
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should not add model virtual attribute with read" do
|
62
|
-
content = <<-EOF
|
63
|
-
class UsersController < ApplicationController
|
64
|
-
|
65
|
-
def show
|
66
|
-
if params[:id]
|
67
|
-
@user = User.find(params[:id])
|
68
|
-
else
|
69
|
-
@user = current_user
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
EOF
|
74
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
75
|
-
errors = @runner.errors
|
76
|
-
errors.should be_empty
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should add model virtual attribute with two dimension params" do
|
80
|
-
content = <<-EOF
|
81
|
-
class UsersController < ApplicationController
|
82
|
-
|
83
|
-
def create
|
84
|
-
@user = User.new(params[:user])
|
85
|
-
@user.first_name = params[:user][:full_name].split(' ', 2).first
|
86
|
-
@user.last_name = params[:user][:full_name].split(' ', 2).last
|
87
|
-
@user.save
|
88
|
-
end
|
89
|
-
end
|
90
|
-
EOF
|
91
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
92
|
-
errors = @runner.errors
|
93
|
-
errors.should_not be_empty
|
94
|
-
errors[0].to_s.should == "app/controllers/users_controller.rb:3 - add model virtual attribute (for @user)"
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should no add model virtual attribute with two dimension params" do
|
98
|
-
content = <<-EOF
|
99
|
-
class UsersController < ApplicationController
|
100
|
-
|
101
|
-
def create
|
102
|
-
@user = User.new(params[:user])
|
103
|
-
@user.first_name = params[:user][:first_name]
|
104
|
-
@user.last_name = params[:user][:last_name]
|
105
|
-
@user.save
|
106
|
-
end
|
107
|
-
end
|
108
|
-
EOF
|
109
|
-
@runner.check('app/controllers/users_controller.rb', content)
|
110
|
-
errors = @runner.errors
|
111
|
-
errors.should be_empty
|
112
|
-
end
|
113
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::AlwaysAddDbIndexCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should always add db index" do
|
9
|
-
content = <<-EOF
|
10
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
11
|
-
create_table "comments", :force => true do |t|
|
12
|
-
t.string "content"
|
13
|
-
t.integer "post_id"
|
14
|
-
t.integer "user_id"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
EOF
|
18
|
-
@runner.check('db/schema.rb', content)
|
19
|
-
errors = @runner.errors
|
20
|
-
errors.should_not be_empty
|
21
|
-
errors[0].to_s.should == "db/schema.rb:4 - always add db index (comments => post_id)"
|
22
|
-
errors[1].to_s.should == "db/schema.rb:5 - always add db index (comments => user_id)"
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should always add db index with column has no id" do
|
26
|
-
content = <<-EOF
|
27
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
28
|
-
create_table "comments", :force => true do |t|
|
29
|
-
t.string "content"
|
30
|
-
t.integer "position"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
EOF
|
34
|
-
@runner.check('db/schema.rb', content)
|
35
|
-
errors = @runner.errors
|
36
|
-
errors.should be_empty
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should not always add db index with add_index" do
|
40
|
-
content = <<-EOF
|
41
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
42
|
-
create_table "comments", :force => true do |t|
|
43
|
-
t.string "content"
|
44
|
-
t.integer "post_id"
|
45
|
-
t.integer "user_id"
|
46
|
-
end
|
47
|
-
|
48
|
-
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
|
49
|
-
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
|
50
|
-
end
|
51
|
-
EOF
|
52
|
-
@runner.check('db/schema.rb', content)
|
53
|
-
errors = @runner.errors
|
54
|
-
errors.should be_empty
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should not always add db index with multi-column index" do
|
58
|
-
content = <<-EOF
|
59
|
-
ActiveRecord::Schema.define(:version => 20100603080629) do
|
60
|
-
create_table "versions", :force => true do |t|
|
61
|
-
t.integer "versioned_id"
|
62
|
-
t.string "versioned_type"
|
63
|
-
t.string "tag"
|
64
|
-
end
|
65
|
-
|
66
|
-
add_index "versions", ["versioned_id", "versioned_type"], :name => "index_versions_on_versioned_id_and_versioned_type"
|
67
|
-
end
|
68
|
-
EOF
|
69
|
-
@runner.check('db/schema.rb', content)
|
70
|
-
errors = @runner.errors
|
71
|
-
errors.should be_empty
|
72
|
-
end
|
73
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::IsolateSeedDataCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::IsolateSeedDataCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should isolate seed data" do
|
9
|
-
content = <<-EOF
|
10
|
-
class CreateRoles < ActiveRecord::Migration
|
11
|
-
def self.up
|
12
|
-
create_table "roles", :force => true do |t|
|
13
|
-
t.string :name
|
14
|
-
end
|
15
|
-
|
16
|
-
["admin", "author", "editor", "account"].each do |name|
|
17
|
-
Role.create!(:name => name)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.down
|
22
|
-
drop_table "roles"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
EOF
|
26
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
27
|
-
errors = @runner.errors
|
28
|
-
errors.should_not be_empty
|
29
|
-
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:8 - isolate seed data"
|
30
|
-
errors.size.should == 1
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should isolate seed data with new and save" do
|
34
|
-
content = <<-EOF
|
35
|
-
class CreateRoles < ActiveRecord::Migration
|
36
|
-
def self.up
|
37
|
-
create_table "roles", :force => true do |t|
|
38
|
-
t.string :name
|
39
|
-
end
|
40
|
-
|
41
|
-
["admin", "author", "editor", "account"].each do |name|
|
42
|
-
role = Role.new(:name => name)
|
43
|
-
role.save!
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.down
|
48
|
-
drop_table "roles"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
EOF
|
52
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
53
|
-
errors = @runner.errors
|
54
|
-
errors.should_not be_empty
|
55
|
-
errors[0].to_s.should == "db/migrate/20090818130258_create_roles.rb:9 - isolate seed data"
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should not isolate seed data without data insert" do
|
59
|
-
content = <<-EOF
|
60
|
-
class CreateRoles < ActiveRecord::Migration
|
61
|
-
def self.up
|
62
|
-
create_table "roles", :force => true do |t|
|
63
|
-
t.string :name
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.down
|
68
|
-
drop_table "roles"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
EOF
|
72
|
-
@runner.check('db/migrate/20090818130258_create_roles.rb', content)
|
73
|
-
errors = @runner.errors
|
74
|
-
errors.should be_empty
|
75
|
-
end
|
76
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should keep finders on thier own model" do
|
9
|
-
content = <<-EOF
|
10
|
-
class Post < ActiveRecord::Base
|
11
|
-
has_many :comments
|
12
|
-
|
13
|
-
def find_valid_comments
|
14
|
-
self.comment.find(:all, :conditions => { :is_spam => false },
|
15
|
-
:limit => 10)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
EOF
|
19
|
-
@runner.check('app/models/post.rb', content)
|
20
|
-
errors = @runner.errors
|
21
|
-
errors.should_not be_empty
|
22
|
-
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should keep finders on thier own model with all method" do
|
26
|
-
content = <<-EOF
|
27
|
-
class Post < ActiveRecord::Base
|
28
|
-
has_many :comments
|
29
|
-
|
30
|
-
def find_valid_comments
|
31
|
-
self.comment.all(:conditions => { :is_spam => false },
|
32
|
-
:limit => 10)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
EOF
|
36
|
-
@runner.check('app/models/post.rb', content)
|
37
|
-
errors = @runner.errors
|
38
|
-
errors.should_not be_empty
|
39
|
-
errors[0].to_s.should == "app/models/post.rb:5 - keep finders on their own model"
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should not keep finders on thier own model with self finder" do
|
43
|
-
content = <<-EOF
|
44
|
-
class Post < ActiveRecord::Base
|
45
|
-
has_many :comments
|
46
|
-
|
47
|
-
def find_valid_comments
|
48
|
-
self.find(:all, :conditions => { :is_spam => false },
|
49
|
-
:limit => 10)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
EOF
|
53
|
-
@runner.check('app/models/post.rb', content)
|
54
|
-
errors = @runner.errors
|
55
|
-
errors.should be_empty
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should not keep finders on thier own model with own finder" do
|
59
|
-
content = <<-EOF
|
60
|
-
class Post < ActiveRecord::Base
|
61
|
-
has_many :comments
|
62
|
-
|
63
|
-
def find_valid_comments
|
64
|
-
Post.find(:all, :conditions => { :is_spam => false },
|
65
|
-
:limit => 10)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
EOF
|
69
|
-
@runner.check('app/models/post.rb', content)
|
70
|
-
errors = @runner.errors
|
71
|
-
errors.should be_empty
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should not keep finders on their own model without finder" do
|
75
|
-
content = <<-EOF
|
76
|
-
class Post < ActiveRecord::Base
|
77
|
-
has_many :comments
|
78
|
-
|
79
|
-
def find_valid_comments
|
80
|
-
self.comments.destroy_all
|
81
|
-
end
|
82
|
-
end
|
83
|
-
EOF
|
84
|
-
@runner.check('app/models/post.rb', content)
|
85
|
-
errors = @runner.errors
|
86
|
-
errors.should be_empty
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should not keep finders on their own model with ruby Array#find" do
|
90
|
-
content = <<-EOF
|
91
|
-
class Post < ActiveRecord::Base
|
92
|
-
has_many :comments
|
93
|
-
|
94
|
-
def active_comments
|
95
|
-
self.comments.find {|comment| comment.status == 'active'}
|
96
|
-
end
|
97
|
-
end
|
98
|
-
EOF
|
99
|
-
@runner.check('app/models/post.rb', content)
|
100
|
-
errors = @runner.errors
|
101
|
-
errors.should be_empty
|
102
|
-
end
|
103
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::LawOfDemeterCheck do
|
4
|
-
|
5
|
-
describe "belongs_to" do
|
6
|
-
before(:each) do
|
7
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
8
|
-
|
9
|
-
content = <<-EOF
|
10
|
-
class Invoice < ActiveRecord::Base
|
11
|
-
belongs_to :user
|
12
|
-
end
|
13
|
-
EOF
|
14
|
-
@runner.check('app/models/invoice.rb', content)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should law of demeter" do
|
18
|
-
content = <<-EOF
|
19
|
-
<%= @invoice.user.name %>
|
20
|
-
<%= @invoice.user.address %>
|
21
|
-
<%= @invoice.user.cellphone %>
|
22
|
-
EOF
|
23
|
-
@runner.check('app/views/invoices/show.html.erb', content)
|
24
|
-
errors = @runner.errors
|
25
|
-
errors.should_not be_empty
|
26
|
-
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should law of demeter" do
|
30
|
-
content = <<-EOF
|
31
|
-
= @invoice.user.name
|
32
|
-
= @invoice.user.address
|
33
|
-
= @invoice.user.cellphone
|
34
|
-
EOF
|
35
|
-
@runner.check('app/views/invoices/show.html.haml', content)
|
36
|
-
errors = @runner.errors
|
37
|
-
errors.should_not be_empty
|
38
|
-
errors[0].to_s.should == "app/views/invoices/show.html.haml:1 - law of demeter"
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should no law of demeter" do
|
42
|
-
content = <<-EOF
|
43
|
-
<%= @invoice.user_name %>
|
44
|
-
<%= @invoice.user_address %>
|
45
|
-
<%= @invoice.user_cellphone %>
|
46
|
-
EOF
|
47
|
-
@runner.check('app/views/invoices/show.html.erb', content)
|
48
|
-
errors = @runner.errors
|
49
|
-
errors.should be_empty
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "has_one" do
|
54
|
-
before(:each) do
|
55
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::LawOfDemeterCheck.new)
|
56
|
-
|
57
|
-
content = <<-EOF
|
58
|
-
class Invoice < ActiveRecord::Base
|
59
|
-
has_one :price
|
60
|
-
end
|
61
|
-
EOF
|
62
|
-
@runner.check('app/models/invoice.rb', content)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should law of demeter" do
|
66
|
-
content = <<-EOF
|
67
|
-
<%= @invoice.price.currency %>
|
68
|
-
<%= @invoice.price.number %>
|
69
|
-
EOF
|
70
|
-
@runner.check('app/views/invoices/show.html.erb', content)
|
71
|
-
errors = @runner.errors
|
72
|
-
errors.should_not be_empty
|
73
|
-
errors[0].to_s.should == "app/views/invoices/show.html.erb:1 - law of demeter"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe RailsBestPractices::Checks::MoveCodeIntoControllerCheck do
|
4
|
-
before(:each) do
|
5
|
-
@runner = RailsBestPractices::Core::Runner.new(RailsBestPractices::Checks::MoveCodeIntoControllerCheck.new)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should move code into controller" do
|
9
|
-
content = <<-EOF
|
10
|
-
<% @posts = Post.find(:all) %>
|
11
|
-
<% @posts.each do |post| %>
|
12
|
-
<%=h post.title %>
|
13
|
-
<%=h post.content %>
|
14
|
-
<% end %>
|
15
|
-
EOF
|
16
|
-
@runner.check('app/views/posts/index.html.erb', content)
|
17
|
-
errors = @runner.errors
|
18
|
-
errors.should_not be_empty
|
19
|
-
errors[0].to_s.should == "app/views/posts/index.html.erb:1 - move code into controller"
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should not move code into controller" do
|
23
|
-
content = <<-EOF
|
24
|
-
<% @posts.each do |post| %>
|
25
|
-
<%=h post.title %>
|
26
|
-
<%=h post.content %>
|
27
|
-
<% end %>
|
28
|
-
EOF
|
29
|
-
@runner.check('app/views/posts/index.html.erb', content)
|
30
|
-
errors = @runner.errors
|
31
|
-
errors.should be_empty
|
32
|
-
end
|
33
|
-
end
|