rails_doctor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/lib/rails_doctor.rb +81 -0
- data/lib/rails_doctor/version.rb +3 -0
- data/rails_doctor.gemspec +21 -0
- data/spec/rails_doctor_spec.rb +54 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Dmitry Gruzd
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# RailsDoctor
|
2
|
+
|
3
|
+
Gem is used to find troubles in rails code, e.g. associations without index
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails_doctor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails_doctor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
doctor = RailsDoctor.new
|
22
|
+
doctor.analize
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/rails_doctor.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rails_doctor/version'
|
2
|
+
class RailsDoctor
|
3
|
+
def initialize
|
4
|
+
end
|
5
|
+
|
6
|
+
def analize
|
7
|
+
indexes
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
def associations
|
12
|
+
hash = {}
|
13
|
+
models = Dir.glob("#{Rails.root}/app/models/*.rb").map{|model_path| model_path.split('/').last[/.+(?=\.rb)/]}
|
14
|
+
#models = ['patient', 'tooth_formula']
|
15
|
+
models.each do |model_name|
|
16
|
+
table = model_name.pluralize.to_sym
|
17
|
+
hash[table]||=[]
|
18
|
+
model = model_name.split('_').map(&:capitalize).join.constantize
|
19
|
+
next unless model.respond_to?(:reflect_on_all_associations)
|
20
|
+
belongs_to = model.reflect_on_all_associations.select{|s| s.macro == :belongs_to}
|
21
|
+
ids = belongs_to.map{|a| "#{a.name}_id"}
|
22
|
+
hash[table] += ids
|
23
|
+
end
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def indexes
|
28
|
+
hash = {}
|
29
|
+
#puts 'run associations'
|
30
|
+
assoc = associations
|
31
|
+
#puts "get #{assoc.inspect}"
|
32
|
+
#puts 'run migrations'
|
33
|
+
migrate = migrations
|
34
|
+
#puts "get #{migrate.inspect}"
|
35
|
+
|
36
|
+
tables = assoc.keys&migrate.keys
|
37
|
+
tables.each do |table|
|
38
|
+
columns = assoc[table]-migrate[table]
|
39
|
+
unless columns.blank?
|
40
|
+
hash[table] = columns
|
41
|
+
puts "WARNING: in table:#{table} is no indexes in columns #{columns.join(', ')}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
create_migration(hash)
|
45
|
+
hash
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_migration(hash)
|
49
|
+
return false if hash.blank?
|
50
|
+
puts "###### migrations #######"
|
51
|
+
hash.each do |table, columns|
|
52
|
+
columns.each do |column|
|
53
|
+
puts "add_index #{table.inspect}, #{column.inspect}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
puts "###### migrations end ###"
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def migrations
|
61
|
+
hash = {}
|
62
|
+
file_list = Dir.glob("#{Rails.root}/db/migrate/*.rb")
|
63
|
+
file_list.each do |file|
|
64
|
+
parse = parse_migration(File.read(file))
|
65
|
+
hash.merge! parse
|
66
|
+
end
|
67
|
+
hash
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
def parse_migration(text)
|
72
|
+
hash = {}
|
73
|
+
scan = text.scan(/^\s*add_index\s+:?(?<table>\w+),\s*:?(?<column>\w+)/)
|
74
|
+
scan.each do |s|
|
75
|
+
table = s.first.to_sym
|
76
|
+
hash[table]||=[]
|
77
|
+
hash[table] << s.last
|
78
|
+
end
|
79
|
+
hash
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_doctor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rails_doctor"
|
8
|
+
gem.version = RailsDoctor::VERSION
|
9
|
+
gem.authors = ["Dmitry Gruzd"]
|
10
|
+
gem.email = ["donotsendhere@gmail.com"]
|
11
|
+
gem.description = %q{ gem detect problems in your rails project}
|
12
|
+
gem.summary = %q{ rails_doctor gem that can help you to find errors in different ways e.g. if you forgot to add indexes on association column}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rails_doctor'
|
2
|
+
|
3
|
+
|
4
|
+
describe "Test migration parse" do
|
5
|
+
before(:all) do
|
6
|
+
@doctor = RailsDoctor.new
|
7
|
+
end
|
8
|
+
it 'check simple migration' do
|
9
|
+
migration = <<EOF
|
10
|
+
class CreateUsers < ActiveRecord::Migration
|
11
|
+
def change
|
12
|
+
add_index :users, :role_id
|
13
|
+
add_index :users, :other_id
|
14
|
+
end
|
15
|
+
end
|
16
|
+
EOF
|
17
|
+
@doctor.send(:parse_migration, migration).should == {:users =>['role_id', 'other_id']}
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'check up-down migration' do
|
21
|
+
migration = <<EOF
|
22
|
+
class CreateUsers < ActiveRecord::Migration
|
23
|
+
def up
|
24
|
+
add_index :users, :role_id
|
25
|
+
add_index :users, :other_id
|
26
|
+
end
|
27
|
+
def down
|
28
|
+
remove_index :users, :role_id
|
29
|
+
remove_index :users, :other_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
EOF
|
33
|
+
@doctor.send(:parse_migration, migration).should == {:users =>['role_id', 'other_id']}
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
it 'check up-down reverted migration' do
|
38
|
+
migration = <<EOF
|
39
|
+
class CreateUsers < ActiveRecord::Migration
|
40
|
+
def down
|
41
|
+
add_index :users, :role_id
|
42
|
+
add_index :users, :other_id
|
43
|
+
end
|
44
|
+
def up
|
45
|
+
remove_index :users, :role_id
|
46
|
+
remove_index :users, :other_id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
EOF
|
50
|
+
@doctor.send(:parse_migration, migration).should == {}
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_doctor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmitry Gruzd
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! ' gem detect problems in your rails project'
|
31
|
+
email:
|
32
|
+
- donotsendhere@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/rails_doctor.rb
|
43
|
+
- lib/rails_doctor/version.rb
|
44
|
+
- rails_doctor.gemspec
|
45
|
+
- spec/rails_doctor_spec.rb
|
46
|
+
homepage: ''
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: rails_doctor gem that can help you to find errors in different ways e.g.
|
70
|
+
if you forgot to add indexes on association column
|
71
|
+
test_files:
|
72
|
+
- spec/rails_doctor_spec.rb
|