Create a gist now

Instantly share code, notes, and snippets.

Not a good idea.
require 'rubygems'
require 'acts_as_fu'
build_model(:articles) do
has_many :comments do
def votes
Vote.scoped(:include => { :comment => :article }, :conditions => ['articles.id = ?', proxy_owner.id])
end
def authors
Author.scoped(:include => :comments, :conditions => ['comments.article_id = ?', proxy_owner.id])
end
end
end
build_model(:comments) do
integer :author_id
integer :article_id
belongs_to :article
belongs_to :author
has_many :votes
end
build_model(:authors) do
has_many :comments
end
build_model(:votes) do
integer :comment_id
belongs_to :comment
end
require 'spec'
describe 'Association funkiness' do
before(:each) do
@article = Article.create!
@comment = Comment.create! :article => @article
@other_comment = Comment.create! :article => @article
end
it "works normally" do
@article.comments.should include(@comment, @other_comment)
end
describe "loading has many relationships" do
before(:each) do
@vote = Vote.create! :comment => @comment
@other_vote = Vote.create! :comment => @other_comment
end
it "loads through has many throughs" do
@article.comments.votes.should include(@vote, @other_vote)
end
it "is a proxy object" do
@article.comments.votes.find(:first).should == @vote
end
end
describe "loading belongs to relationships" do
before(:each) do
@author = Author.create! :comments => [@comment]
@other_author = Author.create! :comments => [@other_comment]
end
it "loads through has many throughs" do
@article.comments.authors.should include(@author, @other_author)
end
it "is a proxy object" do
@article.comments.authors.find(:first).should == @author
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment