Skip to content

Commit

Permalink
Merge pull request #22 from deviantech/master
Browse files Browse the repository at this point in the history
Added rescue blocks to allow some useful output even when there are some errors
  • Loading branch information
Elad Meidar committed Oct 15, 2012
2 parents 1bc8703 + aa91ad3 commit ca2d30c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.textile
Expand Up @@ -2,7 +2,7 @@ h1. Rails Indexes

Rails indexes is a small package of 2 rake tasks that scan your application models and displays a list of columns that _probably_ should be indexed.

note: there should be mode fields depending on your application design and custom queries.
Note: there may be more fields depending on your application design and custom queries.

h2. Installation

Expand Down
30 changes: 20 additions & 10 deletions lib/indexer.rb
Expand Up @@ -160,6 +160,8 @@ def self.scan_finds
# Check line for find* methods (include find_all, find_by and just find)
def self.check_line_for_find_indexes(file_name, line)

# TODO: Chokes on finders called on associations, e.g. current_user.widgets.find( params[:widget_id] )

# TODO: Assumes that you have a called on #find. you can actually call #find without a caller in a model code. ex:
# def something
# find(self.id)
Expand All @@ -174,16 +176,20 @@ def self.check_line_for_find_indexes(file_name, line)

model_name, column_names, options = matches[2], matches[7], matches[8]

# if the finder class is "self" or empty (can be a simple "find()" in a model)
if model_name == "self" || model_name.blank?
model_name = File.basename(file_name).sub(/\.rb$/,'').camelize
table_name = model_name.constantize.table_name
else
if model_name.respond_to?(:constantize)
if model_name.constantize.respond_to?(:table_name)
table_name = model_name.constantize.table_name
begin
# if the finder class is "self" or empty (can be a simple "find()" in a model)
if model_name == "self" || model_name.blank?
model_name = File.basename(file_name).sub(/\.rb$/,'').camelize
table_name = model_name.constantize.table_name
else
if model_name.respond_to?(:constantize)
if model_name.constantize.respond_to?(:table_name)
table_name = model_name.constantize.table_name
end
end
end
rescue
puts "ERROR: #{e} in #{line}"
end

# Check that all prerequisites are met
Expand Down Expand Up @@ -213,8 +219,12 @@ def self.check_line_for_find_indexes(file_name, line)
end

def self.key_exists?(table,key_columns)
result = (key_columns.to_a - ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten)
result.empty?
begin
result = (key_columns.to_a - ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten)
result.empty?
rescue Exception => e
puts "ERROR: #{e} for table #{table}"
end
end

def self.simple_migration
Expand Down

0 comments on commit ca2d30c

Please sign in to comment.