From dedc03ff0e2bf744afecf417a0a2f00107dd7f2f Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Thu, 24 Apr 2008 20:59:34 +0100 Subject: [PATCH] question loads its data --- app/models/answer_group.rb | 2 +- app/models/question.rb | 5 +++++ spec/models/answer_group_spec.rb | 2 +- spec/models/question_spec.rb | 24 ++++++++++++++++++++++++ spec/spec_utils.rb | 1 - 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/models/answer_group.rb b/app/models/answer_group.rb index a67237a1..025067c7 100644 --- a/app/models/answer_group.rb +++ b/app/models/answer_group.rb @@ -15,6 +15,6 @@ def self.create_from data end def title - minor_subject ? "#{subject.name}: #{minor_subject.name}" : subject.name + (specifics = minor_subject) ? "#{subject.name}: #{specifics.name}" : subject.name end end diff --git a/app/models/question.rb b/app/models/question.rb index b55e8b41..ac032952 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -2,4 +2,9 @@ class Question < ActiveRecord::Base belongs_to :answer + def self.create_from data + attributes = data.slice(:uin, :member, :text) + Question.new(attributes) + end + end diff --git a/spec/models/answer_group_spec.rb b/spec/models/answer_group_spec.rb index 9a154830..1e2647ae 100644 --- a/spec/models/answer_group_spec.rb +++ b/spec/models/answer_group_spec.rb @@ -78,7 +78,7 @@ it 'should use subject name only, if has no minor subject' do specifics = 'specifics' minor_subject = mock_model(Subject, :name => specifics) - @group.should_receive(:minor_subject).twice.and_return minor_subject + @group.should_receive(:minor_subject).and_return minor_subject @group.title.should == "#{@topic}: #{specifics}" end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 2c167a2a..1ef8cb4b 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -1,8 +1,32 @@ require File.dirname(__FILE__) + '/../spec_helper' +require File.dirname(__FILE__) + '/model_spec_helper' describe Question do it 'should belong to an answer' do assert_model_belongs_to Question, :answer end + +end + +describe Question, 'when loading' do + + include ModelSpecHelper + + it 'should ignore unneeded attribute' do + lambda { Question.create_from(:title => 'title') }.should_not raise_error + end + + it 'should load member' do + assert_loads_attribute Question, :member + end + + it 'should load uin' do + assert_loads_attribute Question, :uin + end + + it 'should load text' do + assert_loads_attribute Question, :text + end + end diff --git a/spec/spec_utils.rb b/spec/spec_utils.rb index 02e94e93..a550eb22 100644 --- a/spec/spec_utils.rb +++ b/spec/spec_utils.rb @@ -30,4 +30,3 @@ def assert_association_exists model, association_macro, association_name, model_ 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 -