diff options
-rw-r--r-- | app/models/public_body.rb | 5 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 48 |
2 files changed, 51 insertions, 2 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index ca7d39c74..2a701deb9 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -184,12 +184,15 @@ class PublicBody < ActiveRecord::Base end end - # Given an input string of tags, sets all tags to that string + # Given an input string of tags, sets all tags to that string. + # XXX This immediately saves the new tags. def tag_string=(tag_string) + tag_string = tag_string.strip tags = tag_string.split(/\s+/).uniq ActiveRecord::Base.transaction do for public_body_tag in self.public_body_tags + STDERR.puts("destroying tag " + public_body_tag.name) public_body_tag.destroy end for tag in tags diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5c58cdc54..d48079ba1 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -1,6 +1,52 @@ require File.dirname(__FILE__) + '/../spec_helper' - describe PublicBody, "making up the URL name" do +describe PublicBody, " using tags" do + before do + @public_body = PublicBody.new(:name => 'Aardvark Monitoring Service', + :short_name => 'AMS', + :request_email => 'foo@flourish.org', + :last_edit_editor => 'test', + :last_edit_comment => '') + end + + it 'should correctly convert a tag string into tags' do + @public_body.tag_string = 'stilton emmental' + @public_body.tag_string.should == 'stilton emmental' + + @public_body.has_tag?('stilton').should be_true + @public_body.has_tag?('emmental').should be_true + @public_body.has_tag?('jarlsberg').should be_false + end + + it 'should strip spaces from tag strings' do + @public_body.tag_string = ' chesire lancashire' + @public_body.tag_string.should == 'chesire lancashire' + end + + it 'should remove tags when changing them' do + @public_body.tag_string = 'stilton' + @public_body.tag_string.should == 'stilton' + + @public_body.has_tag?('stilton').should be_true + @public_body.has_tag?('jarlsberg').should be_false + + @public_body.tag_string = 'jarlsberg' + + @public_body.has_tag?('stilton').should be_false + @public_body.has_tag?('jarlsberg').should be_true + end + + it 'should be able to append tags' do + @public_body.tag_string.should == '' + @public_body.add_tag_if_not_already_present('cheddar') + + @public_body.tag_string.should == 'cheddar' + @public_body.has_tag?('cheddar').should be_true + end + +end + +describe PublicBody, " when making up the URL name" do before do @public_body = PublicBody.new end |