Created
February 3, 2012 03:01
-
-
Save jasonkarns/1727371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script greps the code base for all associations with an explicit foreign_key. | |
# For each defined association, it attempts to infer the foreign_key using our inflection rules. | |
# If the inferred foreign_key matches the explicit foreign_key, then the foreign_key declaration can be removed. | |
# If not, then there is the potential that the inflection can be added to config/initializers/inflections | |
# (unless it is an exception to an already-defined initializer inflection) | |
# USAGE: run from the command line | |
# $ ruby find_redundant_foreign_keys.rb | |
root = File.expand_path(File.dirname(__FILE__) + '/../') | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'iktiv_record' | |
require 'logger' | |
require File.expand_path(root + '/config/initializers/inflections') | |
class ForeignKeyDeclaration | |
def initialize(defined, class_name) | |
@defined, @class_name = defined, class_name | |
end | |
def verify | |
unless composite_key? | |
puts "#{redundant?.to_s.upcase}: :#{@class_name}, :foreign_key => :#{@defined} (derived as :#{derived_foreign_key.downcase})" | |
end | |
end | |
private | |
def composite_key? | |
@defined.is_a? Array | |
end | |
def redundant? | |
(derived_foreign_key.upcase == @defined.to_s.upcase)? :redundant : :defined | |
end | |
def derived_foreign_key | |
@class_name.to_s.foreign_key | |
end | |
end | |
def belongs_to(assoc, hash) | |
ForeignKeyDeclaration.new(hash[:foreign_key], assoc).verify | |
end | |
def has_many(assoc, hash) | |
ForeignKeyDeclaration.new(hash[:foreign_key], hash[:filename]).verify | |
end | |
def has_one(assoc, hash) | |
ForeignKeyDeclaration.new(hash[:foreign_key], hash[:filename]).verify | |
end | |
def set_table_name(table_name, class_name) | |
redundant = (class_name.to_s.pluralize == table_name.to_s)? :redundant : :defined | |
puts "#{redundant.to_s.upcase}: #{class_name.to_s.classify}, set_table_name :#{table_name} (derived as :#{class_name.to_s.pluralize})" | |
end | |
puts "\n=== belongs_to ===" | |
eval `grep -hr -e "belongs_to .* :foreign_key" #{root}/models/ | sed -e 's/^ *//' -e 's/^base\\.//'` | |
puts "\n=== has_many ===" | |
eval `grep -r -e "has_many .* :foreign_key" #{root}/models/ | sed -e 's/^.*\\/models\\/[a-z\\/]*\\/\\([a-z_0-9]*\\).rb\\(.*\\)has_many\\(.*\\)\\(=.\\)\\(.*\\)/has_many\\3\\4\\5, :filename \\4 :\\1/'` | |
puts "\n=== has_one ===" | |
eval `grep -r -e "has_one .* :foreign_key" #{root}/models/ | sed -e 's/^.*\\/models\\/[a-z\\/]*\\/\\([a-z_0-9]*\\).rb\\(.*\\)has_one\\(.*\\)\\(=.\\)\\(.*\\)/has_one\\3\\4\\5, :filename \\4 :\\1/'` | |
puts "\n=== set_table_name ===" | |
eval `grep -r -e "set_table_name .*" #{root}/models/ | sed -e 's/^.*\\/models\\/[a-z\\/]*\\/\\([a-z_0-9]*\\).rb\\(.*\\)set_table_name\\(.*\\)/set_table_name\\3, :\\1/'` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment