Created
August 31, 2013 04:16
-
-
Save kyamaguchi/6396196 to your computer and use it in GitHub Desktop.
Migration to create tables using CoreData file
This file contains 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
# gem 'core_data' | |
class CreateModelsFromCoreData < ActiveRecord::Migration | |
TYPE_MAP = { | |
"Boolean" => 'boolean', | |
"Date" => 'datetime', | |
"Integer 32" => 'integer', | |
"String" => 'string', | |
} | |
def up | |
schema = CoreData::DataModel.new("#{Rails.root}/db/data.xcdatamodeld") | |
entities = schema.entities | |
entities.each do |entity| | |
create_table entity.name.tableize.to_sym do |t| | |
entity.attributes.each do |attribute| | |
t.send(TYPE_MAP[attribute.type], attribute.name.underscore.to_sym) | |
end | |
entity.relationships.each do |relationship| | |
t.send('integer', "#{relationship.name.underscore}_id".to_sym) if relationship.to_one? | |
end | |
t.timestamps | |
end | |
end | |
end | |
def down | |
schema = CoreData::DataModel.new("#{Rails.root}/db/sekken.xcdatamodeld") | |
entities = schema.entities | |
entities.each do |entity| | |
drop_table entity.name.tableize.to_sym | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment