This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
robmckinnon
committed
Apr 24, 2008
1 parent
14ab2df
commit 16cfb26
Showing
9 changed files
with
52 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class Question < ActiveRecord::Base | ||
|
||
belongs_to :answer | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Subject < ActiveRecord::Base | ||
has_many :answer_groups | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|