# Copyright (c) 2006-2007 Kelly McCauley # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. namespace :model do desc "Load the given CSV fixtures into the #{RAILS_ENV} database. Usage: SRC=path FIXTURES=x[,y,...]" task :load_from_csv => :environment do if ENV['SRC'].blank? raise 'No SRC value given. Set SRC=/some/path/containing/fixture/files' end require 'active_record/fixtures' ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(ENV['SRC'], '*.csv')).each do |fixture_file| Fixtures.create_fixtures(ENV['SRC'], File.basename(fixture_file, '.*')) end end desc "Dump some or all of a model's data to a CSV file in the given destination directory. Usage: MODEL=ModelName[,ModelName,...] DEST=path [LIMIT=100] " task :dump_to_csv => :environment do if ENV['MODEL'].blank? raise "No MODEL value given. Set MODEL=ModelName" else models = ENV['MODEL'].split(/,/) if ENV['LIMIT'].blank? if ENV['DEST'].blank? raise "No DEST value given. Set DEST=/some/path" else # Dumping to a destination path models.each {|m| eval "#{m.strip}.to_csv('#{ENV['DEST']}')" } end else if ENV['DEST'].blank? raise "No DEST value given. Set DEST=/some/path" else # Dumping to a file models.each {|m| eval "#{m.strip}.to_csv('#{ENV['DEST']}', {:limit => #{ENV['LIMIT']}})" } end end end end desc "Load the given fixtures into the #{RAILS_ENV} database. Usage: SRC=path FIXTURES=x[,y,...]" task :load_from_fixture => :environment do if ENV['SRC'].blank? raise 'No SRC value given. Set SRC=/some/path/containing/fixture/files' end require 'active_record/fixtures' ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym) (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(ENV['SRC'], '*.yml')).each do |fixture_file| Fixtures.create_fixtures(ENV['SRC'], File.basename(fixture_file, '.*')) end end desc "Dump some or all of a model's data to a YAML fixture file in the given destination directory. Usage: MODEL=ModelName[,ModelName,...] DEST=path [LIMIT=100]" task :dump_to_fixture => :environment do if ENV['MODEL'].blank? raise "No MODEL value given. Set MODEL=ModelName" else models = ENV['MODEL'].split(/,/) if ENV['LIMIT'].blank? if ENV['DEST'].blank? raise "No DEST value given. Set DEST=/some/path" else # Dumping to a destination path models.each {|m| eval "#{m.strip}.to_fixture('#{ENV['DEST']}')" } end else if ENV['DEST'].blank? raise "No DEST value given. Set DEST=/some/path" else # Dumping to a file models.each {|m| eval "#{m.strip}.to_fixture('#{ENV['DEST']}', {:limit => #{ENV['LIMIT']}})" } end end end end end