has_tags 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/has_tags/taggable.rb +12 -2
- data/lib/has_tags/version.rb +1 -1
- data/spec/taggable_spec.rb +10 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa564f912468812fe9593203b9e5c699726b42b7
|
4
|
+
data.tar.gz: d9678862c27bbc193e174562c31de35959c83a83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb406ba5cda841342fa0761cb8e7ba04476dcbc6e3720d42b47ba75be8eb47efa09b515931cc441be044645d8c4ad7496084853d0bf5359b8e0c33a0b755f83d
|
7
|
+
data.tar.gz: 7ea802c4e3a5f0d27909dc47e2f9cd20ee301dc54b8aaf9b880951f0c0dcb49eaa30bd091399712fe89f6ea6a6f6a119180c5af890057997d34fcdca3df56441
|
data/lib/has_tags/taggable.rb
CHANGED
@@ -16,6 +16,8 @@ module HasTags
|
|
16
16
|
# end
|
17
17
|
|
18
18
|
def has_tags
|
19
|
+
after_save :save_taggings
|
20
|
+
|
19
21
|
has_many :taggings, as: :taggable, class_name: "HasTags::Tagging"
|
20
22
|
has_many :tags, through: :taggings, class_name: "HasTags::Tag"
|
21
23
|
|
@@ -23,6 +25,14 @@ module HasTags
|
|
23
25
|
end
|
24
26
|
|
25
27
|
module InstanceMethods
|
28
|
+
def save_taggings
|
29
|
+
taggings = HasTags::Tagging.where(taggable_type: self.class.to_s, taggable_id: nil)
|
30
|
+
taggings.each do |tagging|
|
31
|
+
tagging.taggable_id = self.id
|
32
|
+
tagging.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
26
36
|
def tag_list
|
27
37
|
tags.map(&:name).join(", ")
|
28
38
|
end
|
@@ -35,8 +45,8 @@ module HasTags
|
|
35
45
|
tag = HasTags::Tag.where(name: name).first_or_create!
|
36
46
|
else
|
37
47
|
tag = HasTags::Tag.where(name: name, context_id: Tag.find_by(name: names[index-1]).id).first_or_create!
|
38
|
-
end
|
39
|
-
HasTags::Tagging.where(tag_id: tag.id,
|
48
|
+
end
|
49
|
+
HasTags::Tagging.where(tag_id: tag.id, taggable_type: self.class.to_s).first_or_create!
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
data/lib/has_tags/version.rb
CHANGED
data/spec/taggable_spec.rb
CHANGED
@@ -1,37 +1,45 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe TaggableModel do
|
4
|
-
let (:model) { TaggableModel.
|
4
|
+
#let (:model) { TaggableModel.new(name: "post") }
|
5
5
|
|
6
6
|
it { should have_many(:taggings) }
|
7
7
|
it { should have_many(:tags) }
|
8
8
|
|
9
9
|
context "setting tag list" do
|
10
10
|
it "should be able to use two separate tags" do
|
11
|
+
model = TaggableModel.new(name: "post")
|
11
12
|
tag_names = "Sports, Food"
|
12
13
|
model.tag_list = tag_names
|
14
|
+
model.save
|
13
15
|
|
14
16
|
expect(model.tags.count).to eq(2)
|
15
17
|
end
|
16
18
|
|
17
19
|
it "should create a context tag with colon" do
|
20
|
+
model = TaggableModel.new(name: "post")
|
18
21
|
tag_names = "Sports:Lacrosse"
|
19
22
|
model.tag_list = tag_names
|
23
|
+
model.save
|
20
24
|
|
21
25
|
expect(HasTags::Tag.last.context.name).to eq("Sports")
|
22
26
|
end
|
23
|
-
|
27
|
+
|
24
28
|
it "should support multiple nested contexts" do
|
29
|
+
model = TaggableModel.new(name: "post")
|
25
30
|
tag_names = "Sports:Lacrosse:Plays"
|
26
31
|
model.tag_list = tag_names
|
32
|
+
model.save
|
27
33
|
|
28
34
|
expect(HasTags::Tag.last.context.name).to eq("Lacrosse")
|
29
35
|
end
|
30
36
|
end
|
31
37
|
|
32
38
|
it "should retrieve tag list" do
|
39
|
+
model = TaggableModel.new(name: "post")
|
33
40
|
tag_names = "Sports:Lacrosse"
|
34
41
|
model.tag_list = tag_names
|
42
|
+
model.save
|
35
43
|
|
36
44
|
expect(model.tag_list).to eq("Sports, Lacrosse")
|
37
45
|
end
|