Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 109 lines (78 sloc) 2.809 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
require 'spec_helper'

describe "Managing tags via names" do
  let(:article) { Article.create }

  it "returns tag names" do
    melbourne = Gutentag::Tag.create :name => 'melbourne'

    article.tags << melbourne

    article.tag_names.should == ['melbourne']
  end

  it "adds tags via their names" do
    article.tag_names << 'melbourne'
    article.save!

    article.tags.collect(&:name).should == ['melbourne']
  end

  it "allows for different tag normalisation" do
    Gutentag.normaliser = lambda { |name| name.upcase }

    Gutentag::Tag.create(:name => 'melbourne').name.should == 'MELBOURNE'

    Gutentag.normaliser = nil
  end

  it "doesn't complain when adding an existing tag" do
    article.tag_names << 'melbourne'
    article.tag_names << 'melbourne'
    article.save!

    article.tags.collect(&:name).should == ['melbourne']
  end

  it "accepts a completely new set of tags" do
    article.tag_names = ['portland', 'oregon']
    article.save!

    article.tags.collect(&:name).should == ['portland', 'oregon']
  end

  it "does not allow duplication of tags" do
    existing = Article.create
    existing.tags << Gutentag::Tag.create(:name => 'portland')

    article.tag_names = ['portland']
    article.save!

    existing.tag_ids.should == article.tag_ids
  end

  it "appends tag names" do
    article.tag_names = ['portland']
    article.tag_names += ['oregon', 'ruby']
    article.save!

    article.tags.collect(&:name).should == ['portland', 'oregon', 'ruby']
  end

  it "does not repeat appended names that already exist" do
    article.tag_names = ['portland', 'oregon']
    article.tag_names += ['oregon', 'ruby']
    article.save!

    article.tags.collect(&:name).should == ['portland', 'oregon', 'ruby']
  end

  it "removes a single tag name" do
    article.tag_names = ['portland', 'oregon']
    article.tag_names.delete 'oregon'
    article.save!

    article.tags.collect(&:name).should == ['portland']
  end

  it "removes tag names" do
    article.tag_names = ['portland', 'oregon', 'ruby']
    article.tag_names -= ['oregon', 'ruby']
    article.save!

    article.tags.collect(&:name).should == ['portland']
  end

  it "matches tag names ignoring case" do
    article.tag_names = ['portland']
    article.tag_names += ['Portland']
    article.save!

    article.tags.collect(&:name).should == ['portland']

    article.tag_names << 'Portland'
    article.save!

    article.tags.collect(&:name).should == ['portland']
  end

  it "allows setting of tag names on unpersisted objects" do
    article = Article.new :tag_names => ['melbourne', 'pancakes']
    article.save!

    article.tag_names.should == ['melbourne', 'pancakes']
  end

  it "allows overriding of tag_names=" do
    Article.instance_methods(false).should_not include(:tag_names=)
  end
end
Something went wrong with that request. Please try again.