Posts Tagged ‘rails models’
rails data migrations, part 2
October 11th, 2007 •
tags: playground, rails, rails models
class DataMigration < ActiveRecord::Migration
class << self
def fixtures(file_name, options = { :folder => 'data' })
@file_name, @folder_name = file_name, options[:folder]
end
def migrate(direction)
super(direction) # perform migration
if direction == :up && valid_options_present?
require 'active_record/fixtures'
data_migration_path = "#{RAILS_ROOT}/db/migrate/#{@folder_name}"
Fixtures.create_fixtures(data_migration_path, @file_name.to_s)
say "DataMigration INFO"
say "Loaded #{data_migration_path}/#{@file_name.to_s}.yml", true
end
rescue Errno::ENOENT => e
say "Snusnu::DataMigration WARNING"
say e.message, true
end
private
def valid_options_present?
[ @file_name, @folder_name ].all? do |v|
v && (v.is_a?(String) || v.is_a?(Symbol))
end
end
end
end
class CreateMyMigration < DataMigration
fixtures :people #, :folder => "fixtures"
def self.up
create_table :people, :force => true do |t|
#...
end
end
end
rails data migrations, part 1
October 11th, 2007 •
tags: playground, rails, rails models
class DataMigration < ActiveRecord::Migration
class << self
ERROR = 'Snusnu::DataMigration ERROR'
WARNING = 'Snusnu::DataMigration WARNING'
INFO = 'Snusnu::DataMigration INFO'
def load_fixtures(file_name, fixtures_folder = 'data')
unless file_name.is_a?(String) || file_name.is_a?(Symbol)
raise ArgumentError, "#{ERROR} - file_name must be String or Symbol"
end
require 'active_record/fixtures'
data_migration_path = "#{RAILS_ROOT}/db/migrate/#{fixtures_folder}"
Fixtures.create_fixtures(data_migration_path, file_name.to_s)
say "#{INFO}"
say "Successfully loaded #{data_migration_path}/#{file_name.to_s}.yml", true
rescue Errno::ENOENT => e
say "#{WARNING}"
say e.message, true
end
end
end
class CreateMyMigration < Snusnu::DataMigration
def self.up
create_table :people, :force => true do |t|
#...
end
load_fixtures :people #, :folder => "fixtures"
end
end