tag_giraffe 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tag_giraffe.rb +2 -1
- data/lib/tag_giraffe/active_record.rb +14 -2
- data/lib/tag_giraffe/tag_names.rb +51 -0
- data/spec/acceptance/tag_names_spec.rb +64 -0
- data/tag_giraffe.gemspec +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d652c8d5f084eb19f1047f7887b0d8f97af20af3
|
4
|
+
data.tar.gz: e5d839fcb07db2112d162578f665addb329e29a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e010a4c0d441c33833e2bf80e0fca00089a965ad8428ef79e505a113d7b56f350234c2050b101973b773020d5de4b21a58755e3d3b0ee081b10a04359c534c5c
|
7
|
+
data.tar.gz: e1e5e81f53976cf72f813aa003dd3d3a94e58067f053c93cf3d7d03203ab1b532f8ad4f09f0360aa295f891e5faca24ef1ac272d8c7e8dd97639e5322b8315d1
|
data/lib/tag_giraffe.rb
CHANGED
@@ -4,6 +4,19 @@ module TagGiraffe::ActiveRecord
|
|
4
4
|
base.extend TagGiraffe::ActiveRecord::ClassMethods
|
5
5
|
end
|
6
6
|
|
7
|
+
def tag_names
|
8
|
+
@tag_names ||= TagGiraffe::TagNames.new self
|
9
|
+
end
|
10
|
+
|
11
|
+
def tag_names=(names)
|
12
|
+
if names.is_a?(TagGiraffe::TagNames)
|
13
|
+
@tag_names = names
|
14
|
+
else
|
15
|
+
# convert the array of strings to a TagNames object
|
16
|
+
@tag_names = TagGiraffe::TagNames.new_with_names self, names
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
7
20
|
module ClassMethods
|
8
21
|
# set up the underlying tag assocations in the model
|
9
22
|
def has_many_tags
|
@@ -13,5 +26,4 @@ module TagGiraffe::ActiveRecord
|
|
13
26
|
through: :taggings
|
14
27
|
end
|
15
28
|
end
|
16
|
-
end
|
17
|
-
|
29
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class TagGiraffe::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 = TagGiraffe::Tag.where(name: name).first ||
|
15
|
+
TagGiraffe::Tag.create(name: name)
|
16
|
+
|
17
|
+
taggable.tags << tag
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete(name)
|
21
|
+
taggable.tags.delete TagGiraffe::Tag.where(name: name).first
|
22
|
+
end
|
23
|
+
|
24
|
+
def +(array)
|
25
|
+
array.each { |name| self.<< name }
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def -(array)
|
30
|
+
array.each { |name| self.delete name }
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def each(&block)
|
35
|
+
to_a.each &block
|
36
|
+
end
|
37
|
+
|
38
|
+
def clear
|
39
|
+
taggable.tags.clear
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.new_with_names(taggable, names)
|
43
|
+
tag_names = new(taggable)
|
44
|
+
tag_names.clear
|
45
|
+
names.each { |name| tag_names << name }
|
46
|
+
tag_names
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
attr_reader :taggable
|
51
|
+
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 << TagGiraffe::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 << TagGiraffe::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_giraffe.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tag_giraffe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Kennedy
|
@@ -81,6 +81,8 @@ files:
|
|
81
81
|
- lib/tag_giraffe.rb
|
82
82
|
- lib/tag_giraffe/active_record.rb
|
83
83
|
- lib/tag_giraffe/engine.rb
|
84
|
+
- lib/tag_giraffe/tag_names.rb
|
85
|
+
- spec/acceptance/tag_names_spec.rb
|
84
86
|
- spec/acceptance/tags_spec.rb
|
85
87
|
- spec/internal/app/models/article.rb
|
86
88
|
- spec/internal/config/database.yml
|