rails_best_practices 0.3.19 → 0.3.20
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/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/rails_best_practices/checks/always_add_db_index_check.rb +24 -97
- data/lib/rails_best_practices/checks/check.rb +1 -1
- data/rails_best_practices.gemspec +19 -19
- data/spec/rails_best_practices/checks/always_add_db_index_check_spec.rb +23 -290
- metadata +19 -19
data/Rakefile
CHANGED
@@ -6,10 +6,10 @@ require 'jeweler'
|
|
6
6
|
desc 'Default: run unit tests.'
|
7
7
|
task :default => :spec
|
8
8
|
|
9
|
-
desc 'Generate documentation for the
|
9
|
+
desc 'Generate documentation for the rails_best_practices plugin.'
|
10
10
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
11
11
|
rdoc.rdoc_dir = 'rdoc'
|
12
|
-
rdoc.title = '
|
12
|
+
rdoc.title = 'rails_best_practices'
|
13
13
|
rdoc.options << '--line-numbers' << '--inline-source'
|
14
14
|
rdoc.rdoc_files.include('README')
|
15
15
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.20
|
@@ -2,125 +2,52 @@ require 'rails_best_practices/checks/check'
|
|
2
2
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Checks
|
5
|
-
# Check
|
5
|
+
# Check db/schema.rb file to make sure every reference key has a database index.
|
6
6
|
#
|
7
|
-
# Implementation:
|
8
|
-
# Parse migration files twice.
|
9
|
-
# First, remember all reference keys and index keys.
|
10
|
-
# Second, compare reference keys and index keys, and add error when reference keys are not in index keys.
|
7
|
+
# Implementation: read all add_index method calls to get the indexed columns in table, then read integer method call in create_table block to get the reference columns in tables, compare with indexed columns, if not in the indexed columns, then it violates always_add_db_index_check.
|
11
8
|
class AlwaysAddDbIndexCheck < Check
|
12
9
|
|
13
10
|
def interesting_nodes
|
14
|
-
[:
|
11
|
+
[:block, :call]
|
15
12
|
end
|
16
13
|
|
17
14
|
def interesting_files
|
18
|
-
|
15
|
+
/db\/schema.rb/
|
19
16
|
end
|
20
17
|
|
21
18
|
def initialize
|
22
19
|
super
|
23
|
-
@
|
24
|
-
@@indexes = {}
|
25
|
-
@@tables = []
|
26
|
-
@parse = false
|
27
|
-
end
|
28
|
-
|
29
|
-
# make indexes class method for get indexes out of AlwaysAddDbIndexCheck class.
|
30
|
-
def self.indexes
|
31
|
-
@@indexes
|
20
|
+
@index_columns = []
|
32
21
|
end
|
33
22
|
|
34
23
|
def evaluate_start(node)
|
35
|
-
if :
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
remember(node.body)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def check_references(nodes)
|
53
|
-
nodes[1..-1].each do |node|
|
54
|
-
create_table_node = node.grep_nodes({:node_type => :call, :message => :create_table}).first
|
55
|
-
if create_table_node
|
56
|
-
table_name = create_table_node.arguments[1].to_ruby_string
|
57
|
-
next unless @@tables.include? table_name
|
58
|
-
node.grep_nodes({:node_type => :call, :message => :integer}).each do |integer_node|
|
59
|
-
column_name = integer_node.arguments[1].to_ruby_string
|
60
|
-
if column_name =~ /_id$/ and !@@indexes[table_name].include? column_name
|
61
|
-
add_error "always add db index (#{table_name} => #{column_name})", integer_node.file, integer_node.line
|
62
|
-
end
|
63
|
-
end
|
64
|
-
node.grep_nodes({:node_type => :call, :message => :references}).each do |references_node|
|
65
|
-
column_name = references_node.arguments[1].to_ruby_string + "_id"
|
66
|
-
if !@@indexes[table_name].include? column_name
|
67
|
-
add_error "always add db index (#{table_name} => #{column_name})", references_node.file, references_node.line
|
68
|
-
end
|
69
|
-
end
|
70
|
-
node.grep_nodes({:node_type => :call, :message => :column}).each do |column_node|
|
71
|
-
if 'integer' == column_node.arguments[2].to_ruby_string
|
72
|
-
column_name = column_node.arguments[1].to_ruby_string
|
73
|
-
if column_name =~ /_id$/ and !@@indexes[table_name].include? column_name
|
74
|
-
add_error "always add db index (#{table_name} => #{column_name})", column_node.file, column_node.line
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
if :call == node.node_type and :add_column == node.message
|
80
|
-
table_name = node.arguments[1].to_ruby_string
|
81
|
-
column_name = node.arguments[2].to_ruby_string
|
82
|
-
column_type = node.arguments[3].to_ruby_string
|
83
|
-
if column_name =~ /_id$/ and column_type == "integer" and !@@indexes[table_name].include? column_name
|
84
|
-
add_error "always add db index (#{table_name} => #{column_name})", node.file, node.line
|
24
|
+
if :block == node.node_type
|
25
|
+
find_index_columns(node)
|
26
|
+
elsif :call == node.node_type
|
27
|
+
case node.message
|
28
|
+
when :create_table
|
29
|
+
@table_name = node.arguments[1].to_ruby_string
|
30
|
+
when :integer
|
31
|
+
column_name = node.arguments[1].to_ruby_string
|
32
|
+
if column_name =~ /_id$/ and !indexed?(@table_name, column_name)
|
33
|
+
add_error "always add db index (#@table_name => #{column_name})", node.file, node.line
|
85
34
|
end
|
86
35
|
end
|
87
36
|
end
|
88
37
|
end
|
89
38
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
39
|
+
private
|
40
|
+
def find_index_columns(node)
|
41
|
+
node.grep_nodes({:node_type => :call, :message => :add_index}).each do |index_node|
|
42
|
+
table_name = index_node.arguments[1].to_ruby_string
|
43
|
+
reference_column_name = index_node.arguments[2].to_ruby_string
|
44
|
+
@index_columns << [table_name, reference_column_name]
|
45
|
+
end
|
96
46
|
end
|
97
|
-
@@tables.compact!
|
98
47
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
# dynamically execute add_index because static parser can't handle
|
103
|
-
#
|
104
|
-
# [[:comments, :post_id], [:comments, :user_id]].each do |args|
|
105
|
-
# add_index *args
|
106
|
-
# end
|
107
|
-
def remember_indexes(nodes)
|
108
|
-
nodes[1..-1].each do |node|
|
109
|
-
begin
|
110
|
-
eval(node.to_ruby)
|
111
|
-
rescue Exception
|
112
|
-
end
|
48
|
+
def indexed?(table_name, column_name)
|
49
|
+
!!@index_columns.find { |reference| reference[0] == table_name and reference[1] == column_name }
|
113
50
|
end
|
114
|
-
end
|
115
51
|
end
|
116
52
|
end
|
117
53
|
end
|
118
|
-
|
119
|
-
def add_index(*args)
|
120
|
-
table_name, column_names = *args
|
121
|
-
table_name = table_name.to_s
|
122
|
-
RailsBestPractices::Checks::AlwaysAddDbIndexCheck.indexes[table_name] ||= []
|
123
|
-
Array(column_names).each do |column_name|
|
124
|
-
RailsBestPractices::Checks::AlwaysAddDbIndexCheck.indexes[table_name] << column_name.to_s
|
125
|
-
end
|
126
|
-
end
|
@@ -3,7 +3,7 @@ require 'rails_best_practices/core/error'
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Checks
|
5
5
|
class Check
|
6
|
-
NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn, :ivar]
|
6
|
+
NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn, :ivar, :block]
|
7
7
|
|
8
8
|
CONTROLLER_FILES = /_controller.rb$/
|
9
9
|
MIGRATION_FILES = /db\/migrate\/.*rb/
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rails_best_practices}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.20"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Richard Huang"]
|
12
|
-
s.date = %q{2010-03
|
12
|
+
s.date = %q{2010-06-03}
|
13
13
|
s.default_executable = %q{rails_best_practices}
|
14
14
|
s.description = %q{check rails files according to ihower's presentation 'rails best practices'}
|
15
15
|
s.email = %q{flyerhzm@gmail.com}
|
@@ -91,28 +91,28 @@ Gem::Specification.new do |s|
|
|
91
91
|
s.rubygems_version = %q{1.3.6}
|
92
92
|
s.summary = %q{check rails files according to ihower's presentation 'rails best practices'}
|
93
93
|
s.test_files = [
|
94
|
-
"spec/rails_best_practices/checks/
|
95
|
-
"spec/rails_best_practices/checks/always_add_db_index_check_spec.rb",
|
96
|
-
"spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
|
97
|
-
"spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
|
98
|
-
"spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
|
99
|
-
"spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
|
100
|
-
"spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb",
|
101
|
-
"spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb",
|
102
|
-
"spec/rails_best_practices/checks/move_code_into_model_check_spec.rb",
|
103
|
-
"spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
|
94
|
+
"spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb",
|
104
95
|
"spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb",
|
105
|
-
"spec/rails_best_practices/checks/
|
106
|
-
"spec/rails_best_practices/checks/
|
107
|
-
"spec/rails_best_practices/checks/
|
96
|
+
"spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb",
|
97
|
+
"spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb",
|
98
|
+
"spec/rails_best_practices/checks/use_observer_check_spec.rb",
|
108
99
|
"spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb",
|
100
|
+
"spec/rails_best_practices/checks/nested_model_forms_check_spec.rb",
|
101
|
+
"spec/rails_best_practices/checks/always_add_db_index_check_spec.rb",
|
102
|
+
"spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb",
|
103
|
+
"spec/rails_best_practices/checks/use_before_filter_check_spec.rb",
|
104
|
+
"spec/rails_best_practices/checks/law_of_demeter_check_spec.rb",
|
105
|
+
"spec/rails_best_practices/checks/use_scope_access_check_spec.rb",
|
109
106
|
"spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb",
|
107
|
+
"spec/rails_best_practices/checks/not_use_default_route_check_spec.rb",
|
108
|
+
"spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb",
|
109
|
+
"spec/rails_best_practices/checks/use_model_callback_check_spec.rb",
|
110
|
+
"spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb",
|
110
111
|
"spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb",
|
111
|
-
"spec/rails_best_practices/checks/use_before_filter_check_spec.rb",
|
112
112
|
"spec/rails_best_practices/checks/use_model_association_check_spec.rb",
|
113
|
-
"spec/rails_best_practices/checks/
|
114
|
-
"spec/rails_best_practices/checks/
|
115
|
-
"spec/rails_best_practices/checks/
|
113
|
+
"spec/rails_best_practices/checks/move_code_into_model_check_spec.rb",
|
114
|
+
"spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb",
|
115
|
+
"spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb",
|
116
116
|
"spec/spec_helper.rb"
|
117
117
|
]
|
118
118
|
|
@@ -7,317 +7,50 @@ describe RailsBestPractices::Checks::AlwaysAddDbIndexCheck do
|
|
7
7
|
|
8
8
|
it "should always add db index" do
|
9
9
|
content = <<-EOF
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
t.integer :user_id
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.down
|
20
|
-
drop_table "comments"
|
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"
|
21
15
|
end
|
22
16
|
end
|
23
17
|
EOF
|
24
|
-
@runner.check('db/
|
25
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
18
|
+
@runner.check('db/schema.rb', content)
|
26
19
|
errors = @runner.errors
|
27
20
|
errors.should_not be_empty
|
28
|
-
errors[0].to_s.should == "db/
|
29
|
-
errors[1].to_s.should == "db/
|
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)"
|
30
23
|
end
|
31
|
-
|
24
|
+
|
32
25
|
it "should always add db index with column has no id" do
|
33
26
|
content = <<-EOF
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
t.integer :position
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.down
|
43
|
-
drop_table "comments"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
EOF
|
47
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
48
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
49
|
-
errors = @runner.errors
|
50
|
-
errors.should be_empty
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should always add db index with references" do
|
54
|
-
content = <<-EOF
|
55
|
-
class CreateComments < ActiveRecord::Migration
|
56
|
-
def self.up
|
57
|
-
create_table "comments", :force => true do |t|
|
58
|
-
t.string :content
|
59
|
-
t.references :post
|
60
|
-
t.references :user
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.down
|
65
|
-
drop_table "comments"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
EOF
|
69
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
70
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
71
|
-
errors = @runner.errors
|
72
|
-
errors.should_not be_empty
|
73
|
-
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:5 - always add db index (comments => post_id)"
|
74
|
-
errors[1].to_s.should == "db/migrate/20090918130258_create_comments.rb:6 - always add db index (comments => user_id)"
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should always add db index with column" do
|
78
|
-
content = <<-EOF
|
79
|
-
class CreateComments < ActiveRecord::Migration
|
80
|
-
def self.up
|
81
|
-
create_table "comments", :force => true do |t|
|
82
|
-
t.string :content
|
83
|
-
t.column :post_id, :integer
|
84
|
-
t.column :user_id, :integer
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.down
|
89
|
-
drop_table "comments"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
EOF
|
93
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
94
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
95
|
-
errors = @runner.errors
|
96
|
-
errors.should_not be_empty
|
97
|
-
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:5 - always add db index (comments => post_id)"
|
98
|
-
errors[1].to_s.should == "db/migrate/20090918130258_create_comments.rb:6 - always add db index (comments => user_id)"
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should not always add db index with add_index" do
|
102
|
-
content = <<-EOF
|
103
|
-
class CreateComments < ActiveRecord::Migration
|
104
|
-
def self.up
|
105
|
-
create_table "comments", :force => true do |t|
|
106
|
-
t.string :content
|
107
|
-
t.integer :post_id
|
108
|
-
t.integer :user_id
|
109
|
-
end
|
110
|
-
|
111
|
-
add_index :comments, :post_id
|
112
|
-
add_index :comments, :user_id
|
113
|
-
end
|
114
|
-
|
115
|
-
def self.down
|
116
|
-
drop_table "comments"
|
117
|
-
end
|
118
|
-
end
|
119
|
-
EOF
|
120
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
121
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
122
|
-
errors = @runner.errors
|
123
|
-
errors.should be_empty
|
124
|
-
end
|
125
|
-
|
126
|
-
it "should not always add db index with add_index in another migration file" do
|
127
|
-
content = <<-EOF
|
128
|
-
class CreateComments < ActiveRecord::Migration
|
129
|
-
def self.up
|
130
|
-
create_table "comments", :force => true do |t|
|
131
|
-
t.string :content
|
132
|
-
t.integer :post_id
|
133
|
-
t.integer :user_id
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def self.down
|
138
|
-
drop_table "comments"
|
139
|
-
end
|
140
|
-
end
|
141
|
-
EOF
|
142
|
-
add_index_content = <<-EOF
|
143
|
-
class AddIndexesToComments < ActiveRecord::Migration
|
144
|
-
def self.up
|
145
|
-
add_index :comments, :post_id
|
146
|
-
add_index :comments, :user_id
|
147
|
-
end
|
148
|
-
end
|
149
|
-
EOF
|
150
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
151
|
-
@runner.check('db/migrate/20090919130260_add_indexes_to_comments.rb', add_index_content)
|
152
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
153
|
-
@runner.check('db/migrate/20090919130260_add_indexes_to_comments.rb', add_index_content)
|
154
|
-
errors = @runner.errors
|
155
|
-
errors.should be_empty
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should not always add db index with add_index in another migration file and a migration between them" do
|
159
|
-
content = <<-EOF
|
160
|
-
class CreateComments < ActiveRecord::Migration
|
161
|
-
def self.up
|
162
|
-
create_table "comments", :force => true do |t|
|
163
|
-
t.string :content
|
164
|
-
t.integer :post_id
|
165
|
-
t.integer :user_id
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def self.down
|
170
|
-
drop_table "comments"
|
171
|
-
end
|
172
|
-
end
|
173
|
-
EOF
|
174
|
-
another_content = <<-EOF
|
175
|
-
class Settings < ActiveRecord::Migration
|
176
|
-
def self.my_escape(val)
|
177
|
-
end
|
178
|
-
|
179
|
-
def self.up
|
180
|
-
add_column :settings, :groep, :string, :limit => 50
|
27
|
+
ActiveRecord::Schema.define(:version => 20100603080629) do
|
28
|
+
create_table "comments", :force => true do |t|
|
29
|
+
t.string "content"
|
30
|
+
t.integer "position"
|
181
31
|
end
|
182
32
|
end
|
183
33
|
EOF
|
184
|
-
|
185
|
-
class AddIndexesToComments < ActiveRecord::Migration
|
186
|
-
def self.up
|
187
|
-
add_index :comments, :post_id
|
188
|
-
add_index :comments, :user_id
|
189
|
-
end
|
190
|
-
end
|
191
|
-
EOF
|
192
|
-
@runner.check('db/migrate/20090918140258_create_comments.rb', content)
|
193
|
-
@runner.check('db/migrate/20090918140259_settings.rb', another_content)
|
194
|
-
@runner.check('db/migrate/20090919140260_add_indexes_to_comments.rb', add_index_content)
|
195
|
-
@runner.check('db/migrate/20090918140258_create_comments.rb', content)
|
196
|
-
@runner.check('db/migrate/20090918140259_settings.rb', another_content)
|
197
|
-
@runner.check('db/migrate/20090919140260_add_indexes_to_comments.rb', add_index_content)
|
34
|
+
@runner.check('db/schema.rb', content)
|
198
35
|
errors = @runner.errors
|
199
36
|
errors.should be_empty
|
200
37
|
end
|
201
|
-
|
202
|
-
it "should always add db index without error" do
|
203
|
-
content = <<-EOF
|
204
|
-
class AddIndexes < ActiveRecord::Migration
|
205
|
-
def self.up
|
206
|
-
[[:site_wide_admins, :admin_id, { :unique => true }],
|
207
|
-
[:photos, [:target_id, :target_type, :type]],
|
208
|
-
[:photos, [:target_id, :target_type, :parent_id, :is_avatar]],
|
209
|
-
[:category_assignments, [:category_id, :sub_category_id, :target_id, :target_type]],
|
210
|
-
[:network_connections, :user_id]].each do |args|
|
211
|
-
add_index(*args) rescue say "Failed to add index"
|
212
|
-
end
|
213
|
-
# raise "abort migration"
|
214
|
-
end
|
215
|
-
|
216
|
-
def self.down
|
217
|
-
end
|
218
|
-
end
|
219
|
-
EOF
|
220
|
-
@runner.check('db/migrate/20091111113612_add_indexes.rb', content)
|
221
|
-
@runner.check('db/migrate/20091111113612_add_indexes.rb', content)
|
222
|
-
errors = @runner.errors
|
223
|
-
end
|
224
|
-
|
225
|
-
it "should always add db index without error 2" do
|
226
|
-
content = <<-EOF
|
227
|
-
class MoveCategoriesToProfileArea < ActiveRecord::Migration
|
228
|
-
def self.up
|
229
|
-
CategoryArea.update_all({:name => "Profile"}, ["name IN (?)", %w(UserSite UserProfile Site)])
|
230
|
-
end
|
231
|
-
|
232
|
-
def self.down
|
233
|
-
end
|
234
|
-
end
|
235
|
-
EOF
|
236
|
-
@runner.check('db/migrate/20090706091635_move_categories_to_profile_area.rb', content)
|
237
|
-
@runner.check('db/migrate/20090706091635_move_categories_to_profile_area.rb', content)
|
238
|
-
errors = @runner.errors
|
239
|
-
end
|
240
|
-
|
241
|
-
it "should always add db index without duplicate error outputs" do
|
242
|
-
content = <<-EOF
|
243
|
-
class AllTables < ActiveRecord::Migration
|
244
|
-
def self.up
|
245
|
-
create_table "ducks" do |t|
|
246
|
-
t.column "registration", :string, :limit => 32
|
247
|
-
t.column "description", :string
|
248
|
-
end
|
249
|
-
|
250
|
-
create_table "lab_data" do |t|
|
251
|
-
t.integer "input_biologist_id", :null => true
|
252
|
-
t.integer "owner_biologist_id", :null => false
|
253
|
-
t.column "remark", :string, :limit => 250
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
EOF
|
258
|
-
@runner.check('db/migrate/20090204160203_all_tables.rb', content)
|
259
|
-
@runner.check('db/migrate/20090204160203_all_tables.rb', content)
|
260
|
-
errors = @runner.errors
|
261
|
-
errors.should_not be_empty
|
262
|
-
errors[0].to_s.should == "db/migrate/20090204160203_all_tables.rb:9 - always add db index (lab_data => input_biologist_id)"
|
263
|
-
errors[1].to_s.should == "db/migrate/20090204160203_all_tables.rb:10 - always add db index (lab_data => owner_biologist_id)"
|
264
|
-
end
|
265
38
|
|
266
|
-
it "should not always add db index
|
39
|
+
it "should not always add db index with add_index" do
|
267
40
|
content = <<-EOF
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
t.integer :user_id
|
274
|
-
end
|
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"
|
275
46
|
end
|
276
47
|
|
277
|
-
|
278
|
-
|
279
|
-
end
|
48
|
+
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
|
49
|
+
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
|
280
50
|
end
|
281
51
|
EOF
|
282
|
-
|
283
|
-
class DropComments < ActiveRecord::Migration
|
284
|
-
def self.up
|
285
|
-
drop_table "comments"
|
286
|
-
end
|
287
|
-
|
288
|
-
def self.down
|
289
|
-
end
|
290
|
-
end
|
291
|
-
EOF
|
292
|
-
@runner.check('db/migrate/20100118140258_create_comments.rb', content)
|
293
|
-
@runner.check('db/migrate/20100118140259_drop_comments.rb', another_content)
|
294
|
-
@runner.check('db/migrate/20100118140258_create_comments.rb', content)
|
295
|
-
@runner.check('db/migrate/20100118140259_drop_comments.rb', another_content)
|
52
|
+
@runner.check('db/schema.rb', content)
|
296
53
|
errors = @runner.errors
|
297
54
|
errors.should be_empty
|
298
55
|
end
|
299
|
-
|
300
|
-
it "should always add db index when add_column" do
|
301
|
-
content = <<-EOF
|
302
|
-
class CreateComments < ActiveRecord::Migration
|
303
|
-
def self.up
|
304
|
-
create_table "comments", :force => true do |t|
|
305
|
-
t.string :content
|
306
|
-
end
|
307
|
-
add_column :comments, :post_id, :integer
|
308
|
-
add_column :comments, :user_id, :integer
|
309
|
-
end
|
310
|
-
|
311
|
-
def self.down
|
312
|
-
drop_table "comments"
|
313
|
-
end
|
314
|
-
end
|
315
|
-
EOF
|
316
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
317
|
-
@runner.check('db/migrate/20090918130258_create_comments.rb', content)
|
318
|
-
errors = @runner.errors
|
319
|
-
errors.should_not be_empty
|
320
|
-
errors[0].to_s.should == "db/migrate/20090918130258_create_comments.rb:7 - always add db index (comments => post_id)"
|
321
|
-
errors[1].to_s.should == "db/migrate/20090918130258_create_comments.rb:8 - always add db index (comments => user_id)"
|
322
|
-
end
|
323
56
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 20
|
9
|
+
version: 0.3.20
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Richard Huang
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03
|
17
|
+
date: 2010-06-03 00:00:00 +08:00
|
18
18
|
default_executable: rails_best_practices
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -151,26 +151,26 @@ signing_key:
|
|
151
151
|
specification_version: 3
|
152
152
|
summary: check rails files according to ihower's presentation 'rails best practices'
|
153
153
|
test_files:
|
154
|
-
- spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
|
155
|
-
- spec/rails_best_practices/checks/always_add_db_index_check_spec.rb
|
156
154
|
- spec/rails_best_practices/checks/isolate_seed_data_check_spec.rb
|
157
|
-
- spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
|
158
|
-
- spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
|
159
|
-
- spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
|
160
|
-
- spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb
|
161
|
-
- spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb
|
162
|
-
- spec/rails_best_practices/checks/move_code_into_model_check_spec.rb
|
163
|
-
- spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
|
164
155
|
- spec/rails_best_practices/checks/move_model_logic_into_model_check_spec.rb
|
165
|
-
- spec/rails_best_practices/checks/
|
166
|
-
- spec/rails_best_practices/checks/
|
167
|
-
- spec/rails_best_practices/checks/
|
156
|
+
- spec/rails_best_practices/checks/move_code_into_helper_check_spec.rb
|
157
|
+
- spec/rails_best_practices/checks/move_code_into_controller_check_spec.rb
|
158
|
+
- spec/rails_best_practices/checks/use_observer_check_spec.rb
|
168
159
|
- spec/rails_best_practices/checks/overuse_route_customizations_check_spec.rb
|
160
|
+
- spec/rails_best_practices/checks/nested_model_forms_check_spec.rb
|
161
|
+
- spec/rails_best_practices/checks/always_add_db_index_check_spec.rb
|
162
|
+
- spec/rails_best_practices/checks/many_to_many_collection_check_spec.rb
|
163
|
+
- spec/rails_best_practices/checks/use_before_filter_check_spec.rb
|
164
|
+
- spec/rails_best_practices/checks/law_of_demeter_check_spec.rb
|
165
|
+
- spec/rails_best_practices/checks/use_scope_access_check_spec.rb
|
169
166
|
- spec/rails_best_practices/checks/replace_complex_creation_with_factory_method_check_spec.rb
|
167
|
+
- spec/rails_best_practices/checks/not_use_default_route_check_spec.rb
|
168
|
+
- spec/rails_best_practices/checks/move_finder_to_named_scope_check_spec.rb
|
169
|
+
- spec/rails_best_practices/checks/use_model_callback_check_spec.rb
|
170
|
+
- spec/rails_best_practices/checks/add_model_virtual_attribute_check_spec.rb
|
170
171
|
- spec/rails_best_practices/checks/replace_instance_variable_with_local_variable_check_spec.rb
|
171
|
-
- spec/rails_best_practices/checks/use_before_filter_check_spec.rb
|
172
172
|
- spec/rails_best_practices/checks/use_model_association_check_spec.rb
|
173
|
-
- spec/rails_best_practices/checks/
|
174
|
-
- spec/rails_best_practices/checks/
|
175
|
-
- spec/rails_best_practices/checks/
|
173
|
+
- spec/rails_best_practices/checks/move_code_into_model_check_spec.rb
|
174
|
+
- spec/rails_best_practices/checks/needless_deep_nesting_check_spec.rb
|
175
|
+
- spec/rails_best_practices/checks/keep_finders_on_their_own_model_check_spec.rb
|
176
176
|
- spec/spec_helper.rb
|