Permalink
Browse files

Override auto_migrate! and auto_upgrade!, to create the resources, if…

… DataMapper::Migrations is included in the model.
  • Loading branch information...
1 parent 648f66b commit 655381c3a0693666ef80477efb7d66a181985ea2 @postmodern committed Jul 25, 2011
Showing with 86 additions and 0 deletions.
  1. +51 −0 lib/dm-is-predefined/is/predefined.rb
  2. +35 −0 spec/integration/predefined_spec.rb
@@ -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
@@ -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

0 comments on commit 655381c

Please sign in to comment.