has_tags 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -0
- data/lib/has_tags/tag.rb +7 -1
- data/lib/has_tags/taggable.rb +18 -2
- data/lib/has_tags/version.rb +1 -1
- data/spec/tag_spec.rb +10 -0
- data/spec/taggable_spec.rb +39 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28e794b8649a411c1d4003a1bb1f5f2ef5213661
|
4
|
+
data.tar.gz: 435486ee9d2131acdbdef28cb73a463de1224fcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 600995d32c22bdddc71144d9e3e99d400dc937315173b2e71d51b9c516649260ddced1a8e47435ba5ae875653edcecd498cbb9f0d61aab2594c060076f3ef911
|
7
|
+
data.tar.gz: d6effdd771768b4bfa93dbc05cd0b21eaf80a7ae3459746907d29a7377d9f654007ba35e0d356fa307a9ff252da8286694149573bcb7b02089750c58652cbac4
|
data/README.md
CHANGED
@@ -49,6 +49,40 @@ end
|
|
49
49
|
|
50
50
|
<code>tag_list</code> should be a string with tags separated by commas, or colons to create a context tag.
|
51
51
|
|
52
|
+
To find all instances of an object associated with a certain tag, use the <code>tagged_with</code> class method, which takes an array of tag names to search by:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Post.tagged_with(["Sports"]) # => All posts tagged with "Sports"
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
Since it accepts an array of tag names, you can something like this:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
Post.tagged_with(["Sports", "Lacrosse", "2014"]) # => All posts tagged with "Sports", "Lacrosse", and "2014"
|
63
|
+
```
|
64
|
+
|
65
|
+
Input to <code>tagged_with</code> doesn't need to be an exact match, making it good for searching:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
search_term = params[:search_term] # Let's say a user input "sport"
|
69
|
+
|
70
|
+
Post.tagged_with([search_term]) # => All posts tagged with "Sports"
|
71
|
+
```
|
72
|
+
|
73
|
+
You can also get all of the top level tags for a specific class:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Post.top_level_tags # => All tags that are associated with a "Post" object and that have have no parent tags
|
77
|
+
```
|
78
|
+
|
79
|
+
Or for all classes that have tags:
|
80
|
+
|
81
|
+
```
|
82
|
+
HasTags::Tag.top_level_tags # => All tags that have no parent tags
|
83
|
+
```
|
84
|
+
|
85
|
+
|
52
86
|
### Examples
|
53
87
|
|
54
88
|
For creating top-level tags, a user can type in tags only separated by commas:
|
data/lib/has_tags/tag.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
module HasTags
|
2
2
|
class Tag < ::ActiveRecord::Base
|
3
|
+
#### Self referential association for contexts ####
|
3
4
|
belongs_to :context, class_name: "Tag"
|
4
|
-
|
5
5
|
has_many :tags, foreign_key: :context_id
|
6
|
+
###################################################
|
7
|
+
|
6
8
|
has_many :taggings
|
7
9
|
|
8
10
|
validates :name, uniqueness: true, presence: true
|
11
|
+
|
12
|
+
def self.top_level_tags
|
13
|
+
where(context_id: nil)
|
14
|
+
end
|
9
15
|
end
|
10
16
|
end
|
data/lib/has_tags/taggable.rb
CHANGED
@@ -24,10 +24,26 @@ module HasTags
|
|
24
24
|
include InstanceMethods
|
25
25
|
end
|
26
26
|
|
27
|
+
def tagged_with(tag_names)
|
28
|
+
instances = []
|
29
|
+
tag_names.each do |tag_name|
|
30
|
+
self.all.each do |instance|
|
31
|
+
if instance.tags.where("lower(name) LIKE ?", "%#{tag_name}%").present?
|
32
|
+
instances << instance
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
instances
|
37
|
+
end
|
38
|
+
|
39
|
+
def top_level_tags
|
40
|
+
HasTags::Tag.where(context_id: nil) && HasTags::Tag.all
|
41
|
+
.collect{|tag| tag if tag.taggings.where(taggable_type: self.to_s).present? }
|
42
|
+
end
|
43
|
+
|
27
44
|
module InstanceMethods
|
28
45
|
def save_taggings
|
29
|
-
taggings = HasTags::Tagging.where(taggable_type: self.class.to_s, taggable_id: nil)
|
30
|
-
puts taggings.map(&:attributes)
|
46
|
+
taggings = HasTags::Tagging.where(taggable_type: self.class.to_s, taggable_id: nil)
|
31
47
|
taggings.each do |tagging|
|
32
48
|
tagging.taggable_id = self.id
|
33
49
|
tagging.save
|
data/lib/has_tags/version.rb
CHANGED
data/spec/tag_spec.rb
CHANGED
@@ -15,5 +15,15 @@ describe HasTags::Tag do
|
|
15
15
|
HasTags::Tag.create(name: "Sports")
|
16
16
|
HasTags::Tag.create(name: "Sports")
|
17
17
|
expect(HasTags::Tag.all.count).to eq(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return all top level tags" do
|
21
|
+
tag1 = HasTags::Tag.create(name: "Sports")
|
22
|
+
tag2 = HasTags::Tag.create(name: "Lax", context_id: tag1.id)
|
23
|
+
tag3 = HasTags::Tag.create(name: "Hockey", context_id: tag1.id)
|
24
|
+
tag4 = HasTags::Tag.create(name: "Food")
|
25
|
+
|
26
|
+
expect(HasTags::Tag.top_level_tags).not_to include(tag2, tag3)
|
27
|
+
expect(HasTags::Tag.top_level_tags).to include(tag1, tag4)
|
18
28
|
end
|
19
29
|
end
|
data/spec/taggable_spec.rb
CHANGED
@@ -43,4 +43,43 @@ describe TaggableModel do
|
|
43
43
|
|
44
44
|
expect(model.tag_list).to eq("Sports, Lacrosse")
|
45
45
|
end
|
46
|
+
|
47
|
+
it "should retrieve all top level tags for class" do
|
48
|
+
model = TaggableModel.new(name: "post")
|
49
|
+
tag_names = "Sports:Lacrosse"
|
50
|
+
model.tag_list = tag_names
|
51
|
+
model.save
|
52
|
+
tag = model.tags.first
|
53
|
+
|
54
|
+
expect(TaggableModel.top_level_tags).to include(tag)
|
55
|
+
end
|
56
|
+
|
57
|
+
context "TaggableModel.tagged_with" do
|
58
|
+
it "should return all intances of it's class that are tagged with a specific tag" do
|
59
|
+
model = TaggableModel.new(name: "post")
|
60
|
+
tag_names = "Sports:Lacrosse"
|
61
|
+
model.tag_list = tag_names
|
62
|
+
model.save
|
63
|
+
|
64
|
+
expect(TaggableModel.tagged_with(["sport"])).to include(model)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should return tag with similar title" do
|
68
|
+
model = TaggableModel.new(name: "post")
|
69
|
+
tag_names = "Sports:Lacrosse"
|
70
|
+
model.tag_list = tag_names
|
71
|
+
model.save
|
72
|
+
|
73
|
+
expect(TaggableModel.tagged_with(["Sports"])).to include(model)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should accept multiple tags to search by" do
|
77
|
+
model = TaggableModel.new(name: "post")
|
78
|
+
tag_names = "Sports:Lacrosse"
|
79
|
+
model.tag_list = tag_names
|
80
|
+
model.save
|
81
|
+
|
82
|
+
expect(TaggableModel.tagged_with(["sports", "lacrosse"])).to include(model)
|
83
|
+
end
|
84
|
+
end
|
46
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_tags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Garrett Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|