require 'find' namespace :db do desc "Creates and populates the initial BrowserCMS database for a new project." task :install => ["db:create", "db:migrate", "db:seed"] desc "Drop, create and migrate the database" task :redo => ["db:drop", "db:create", "db:migrate"] namespace :seed do desc "Load the seed data from db/browsercms.seeds.rb" task :browsercms => :environment do load File.join("db", "browsercms.seeds.rb") end end namespace :fixtures do desc 'Dumps all models into fixtures.' task :dump => :environment do models = [] Find.find(Rails.root + '/app/models') do |path| unless File.directory?(path) then models << path.match(/(\w+).rb/)[1] end end models -= %w[content_observer guest_user file_block image_block email_message_mailer] puts "Models: " + models.join(', ') models.each do |m| model = m.tableize.classify.constantize create_fixture(model) create_fixture(model.version_class) if model.versioned? end end def create_fixture(model) puts "Creating fixture for #{model.table_name}" entries = model.find(:all, :order => 'id ASC') formatted, increment, tab = '', 1, ' ' entries.each do |a| formatted += model.table_name.singularize + '_' + increment.to_s + ':' + "\n" increment += 1 a.attributes.each do |column, value| formatted += tab match = value.to_s.match(/\n/) if match formatted += column + ': |' + "\n" value.to_a.each do |v| formatted += tab + tab + v end else formatted += column + ': ' + value.to_s end formatted += "\n" end formatted += "\n" end model_file = Rails.root + '/test/fixtures/' + model.table_name + '.yml' # Final munging of file formatted = "# auto-generated by rake db:fixtures:dump, DO NOT EDIT BY HAND!\n#{formatted}" # Scrub ERB formatted.gsub!('<% ', ',<%% ') formatted.gsub!('<%= ', ',<%%= ') formatted.gsub!(' %>', ' %%>') File.exists?(model_file) ? File.delete(model_file) : nil File.open(model_file, 'w') {|f| f << formatted} end end end