Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
defined associations between models
Browse files Browse the repository at this point in the history
  • Loading branch information
robmckinnon committed Apr 24, 2008
1 parent 14ab2df commit 16cfb26
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class Answer < ActiveRecord::Base

has_many :questions
belongs_to :answer_group

end
5 changes: 5 additions & 0 deletions app/models/answer_group.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class AnswerGroup < ActiveRecord::Base

has_many :answers
belongs_to :subject
belongs_to :minor_subject, :class_name => Subject

end
3 changes: 3 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Question < ActiveRecord::Base

belongs_to :answer

end
1 change: 1 addition & 0 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Subject < ActiveRecord::Base
has_many :answer_groups
end
13 changes: 9 additions & 4 deletions spec/models/answer_group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe AnswerGroup do
before(:each) do
@answer_group = AnswerGroup.new

it 'should have many answers' do
assert_model_has_many AnswerGroup, :answers
end

it 'should belong to a subject' do
assert_model_belongs_to AnswerGroup, :subject
end

it "should be valid" do
@answer_group.should be_valid
it 'should belong to a minor subject' do
assert_model_belongs_to AnswerGroup, :minor_subject
end
end
10 changes: 6 additions & 4 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Answer do
before(:each) do
@answer = Answer.new

it 'should have many questions' do
assert_model_has_many Answer, :questions
end

it "should be valid" do
@answer.should be_valid
it 'should belong to an answer group' do
assert_model_belongs_to Answer, :answer_group
end

end
7 changes: 2 additions & 5 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Question do
before(:each) do
@question = Question.new
end

it "should be valid" do
@question.should be_valid
it 'should belong to an answer' do
assert_model_belongs_to Question, :answer
end
end
7 changes: 2 additions & 5 deletions spec/models/subject_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Subject do
before(:each) do
@subject = Subject.new
end

it "should be valid" do
@subject.should be_valid
it 'should have many answer groups' do
assert_model_has_many Subject, :answer_groups
end
end
23 changes: 20 additions & 3 deletions spec/spec_utils.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@

# e.g. assert_model_has_many Answer, :questions
def assert_model_has_many(model, association_name)
assert_association_exists model, :has_many, association_name
end

# e.g. assert_model_belongs_to Question, :answer
def assert_model_belongs_to(model, association_name)
assert_association_exists model, :belongs_to, association_name, model_with_foreign_key=model
end

def assert_association_exists model, association_macro, association_name, model_with_foreign_key=nil
association = model.reflect_on_association(association_name.to_sym)
assert_not_nil association, "Could not find an association for #{association_name}"
assert_equal association_macro, association.macro
model_with_foreign_key = model_with_foreign_key || association.klass
begin
association.klass
rescue Exception => e
unless association.options[:class_name]
klass = e.to_s[/uninitialized constant .*::(.+)/, 1]
raise "Could not find class #{klass} for association #{association_name}, try defining :class_name in the #{association_macro} association declaration."
end
end

assert_equal :has_many, association.macro

assert association.klass.column_names.include?(association.primary_key_name)
assert model_with_foreign_key.column_names.include?(association.primary_key_name), "Could not find foreign key '#{association.primary_key_name}' for the association '#{association_name}' in the table for model '#{model_with_foreign_key}'."
end

0 comments on commit 16cfb26

Please sign in to comment.