Skip to content

Commit

Permalink
don't save empty clouds
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalronin committed Mar 28, 2009
1 parent 0ceae73 commit 027cb0e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/models/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
5 changes: 4 additions & 1 deletion app/models/mp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions spec/models/mp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 027cb0e

Please sign in to comment.