Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Override auto_migrate! and auto_upgrade!, to create the resources, if…
Browse files Browse the repository at this point in the history
… DataMapper::Migrations is included in the model.
  • Loading branch information
postmodern committed Jul 25, 2011
1 parent 648f66b commit 655381c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/dm-is-predefined/is/predefined.rb
Expand Up @@ -6,8 +6,59 @@ module Predefined
#
# Fired when your plugin gets included into a Model.
#
# @note
# If the model already includes `DataMapper::Migrations`, then
# {MigrationMethods} will be extended into the Model.
#
def is_predefined
extend DataMapper::Is::Predefined::ClassMethods

if defined?(DataMapper::Migrations) &&
included_modules.include?(DataMapper::Migrations)
extend MigrationMethods
end
end

module MigrationMethods
#
# Auto-migrates the model, then creates all predefined resources.
#
# @param [Symbol] repository_name
# The repository to perform the migrations within.
#
# @return [true]
#
def auto_migrate!(repository_name=self.repository_name)
result = super(repository_name)

DataMapper.repository(repository_name) do
predefined_attributes.each_value do |attributes|
create(attributes)
end
end

return result
end

#
# Auto-upgrades the model, then creates any missing predefined resources.
#
# @param [Symbol] repository_name
# The repository to perform the upgrade within.
#
# @return [true]
#
def auto_upgrade!(repository_name=self.repository_name)
result = super(repository_name)

DataMapper.repository(repository_name) do
predefined_attributes.each_value do |attributes|
first_or_create(attributes)
end
end

return result
end
end

module ClassMethods
Expand Down
35 changes: 35 additions & 0 deletions spec/integration/predefined_spec.rb
@@ -1,11 +1,46 @@
require 'spec_helper'
require 'integration/models/test_model'
require 'integration/models/migrated_model'

describe DataMapper::Is::Predefined do
before(:all) do
TestModel.auto_migrate!
end

context "migrations" do
it "should extend MigrationMethods, if Migrations are included" do
TestModel.should_not be_kind_of(DataMapper::Is::Predefined::MigrationMethods)
MigratedModel.should be_kind_of(DataMapper::Is::Predefined::MigrationMethods)
end

it "should create all predefined resources after auto_migrate!" do
MigratedModel.auto_migrate!

resources = MigratedModel.all

resources[0].id.should == 1
resources[0].name.should == 'test1'

resources[1].id.should == 2
resources[1].name.should == 'test2'
end

it "should create missing predefined resources after auto_upgrade!" do
MigratedModel.first(:name => 'test2').destroy!

MigratedModel.property :extra, String
MigratedModel.auto_upgrade!

resources = MigratedModel.all

resources[0].id.should == 1
resources[0].name.should == 'test1'

resources[1].id.should == 3
resources[1].name.should == 'test2'
end
end

it "should define the @predefined_attributes instance variable" do
TestModel.should be_instance_variable_defined(:@predefined_attributes)
end
Expand Down

0 comments on commit 655381c

Please sign in to comment.