From 899a3be342c10d5aff4ff619fb698be05f24e157 Mon Sep 17 00:00:00 2001 From: robmckinnon Date: Thu, 24 Apr 2008 21:44:21 +0100 Subject: [PATCH] data loads now, do: git submodule update ... then: rake lordsqna --- app/models/subject.rb | 2 +- lib/tasks/lordsqna.rake | 13 ++++++++++--- spec/models/answer_group_spec.rb | 4 ++-- spec/models/subject_spec.rb | 2 +- vendor/plugins/morph | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/models/subject.rb b/app/models/subject.rb index beb900b5..ee6fe523 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -3,6 +3,6 @@ class Subject < ActiveRecord::Base has_many :answer_groups def self.from_name name - name ? find_or_create_from_name(name) : nil + name ? Subject.find_or_create_by_name(name) : nil end end diff --git a/lib/tasks/lordsqna.rake b/lib/tasks/lordsqna.rake index 10c23dcb..0603f11b 100644 --- a/lib/tasks/lordsqna.rake +++ b/lib/tasks/lordsqna.rake @@ -25,15 +25,22 @@ namespace :lordsqna do task :clean => [:migrate_down, :migrate_up, :clone_structure] do end - task :load_answers do + desc 'loads example data' + task :load_answers => :environment do + puts 'Loading example data' Dir["#{RAILS_ROOT}/data/*"].each do |file| - puts 'todo: load '+file + groups_data = AnswerGroups.from_url(file) + data_hashs = groups_data.collect(&:morph_attributes) + data_hashs.each do |data_hash| + group = AnswerGroup.create_from(data_hash) + group.save! + end end end desc 'does a clean sweep and loads xml, reindexes with solr' task :regenerate => [:migrate_down, :migrate_up, :load_answers] do - puts 'Regenerated all data' + puts "Loaded #{AnswerGroup.count} answer groups" end end diff --git a/spec/models/answer_group_spec.rb b/spec/models/answer_group_spec.rb index 1e2647ae..765328f9 100644 --- a/spec/models/answer_group_spec.rb +++ b/spec/models/answer_group_spec.rb @@ -38,14 +38,14 @@ it 'should associate with subject' do subject_name, subject = mock('subject'), mock_model(Subject) - Subject.should_receive(:find_or_create_from_name).with(subject_name).and_return subject + Subject.should_receive(:from_name).with(subject_name).and_return subject group = AnswerGroup.create_from(:subject => subject_name) group.subject.should == subject end it 'should associate with minor subject, when minor subject present' do subject_name, subject = mock('subject'), mock_model(Subject) - Subject.should_receive(:find_or_create_from_name).with(subject_name).and_return subject + Subject.should_receive(:from_name).with(subject_name).and_return subject group = AnswerGroup.create_from(:minor_subject => subject_name) group.minor_subject.should == subject end diff --git a/spec/models/subject_spec.rb b/spec/models/subject_spec.rb index e77babc0..f19adb41 100644 --- a/spec/models/subject_spec.rb +++ b/spec/models/subject_spec.rb @@ -8,7 +8,7 @@ it 'should find or create from name' do name, subject = 'name', mock_model(Subject) - Subject.should_receive(:find_or_create_from_name).with(name).and_return subject + Subject.should_receive(:find_or_create_by_name).with(name).and_return subject Subject.from_name(name).should == subject Subject.from_name(nil).should == nil diff --git a/vendor/plugins/morph b/vendor/plugins/morph index ba25befa..bb29d423 160000 --- a/vendor/plugins/morph +++ b/vendor/plugins/morph @@ -1 +1 @@ -Subproject commit ba25befadb9b7d201959e70c0240efb92b3a1b3e +Subproject commit bb29d423fb0abbcb7383eb30fc6a1ad226a20cdf