tag_dangelo 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_dangelo.rb +1 -0
- data/lib/tag_dangelo/active_record.rb +12 -0
- data/lib/tag_dangelo/tag_names.rb +54 -0
- data/spec/acceptance/tag_names_spec.rb +49 -0
- data/tag_dangelo.gemspec +4 -3
- metadata +22 -4
data/lib/tag_dangelo.rb
CHANGED
@@ -4,6 +4,18 @@ module TagDangelo::ActiveRecord
|
|
4
4
|
base.extend TagDangelo::ActiveRecord::ClassMethods
|
5
5
|
end
|
6
6
|
|
7
|
+
def tag_names
|
8
|
+
@tag_names ||= TagDangelo::TagNames.new self
|
9
|
+
end
|
10
|
+
|
11
|
+
def tag_names=(names)
|
12
|
+
@tag_names = if names.is_a?(TagDangelo::TagNames)
|
13
|
+
names
|
14
|
+
else
|
15
|
+
TagDangelo::TagNames.new_with_names self, names
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
7
19
|
module ClassMethods
|
8
20
|
# set up the underlying tag associations in the model
|
9
21
|
def has_many_tags
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class TagDangelo::TagNames
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
def initialize(taggable)
|
5
|
+
@taggable = taggable
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.new_with_names(taggable, names)
|
9
|
+
tag_names = new(taggable)
|
10
|
+
tag_names.clear
|
11
|
+
names.each { |name| tag_names << name }
|
12
|
+
tag_names
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear
|
16
|
+
taggable.tags.clear
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_a
|
20
|
+
taggable.tags.map(&:name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
to_a.each &block
|
25
|
+
end
|
26
|
+
|
27
|
+
def <<(name)
|
28
|
+
tag = TagDangelo::Tag.where(name: name).first ||
|
29
|
+
TagDangelo::Tag.create(name: name)
|
30
|
+
|
31
|
+
taggable.tags << tag
|
32
|
+
end
|
33
|
+
|
34
|
+
def delete(name)
|
35
|
+
taggable.tags.delete TagDangelo::Tag.where(name: name).first
|
36
|
+
end
|
37
|
+
|
38
|
+
def +(names)
|
39
|
+
names.each { |name| self << name }
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def -(names)
|
44
|
+
names.each { |name| self.delete name }
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
attr_reader :taggable
|
51
|
+
|
52
|
+
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Managing tags via names" do
|
4
|
+
|
5
|
+
let(:article) { Article.create }
|
6
|
+
|
7
|
+
it "returns tag names" do
|
8
|
+
article.tags << TagDangelo::Tag.create(name: 'melbourne')
|
9
|
+
expect(article.tag_names.to_a).to eq ['melbourne']
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds tags via their names" do
|
13
|
+
article.tag_names << 'melbourne'
|
14
|
+
expect(article.tags.map(&:name)).to eq ['melbourne']
|
15
|
+
end
|
16
|
+
|
17
|
+
it "accepts a completely new set of tags" do
|
18
|
+
article.tag_names = ['portland', 'oregon']
|
19
|
+
expect(article.tags.map(&:name)).to eq ['portland', 'oregon']
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not allow duplication of tags" do
|
23
|
+
existing = Article.create
|
24
|
+
existing.tags << TagDangelo::Tag.create(name: 'portland')
|
25
|
+
article.tag_names = ['portland']
|
26
|
+
expect(existing.tag_ids).to eq article.tag_ids
|
27
|
+
end
|
28
|
+
|
29
|
+
it "appends tag names" do
|
30
|
+
article.tag_names = ['ruby']
|
31
|
+
article.tag_names += ['portland', 'oregon']
|
32
|
+
expect(article.tags.map(&:name)).to eq ['ruby', 'portland', 'oregon']
|
33
|
+
end
|
34
|
+
|
35
|
+
it "removes a single tag name" do
|
36
|
+
article.tag_names = ['ruby']
|
37
|
+
article.tag_names.delete('ruby')
|
38
|
+
expect(article.tags.map(&:name)).to eq []
|
39
|
+
# does not delete the tag completely
|
40
|
+
expect(TagDangelo::Tag.all.map(&:name)).to eq ['ruby']
|
41
|
+
end
|
42
|
+
|
43
|
+
it "removes tag names" do
|
44
|
+
article.tag_names = ['devils', 'pie']
|
45
|
+
article.tag_names -= 'devils'
|
46
|
+
expect(article.tags.map(&:name)).to eq ['pie']
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/tag_dangelo.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'tag_dangelo'
|
3
|
-
s.version = '0.0
|
3
|
+
s.version = '0.1.0'
|
4
4
|
s.authors = ['Jim Harvey']
|
5
5
|
s.email = ['jimrharvey@gmail.com']
|
6
6
|
s.homepage = 'https://github.com/harvj/tag_dangelo'
|
7
|
-
s.summary = 'Tags with
|
8
|
-
s.description = 'Add tags to your
|
7
|
+
s.summary = 'Tags with soul'
|
8
|
+
s.description = 'Add tags to your ActiveRecord models.'
|
9
9
|
|
10
10
|
s.files = `git ls-files`.split("\n")
|
11
11
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
@@ -15,4 +15,5 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.add_development_dependency 'combustion', '~> 0.4.0'
|
16
16
|
s.add_development_dependency 'rspec-rails', '~> 2.13'
|
17
17
|
s.add_development_dependency 'sqlite3', '~> 1.3.7'
|
18
|
+
s.add_development_dependency 'pry', '~> 0.9'
|
18
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tag_dangelo
|
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:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -75,7 +75,23 @@ dependencies:
|
|
75
75
|
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: 1.3.7
|
78
|
-
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0.9'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0.9'
|
94
|
+
description: Add tags to your ActiveRecord models.
|
79
95
|
email:
|
80
96
|
- jimrharvey@gmail.com
|
81
97
|
executables: []
|
@@ -90,6 +106,8 @@ files:
|
|
90
106
|
- lib/tag_dangelo.rb
|
91
107
|
- lib/tag_dangelo/active_record.rb
|
92
108
|
- lib/tag_dangelo/engine.rb
|
109
|
+
- lib/tag_dangelo/tag_names.rb
|
110
|
+
- spec/acceptance/tag_names_spec.rb
|
93
111
|
- spec/acceptance/tags_spec.rb
|
94
112
|
- spec/internal/app/models/article.rb
|
95
113
|
- spec/internal/config/database.yml
|
@@ -120,5 +138,5 @@ rubyforge_project:
|
|
120
138
|
rubygems_version: 1.8.25
|
121
139
|
signing_key:
|
122
140
|
specification_version: 3
|
123
|
-
summary: Tags with
|
141
|
+
summary: Tags with soul
|
124
142
|
test_files: []
|