tag_ocelot 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/lib/tag_ocelot/active_record.rb +13 -0
- data/lib/tag_ocelot/tag_names.rb +55 -0
- data/lib/tag_ocelot.rb +2 -0
- data/spec/acceptance/tag_names_spec.rb +64 -0
- data/tag_ocelot.gemspec +1 -1
- metadata +1 -1
@@ -3,6 +3,19 @@ module TagOcelot::ActiveRecord
|
|
3
3
|
# include our class methods
|
4
4
|
base.extend TagOcelot::ActiveRecord::ClassMethods
|
5
5
|
end
|
6
|
+
|
7
|
+
def tag_names
|
8
|
+
@tag_names ||= TagOcelot::TagNames.new self
|
9
|
+
end
|
10
|
+
|
11
|
+
def tag_names=(names)
|
12
|
+
if names.is_a?(TagOcelot::TagNames)
|
13
|
+
@tag_names = names
|
14
|
+
else
|
15
|
+
# convert the array of strings to a TagNames object
|
16
|
+
@tag_names = TagOcelot::TagNames.new_with_names self, names
|
17
|
+
end
|
18
|
+
end
|
6
19
|
|
7
20
|
module ClassMethods
|
8
21
|
# Set up the underlying tag associations in the model
|
data/lib/tag_ocelot/tag_names.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
class TagOcelot::TagNames
|
2
|
+
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(taggable)
|
6
|
+
@taggable = taggable
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_a
|
10
|
+
taggable.tags.collect &:name
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(name)
|
14
|
+
# find an existing tag, or create a new one
|
15
|
+
tag = TagOcelot::Tag.where(:name => name).first ||
|
16
|
+
TagOcelot::Tag.create(:name => name)
|
17
|
+
|
18
|
+
taggable.tags << tag
|
19
|
+
end
|
20
|
+
|
21
|
+
# Create a new TagNames object, but clear out existing tags
|
22
|
+
# and use the provided set instead.
|
23
|
+
def self.new_with_names(taggable, names)
|
24
|
+
tag_names = new(taggable)
|
25
|
+
tag_names.clear
|
26
|
+
names.each { |name| tag_names << name }
|
27
|
+
tag_names
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear
|
31
|
+
taggable.tags.clear
|
32
|
+
end
|
33
|
+
|
34
|
+
def each(&block)
|
35
|
+
to_a.each &block
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete(name)
|
39
|
+
taggable.tags.delete TagOcelot::Tag.where(:name => name).first
|
40
|
+
end
|
41
|
+
|
42
|
+
def +(array)
|
43
|
+
array.each { |name| self.<< name }
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def -(array)
|
48
|
+
array.each { |name| self.delete name }
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_reader :taggable
|
55
|
+
end
|
data/lib/tag_ocelot.rb
CHANGED
@@ -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 << TagOcelot::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 << TagOcelot::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_ocelot.gemspec
CHANGED