Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many Association #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/ork/model/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,39 @@ def referenced(name, model, reference = to_reference)
end
end

# A macro for defining a method which basically does a find.
#
# Example:
# class Post
# include Ork::Document
#
# reference :blog, :Blog
# end
#
# class Blog
# include Ork::Document
#
# many :posts, :Post
# end
#
# # is the same as
#
# class Blog
# include Ork::Document
#
# def posts
# Post.find(:blog_id => self.id)
# end
# end
#
def many(name, model, reference = to_reference)
define_method name do
return [] if self.id.nil?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

model = Ork::Utils.const(self.class, model)
model.find(:"#{reference}_id", self.id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant self detected.

end
end

# A macro for defining a method which basically does a find.
#
# Example:
Expand Down
64 changes: 64 additions & 0 deletions test/associations/many_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require_relative '../helper'

Protest.describe 'many' do
class Blog
include Ork::Document
attribute :name

many :posts, :Post
many :weird_posts, :Post, :weird_blog
end

class Post
include Ork::Document
attribute :title

reference :blog, :Blog
reference :weird_blog, :Blog
end

setup do
randomize_bucket_name Blog
randomize_bucket_name Post
end

should 'return an empty list when there is no reference object' do
blog = Blog.new

assert blog.posts.empty?
end

test 'defines reader method but not a writer method' do
blog = Blog.new

assert blog.respond_to?(:posts)
assert !blog.respond_to?(:posts=)
end

should 'return the objects referenced' do
blog = Blog.create name: 'New'
post = Post.create blog: blog
blog.reload

assert blog.posts.include?(post)
end

test 'object reference with not default key' do
blog = Blog.create name: 'New'
post = Post.create weird_blog: blog

assert blog.weird_posts.include?(post)
end

should 'update referenced object' do
blog = Blog.create name: 'New'
post = Post.create title: 'First One'

assert blog.posts.empty?

post.blog = blog
post.save

assert blog.posts.include?(post)
end
end