diff --git a/app/models/cloud.rb b/app/models/cloud.rb index c08608e..cc58c70 100644 --- a/app/models/cloud.rb +++ b/app/models/cloud.rb @@ -13,7 +13,9 @@ class Cloud < ActiveRecord::Base belongs_to :mp has_many :terms - def initialize(params) + validate :has_some_terms + + def initialize(params = nil) super params tc = TextChunk.new mp.text_for_cloud self.terms = tc.terms_with_counts @@ -23,4 +25,14 @@ def title "#{mp.full_name} Written Questions" end + private + + # Sometimes the Yahoo! Term Extractor gives back nothing. + # Don't save the cloud if that happens. + def has_some_terms + if self.terms.size == 0 + errors.add :terms, "must not be empty" + end + end + end diff --git a/app/models/mp.rb b/app/models/mp.rb index a4dc2a4..9c9e7b4 100644 --- a/app/models/mp.rb +++ b/app/models/mp.rb @@ -75,7 +75,10 @@ def to_param def get_cloud cloud = self.clouds.last - return clouds.create if cloud.nil? or written_question_text_has_changed? + if cloud.nil? or written_question_text_has_changed? + cloud = clouds.new + cloud.save! + end cloud end diff --git a/spec/models/mp_spec.rb b/spec/models/mp_spec.rb index 1f9533b..d08aa92 100644 --- a/spec/models/mp_spec.rb +++ b/spec/models/mp_spec.rb @@ -11,6 +11,19 @@ } end + it "should not save a cloud with no terms" do + mp = Mp.make + TagExtractor.stub!(:extract).and_return([]) + twfy = mock_model Twfy::Client + Twfy::Client.stub!(:new).and_return twfy + dummy = mock "Answers", :rows => [{'body' => sample_text}] + twfy.stub!(:wrans).with(:person => mp.person_id).and_return(dummy) + lambda { mp.get_cloud }.should raise_error(ActiveRecord::RecordInvalid) + Term.count.should == 0 + Cloud.count.should == 0 + end + + it "should update cloud if wrans is different" do mp = Mp.make mock_twfy_for_mp mp, "Here is the first text"