From 13a584854e15dc213119ad825540e44e3c327a8c Mon Sep 17 00:00:00 2001 From: joshua stein Date: Sat, 30 Jun 2012 18:00:05 -0500 Subject: [PATCH] stories must have at least one tag --- app/models/story.rb | 10 ++++++++-- spec/models/story_spec.rb | 5 +++++ spec/support/blueprints.rb | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/story.rb b/app/models/story.rb index 9bdcd15..72539c4 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -16,7 +16,7 @@ class Story < ActiveRecord::Base MAX_EDIT_MINS = 30 attr_accessor :vote, :story_type, :already_posted_story, :fetched_content - attr_accessor :tags_to_add, :tags_to_delete + attr_accessor :new_tags, :tags_to_add, :tags_to_delete after_save :deal_with_tags before_create :assign_short_id @@ -35,7 +35,12 @@ class Story < ActiveRecord::Base errors.add(:url, "is not valid") end elsif self.description.to_s.strip == "" - self.errors(:description, "must contain text if no URL posted") + errors.add(:description, "must contain text if no URL posted") + end + + if !(self.new_tags || []).reject{|t| t.to_s.strip == "" }.any? + errors.add(:base, "Must have at least one tag. If no tags apply to " + + "your content, it probably doesn't belong here.") end end @@ -138,6 +143,7 @@ class Story < ActiveRecord::Base def tags_a=(new_tags) self.tags_to_delete = [] self.tags_to_add = [] + self.new_tags = new_tags self.tags.each do |tag| if !new_tags.include?(tag.tag) diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 937c0ab..8069fcf 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -28,6 +28,11 @@ describe Story do expect { Story.make!(:title => ("hello" * 100)) }.to raise_error end + it "must have at least one tag" do + expect { Story.make!(:tags_a => nil) }.to raise_error + expect { Story.make!(:tags_a => [ "", " " ]) }.to raise_error + end + it "checks for invalid urls" do expect { Story.make!(:title => "test", :url => "http://gooses.com/") }.to_not raise_error diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb index 36ab12e..aa4d6b2 100644 --- a/spec/support/blueprints.rb +++ b/spec/support/blueprints.rb @@ -11,6 +11,7 @@ Story.blueprint do user_id { User.make } title { "story title #{sn}" } url { "http://example.com/#{sn}" } + tags_a { [ "tag1", "tag2" ] } end Tag.blueprint do