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

Commit

Permalink
Demodulize the source model name for inverse relationship names
Browse files Browse the repository at this point in the history
* Updated resource and collection related scripts to use namespaced
  models to ensure they work perfectly.

[#869 state:resolved]
  • Loading branch information
dkubb committed May 29, 2009
1 parent dfe14d6 commit 2776df2
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 357 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/associations/many_to_one.rb
Expand Up @@ -174,7 +174,7 @@ def inverse_class
#
# @api private
def inverse_name
Extlib::Inflection.underscore(source_model.name).pluralize
Extlib::Inflection.underscore(Extlib::Inflection.demodulize(source_model.name)).pluralize
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/associations/one_to_many.rb
Expand Up @@ -149,7 +149,7 @@ def inverse_class
#
# @api private
def inverse_name
Extlib::Inflection.underscore(source_model.name)
Extlib::Inflection.underscore(Extlib::Inflection.demodulize(source_model.name))
end

##
Expand Down
46 changes: 27 additions & 19 deletions spec/public/associations/many_to_many_spec.rb
Expand Up @@ -9,42 +9,50 @@

# define the model prior to supported_by
before :all do
class ::Author
include DataMapper::Resource
module ::Blog
class Author
include DataMapper::Resource

property :id, Serial
property :name, String
property :id, Serial
property :name, String

has n, :articles, :through => Resource
end
has n, :articles, :through => Resource
end

class ::Article
include DataMapper::Resource
class Article
include DataMapper::Resource

property :id, Serial
property :title, String, :nullable => false
property :content, Text
property :id, Serial
property :title, String, :nullable => false
property :content, Text

has n, :authors, :through => Resource
belongs_to :original, :model => self, :nullable => true
has n, :revisions, :model => self, :child_key => [ :original_id ]
has 1, :previous, :model => self, :child_key => [ :original_id ], :order => [ :id.desc ]
has n, :authors, :through => Resource
belongs_to :original, :model => self, :nullable => true
has n, :revisions, :model => self, :child_key => [ :original_id ]
has 1, :previous, :model => self, :child_key => [ :original_id ], :order => [ :id.desc ]
end
end

@model = Article
@author_model = Blog::Author
@article_model = Blog::Article

# initialize the join model
Blog::Author.relationships[:articles].through

@join_model = Blog::ArticleAuthor
end

supported_by :all do
before :all do
@author = Author.create(:name => 'Dan Kubb')
@author = @author_model.create(:name => 'Dan Kubb')

@original = @author.articles.create(:title => 'Original Article')
@article = @author.articles.create(:title => 'Sample Article', :content => 'Sample', :original => @original)
@other = @author.articles.create(:title => 'Other Article', :content => 'Other')

# load the targets without references to a single source
load_collection = lambda do |query|
Author.get(*@author.key).articles(query)
@author_model.get(*@author.key).articles(query)
end

@articles = load_collection.call(:title => 'Sample Article')
Expand All @@ -68,7 +76,7 @@ class ::Article
end

it 'should only remove the join resource for the destroyed resource' do
ArticleAuthor.all.should_not be_empty
@join_model.all.should_not be_empty
end
end
end
Expand Down
76 changes: 43 additions & 33 deletions spec/public/associations/many_to_one_spec.rb
Expand Up @@ -2,58 +2,68 @@

describe 'Many to One Associations' do
before :all do
class ::User
include DataMapper::Resource
module ::Blog
class User
include DataMapper::Resource

property :name, String, :key => true
property :age, Integer
property :summary, Text
property :description, Text
property :admin, Boolean, :accessor => :private
property :name, String, :key => true
property :age, Integer
property :summary, Text
property :description, Text
property :admin, Boolean, :accessor => :private

belongs_to :referrer, :model => self, :nullable => true
has n, :comments
end
belongs_to :referrer, :model => self, :nullable => true
has n, :comments

class ::Author < User; end
# FIXME: figure out a different approach than stubbing things out
def comment=(*)
# do nothing with comment
end
end

class ::Comment
include DataMapper::Resource
class Author < User; end

property :id, Serial
property :body, Text
class Comment
include DataMapper::Resource

belongs_to :user
end
property :id, Serial
property :body, Text

class ::Article
include DataMapper::Resource
belongs_to :user
end

property :id, Serial
property :body, Text
class Article
include DataMapper::Resource

has n, :paragraphs
end
property :id, Serial
property :body, Text

has n, :paragraphs
end

class ::Paragraph
include DataMapper::Resource
class Paragraph
include DataMapper::Resource

property :id, Serial
property :text, String
property :id, Serial
property :text, String

belongs_to :article
belongs_to :article
end
end

@model = User
@child_model = Comment
@user_model = Blog::User
@author_model = Blog::Author
@comment_model = Blog::Comment
@article_model = Blog::Article
@paragraph_model = Blog::Paragraph
end

supported_by :all do
before :all do
user = @model.create(:name => 'dbussink', :age => 25, :description => 'Test')
comment = @child_model.create(:body => 'Cool spec', :user => user)
user = @user_model.create(:name => 'dbussink', :age => 25, :description => 'Test')
comment = @comment_model.create(:body => 'Cool spec', :user => user)

@comment = @child_model.get(*comment.key)
@comment = @comment_model.get(*comment.key)
@user = @comment.user
end

Expand Down
49 changes: 26 additions & 23 deletions spec/public/associations/one_to_many_spec.rb
Expand Up @@ -30,42 +30,45 @@

# define the model prior to supported_by
before :all do
class ::Author
include DataMapper::Resource
module ::Blog
class Author
include DataMapper::Resource

property :id, Serial
property :name, String
property :id, Serial
property :name, String

has n, :articles
end
has n, :articles
end

class ::Article
include DataMapper::Resource
class Article
include DataMapper::Resource

property :id, Serial
property :title, String, :nullable => false
property :content, Text
property :id, Serial
property :title, String, :nullable => false
property :content, Text

belongs_to :author, :nullable => true
belongs_to :original, :model => self, :nullable => true
has n, :revisions, :model => self, :child_key => [ :original_id ]
has 1, :previous, :model => self, :child_key => [ :original_id ], :order => [ :id.desc ]
belongs_to :author, :nullable => true
belongs_to :original, :model => self, :nullable => true
has n, :revisions, :model => self, :child_key => [ :original_id ]
has 1, :previous, :model => self, :child_key => [ :original_id ], :order => [ :id.desc ]
end
end

@model = Article
@author_model = Blog::Author
@article_model = Blog::Article
end

supported_by :all do
before :all do
@author = Author.create(:name => 'Dan Kubb')
@author = @author_model.create(:name => 'Dan Kubb')

@original = @author.articles.create(:title => 'Original Article')
@article = @author.articles.create(:title => 'Sample Article', :content => 'Sample', :original => @original)
@other = @author.articles.create(:title => 'Other Article', :content => 'Other')

# load the targets without references to a single source
load_collection = lambda do |query|
Author.get(*@author.key).articles(query)
@author_model.get(*@author.key).articles(query)
end

@articles = load_collection.call(:title => 'Sample Article')
Expand Down Expand Up @@ -140,7 +143,7 @@ class ::Article
describe '#create' do
describe 'when the parent is not saved' do
it 'should raise an exception' do
author = Author.new(:name => 'Dan Kubb')
author = @author_model.new(:name => 'Dan Kubb')
lambda {
author.articles.create
}.should raise_error(DataMapper::Associations::UnsavedParentError, 'The source must be saved before creating a Resource')
Expand All @@ -151,7 +154,7 @@ class ::Article
describe '#destroy' do
describe 'when the parent is not saved' do
it 'should raise an exception' do
author = Author.new(:name => 'Dan Kubb')
author = @author_model.new(:name => 'Dan Kubb')
lambda {
author.articles.destroy
}.should raise_error(DataMapper::Associations::UnsavedParentError, 'The source must be saved before mass-deleting the collection')
Expand All @@ -162,7 +165,7 @@ class ::Article
describe '#destroy!' do
describe 'when the parent is not saved' do
it 'should raise an exception' do
author = Author.new(:name => 'Dan Kubb')
author = @author_model.new(:name => 'Dan Kubb')
lambda {
author.articles.destroy!
}.should raise_error(DataMapper::Associations::UnsavedParentError, 'The source must be saved before mass-deleting the collection without validation')
Expand Down Expand Up @@ -291,7 +294,7 @@ class ::Article
describe '#update' do
describe 'when the parent is not saved' do
it 'should raise an exception' do
author = Author.new(:name => 'Dan Kubb')
author = @author_model.new(:name => 'Dan Kubb')
lambda {
author.articles.update(:title => 'New Title')
}.should raise_error(DataMapper::Associations::UnsavedParentError, 'The source must be saved before mass-updating the collection')
Expand All @@ -302,7 +305,7 @@ class ::Article
describe '#update!' do
describe 'when the parent is not saved' do
it 'should raise an exception' do
author = Author.new(:name => 'Dan Kubb')
author = @author_model.new(:name => 'Dan Kubb')
lambda {
author.articles.update!(:title => 'New Title')
}.should raise_error(DataMapper::Associations::UnsavedParentError, 'The source must be saved before mass-updating the collection without validation')
Expand Down

0 comments on commit 2776df2

Please sign in to comment.