Created
February 21, 2011 19:10
-
-
Save electrum/837535 to your computer and use it in GitHub Desktop.
Validate ActiveRecord models
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
| namespace :models do | |
| desc 'Check models for problems' | |
| task :check => :environment do | |
| Dir.glob(RAILS_ROOT + '/app/models/**/*.rb').each do |f| | |
| model = eval File.basename(f)[0..-4].camelize | |
| next unless model.superclass == ActiveRecord::Base # TODO: handle inheritance | |
| print "#{model.name}: " | |
| if model.column_names.exclude? model.primary_key | |
| abort "bad primary key: #{model.primary_key}" | |
| end | |
| model.reflect_on_all_associations.map {|i| i.name.to_s }.each do |association| | |
| begin | |
| eval "model.new.#{association}" | |
| rescue NameError | |
| abort "bad association: #{association}" | |
| end | |
| end | |
| puts "ok" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment