tag_bobby 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/app/models/tag_bobby/tag.rb +9 -0
- data/lib/tag_bobby.rb +2 -1
- data/lib/tag_bobby/active_record.rb +13 -0
- data/lib/tag_bobby/tag_names.rb +54 -0
- data/spec/acceptance/tag_names_spec.rb +64 -0
- data/tag_bobby.gemspec +1 -1
- metadata +3 -1
data/app/models/tag_bobby/tag.rb
CHANGED
@@ -3,4 +3,13 @@ class TagBobby::Tag < ActiveRecord::Base
|
|
3
3
|
:dependent => :destroy
|
4
4
|
|
5
5
|
validates :name, :presence => true, :uniqueness => true
|
6
|
+
|
7
|
+
def tag_names=(names)
|
8
|
+
if names.is_a?(TagBobby::TagNames)
|
9
|
+
@tag_names = names
|
10
|
+
else
|
11
|
+
# convert the array of strings to a TagNames object
|
12
|
+
@tag_names = TagBobby::TagNames.new_with_names self, names
|
13
|
+
end
|
14
|
+
end
|
6
15
|
end
|
data/lib/tag_bobby.rb
CHANGED
@@ -13,4 +13,17 @@ module TagBobby::ActiveRecord
|
|
13
13
|
:through => :taggings
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def tag_names
|
18
|
+
@tag_names ||= TagBobby::TagNames.new self
|
19
|
+
end
|
20
|
+
|
21
|
+
def tag_names=(names)
|
22
|
+
if names.is_a?(TagBobby::TagNames)
|
23
|
+
@tag_names = names
|
24
|
+
else
|
25
|
+
# convert the array of strings to a TagNames object
|
26
|
+
@tag_names = TagBobby::TagNames.new_with_names self, names
|
27
|
+
end
|
28
|
+
end
|
16
29
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class TagBobby::TagNames
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
def initialize(taggable)
|
5
|
+
@taggable = taggable
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_a
|
9
|
+
taggable.tags.collect &:name
|
10
|
+
end
|
11
|
+
|
12
|
+
def <<(name)
|
13
|
+
# find an existing tag, or create a new one
|
14
|
+
tag = TagBobby::Tag.where(:name => name).first ||
|
15
|
+
TagBobby::Tag.create(:name => name)
|
16
|
+
|
17
|
+
taggable.tags << tag
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create a new TagNames object, but clear out existing tags
|
21
|
+
# and use the provided set instead.
|
22
|
+
def self.new_with_names(taggable, names)
|
23
|
+
tag_names = new(taggable)
|
24
|
+
tag_names.clear
|
25
|
+
names.each { |name| tag_names << name }
|
26
|
+
tag_names
|
27
|
+
end
|
28
|
+
|
29
|
+
def clear
|
30
|
+
taggable.tags.clear
|
31
|
+
end
|
32
|
+
|
33
|
+
def each(&block)
|
34
|
+
to_a.each &block
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(name)
|
38
|
+
taggable.tags.delete TagBobby::Tag.where(:name => name).first
|
39
|
+
end
|
40
|
+
|
41
|
+
def +(array)
|
42
|
+
array.each { |name| self.<< name }
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def -(array)
|
47
|
+
array.each { |name| self.delete name }
|
48
|
+
self
|
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 << TagBobby::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 << TagBobby::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_bobby.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tag_bobby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -90,6 +90,8 @@ files:
|
|
90
90
|
- lib/tag_bobby.rb
|
91
91
|
- lib/tag_bobby/active_record.rb
|
92
92
|
- lib/tag_bobby/engine.rb
|
93
|
+
- lib/tag_bobby/tag_names.rb
|
94
|
+
- spec/acceptance/tag_names_spec.rb
|
93
95
|
- spec/acceptance/tags_spec.rb
|
94
96
|
- spec/internal/app/models/article.rb
|
95
97
|
- spec/internal/config/database.yml
|