Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Fixed Model inclusion/extension appending to work with defined models
Browse files Browse the repository at this point in the history
* Added logic to Model#append_inclusions to add inclusions to all
  existing Model descendants
* Added logic to Model#append_extensions to add extensions to all
  existing Model descendants
* Added specs for behavior of Model.append_extensions and .append_inclusions

[#1034 state:resolved]

Signed-off-by: Dan Kubb <dan.kubb@gmail.com>
  • Loading branch information
Blake Gentry authored and dkubb committed Nov 10, 2009
1 parent afec5ad commit a09fa1e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/dm-core/model.rb
Expand Up @@ -86,6 +86,12 @@ def self.descendants
# @api semipublic
def self.append_inclusions(*inclusions)
extra_inclusions.concat inclusions

# Add the inclusion to existing descendants
descendants.each do |model|
inclusions.each { |inclusion| model.send :include, inclusion }
end

true
end

Expand All @@ -111,6 +117,12 @@ def self.extra_inclusions
# @api semipublic
def self.append_extensions(*extensions)
extra_extensions.concat extensions

# Add the extension to existing descendants
descendants.each do |model|
extensions.each { |extension| model.extend(extension) }
end

true
end

Expand Down
96 changes: 96 additions & 0 deletions spec/semipublic/model_spec.rb
@@ -0,0 +1,96 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

describe DataMapper::Model do

it { should respond_to(:append_inclusions) }

describe '.append_inclusions' do
module ::Inclusions
def new_method
end
end

describe 'before the model is defined' do
before :all do
DataMapper::Model.append_inclusions(Inclusions)

class ::User
include DataMapper::Resource
property :id, Serial
end
end

it 'should respond to :new_method' do
User.new.should respond_to(:new_method)
end

after :all do
DataMapper::Model.extra_inclusions.delete(Inclusions)
end
end

describe 'after the model is defined' do
before :all do
class ::User
include DataMapper::Resource
property :id, Serial
end
DataMapper::Model.append_inclusions(Inclusions)
end

it 'should respond to :new_method' do
User.new.should respond_to(:new_method)
end

after :all do
DataMapper::Model.extra_inclusions.delete(Inclusions)
end
end
end

it { should respond_to(:append_extensions) }

describe '.append_extensions' do
module ::Extensions
def new_method
end
end

describe 'before the model is defined' do
before :all do
DataMapper::Model.append_extensions(Extensions)

class ::User
include DataMapper::Resource
property :id, Serial
end
end

it 'should respond to :new_method' do
User.should respond_to(:new_method)
end

after :all do
DataMapper::Model.extra_extensions.delete(Extensions)
end
end

describe 'after the model is defined' do
before :all do
class ::User
include DataMapper::Resource
property :id, Serial
end
DataMapper::Model.append_extensions(Extensions)
end

it 'should respond to :new_method' do
User.should respond_to(:new_method)
end

after :all do
DataMapper::Model.extra_extensions.delete(Extensions)
end
end
end
end

0 comments on commit a09fa1e

Please sign in to comment.