tag_ringtail 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .ruby-gemset
3
3
  Gemfile.lock
4
4
  spec/internal/db/*.sqlite
5
+ *.gem
data/lib/tag_ringtail.rb CHANGED
@@ -6,3 +6,4 @@ end
6
6
  # Require the rest of the gem's files
7
7
  require 'tag_ringtail/active_record'
8
8
  require 'tag_ringtail/engine'
9
+ require 'tag_ringtail/tag_names'
@@ -4,6 +4,19 @@ module TagRingtail::ActiveRecord
4
4
  base.extend TagRingtail::ActiveRecord::ClassMethods
5
5
  end
6
6
 
7
+ def tag_names
8
+ @tag_names ||= TagRingtail::TagNames.new self
9
+ end
10
+
11
+ def tag_names=(names)
12
+ if names.is_a?(TagRingtail::TagNames)
13
+ @tag_names = names
14
+ else
15
+ # convert the array of strings to a TagNames object
16
+ @tag_names = TagRingtail::TagNames.new_with_names self, names
17
+ end
18
+ end
19
+
7
20
  module ClassMethods
8
21
  # Set up the underlying tag associations in the model
9
22
  def has_many_tags
@@ -0,0 +1,54 @@
1
+ class TagRingtail::TagNames
2
+ include Enumerable
3
+
4
+ # Create a new TagNames object, but clear out existing tags
5
+ # and use the provided set instead.
6
+ def self.new_with_names(taggable, names)
7
+ tag_names = new(taggable)
8
+ tag_names.clear
9
+ names.each { |name| tag_names << name }
10
+ tag_names
11
+ end
12
+
13
+ def initialize(taggable)
14
+ @taggable = taggable
15
+ end
16
+
17
+ def to_a
18
+ taggable.tags.collect &:name
19
+ end
20
+
21
+ def <<(name)
22
+ # find an existing tag, or create a new one
23
+ tag = TagRingtail::Tag.where(:name => name).first ||
24
+ TagRingtail::Tag.create(:name => name)
25
+
26
+ taggable.tags << tag
27
+ end
28
+
29
+ def delete(name)
30
+ taggable.tags.delete TagRingtail::Tag.where(:name => name).first
31
+ end
32
+
33
+ def +(array)
34
+ array.each { |name| self.<< name }
35
+ self
36
+ end
37
+
38
+ def -(array)
39
+ array.each { |name| self.delete name }
40
+ self
41
+ end
42
+
43
+ def each(&block)
44
+ to_a.each &block
45
+ end
46
+
47
+ def clear
48
+ taggable.tags.clear
49
+ end
50
+
51
+ private
52
+
53
+ attr_reader :taggable
54
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Managing tags via names" do
4
+ let(:article) { Article.create }
5
+
6
+ it "returns tag names" do
7
+ article.tags << TagRingtail::Tag.create(:name => 'melbourne')
8
+
9
+ article.tag_names.to_a.should == ['melbourne']
10
+ end
11
+
12
+ it "adds tags via their names" do
13
+ article.tag_names << 'melbourne'
14
+
15
+ article.tags.collect(&:name).should == ['melbourne']
16
+ end
17
+
18
+ it "accepts a completely new set of tags" do
19
+ article.tag_names = ['portland', 'oregon']
20
+
21
+ article.tags.collect(&:name).should == ['portland', 'oregon']
22
+ end
23
+
24
+ it "enumerates through tag names" do
25
+ article.tag_names = ['melbourne', 'victoria']
26
+ names = []
27
+
28
+ article.tag_names.each do |name|
29
+ names << name
30
+ end
31
+
32
+ names.should == ['melbourne', 'victoria']
33
+ end
34
+
35
+ it "does not allow duplication of tags" do
36
+ existing = Article.create
37
+ existing.tags << TagRingtail::Tag.create(:name => 'portland')
38
+
39
+ article.tag_names = ['portland']
40
+
41
+ existing.tag_ids.should == article.tag_ids
42
+ end
43
+
44
+ it "appends tag names" do
45
+ article.tag_names = ['portland']
46
+ article.tag_names += ['oregon', 'ruby']
47
+
48
+ article.tags.collect(&:name).should == ['portland', 'oregon', 'ruby']
49
+ end
50
+
51
+ it "removes a single tag name" do
52
+ article.tag_names = ['portland', 'oregon']
53
+ article.tag_names.delete 'oregon'
54
+
55
+ article.tags.collect(&:name).should == ['portland']
56
+ end
57
+
58
+ it "removes tag names" do
59
+ article.tag_names = ['portland', 'oregon', 'ruby']
60
+ article.tag_names -= ['oregon', 'ruby']
61
+
62
+ article.tags.collect(&:name).should == ['portland']
63
+ end
64
+ end
data/tag_ringtail.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'tag_ringtail'
4
- s.version = '0.0.2'
4
+ s.version = '0.1.0'
5
5
  s.authors = ['Bob McClellan Jr']
6
6
  s.email = ['bob.mcclellan@gmail.com']
7
7
  s.homepage = 'https://github.com/kabobbob/tag_ringtail'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tag_ringtail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
12
+ date: 2013-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -91,6 +91,8 @@ files:
91
91
  - lib/tag_ringtail.rb
92
92
  - lib/tag_ringtail/active_record.rb
93
93
  - lib/tag_ringtail/engine.rb
94
+ - lib/tag_ringtail/tag_names.rb
95
+ - spec/acceptance/tag_names_spec.rb
94
96
  - spec/acceptance/tags_spec.rb
95
97
  - spec/internal/app/models/article.rb
96
98
  - spec/internal/config/database.yml