simple_hashtag 0.1.6 → 0.1.8
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/README.md +0 -11
- data/lib/simple_hashtag/hashtag.rb +14 -7
- data/lib/simple_hashtag/version.rb +1 -1
- data/spec/simple_hashtag_spec.rb +19 -8
- 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: 92cf359e8763bc3f5110ceecae2df02a19de730e
|
|
4
|
+
data.tar.gz: 2745a7c66d0eb823ff9d36291fb081f95b8dc6e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8310a4c6ae2144c15bde96ff7bcba20ed8f422e8d03d375dc0327ef36e16ce5463bcf2c52ef23b91cd319695c76e9f2476a2cd3d9f4d24d1987ad9a9a5df65e2
|
|
7
|
+
data.tar.gz: d0236caaa76693f6244f6b1f8edaf6864e5fc1dc3267b60175344c40e790078057ebf2de94f3f6fc3d1b47b1b94668f369292a9d4c48c3afababe77a8446ba32
|
data/README.md
CHANGED
|
@@ -142,8 +142,6 @@ Improvements for this method are listed in the Todo section below.
|
|
|
142
142
|
## Gotchas
|
|
143
143
|
### Association Query
|
|
144
144
|
The association between a Hashtag and your models is a polymorphic many-to-many.
|
|
145
|
-
When querying the polymorphic association from the other side (tag.hashtaggables),
|
|
146
|
-
we perform a DB query for each hashtaggable, resulting in an n+1 query.
|
|
147
145
|
|
|
148
146
|
The object returned by the query is an array, not an Arel query, so you can't chain (i.e.: to specify the order), and should do it by hand:
|
|
149
147
|
|
|
@@ -153,15 +151,6 @@ posts_and_picts = hashtag.hattaggables
|
|
|
153
151
|
posts_and_picts.sort_by! { |p| p.created_at }
|
|
154
152
|
```
|
|
155
153
|
|
|
156
|
-
To avoid the N+1 query problem, use the hashtagged_ids_for_type(type) method
|
|
157
|
-
to retrieve the IDs for the items instead:
|
|
158
|
-
|
|
159
|
-
```ruby
|
|
160
|
-
hashtag = SimpleHashtag.find_by_name("RubyRocks")
|
|
161
|
-
@comment_ids = @hashtag.hashtagged_ids_for_type("Comment") if @hashtag
|
|
162
|
-
@comments = Comment.where(:id => @hashtagged_elements)
|
|
163
|
-
```
|
|
164
|
-
|
|
165
154
|
### find_by
|
|
166
155
|
|
|
167
156
|
To preserve coherence, Hashtags are stored downcased.
|
|
@@ -28,17 +28,24 @@ module SimpleHashtag
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def hashtaggables
|
|
31
|
-
self.hashtaggings.collect { |h| h.hashtaggable }
|
|
31
|
+
self.hashtaggings.includes(:hashtaggable).collect { |h| h.hashtaggable }
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
def
|
|
35
|
-
|
|
34
|
+
def hashtagged_types
|
|
35
|
+
self.hashtaggings.pluck(:hashtaggable_type).uniq
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def hashtagged_ids_by_types
|
|
39
|
+
hashtagged_ids ||= {}
|
|
36
40
|
self.hashtaggings.each do |h|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
41
|
+
hashtagged_ids[h.hashtaggable_type] ||= Array.new
|
|
42
|
+
hashtagged_ids[h.hashtaggable_type] << h.hashtaggable_id
|
|
40
43
|
end
|
|
41
|
-
|
|
44
|
+
hashtagged_ids
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def hashtagged_ids_for_type(type)
|
|
48
|
+
hashtagged_ids_by_types[type]
|
|
42
49
|
end
|
|
43
50
|
|
|
44
51
|
def to_s
|
data/spec/simple_hashtag_spec.rb
CHANGED
|
@@ -41,14 +41,25 @@ describe SimpleHashtag do
|
|
|
41
41
|
tag.name.should eq "weirdcase"
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
context "knows about its hashtaggables" do
|
|
45
|
+
before do
|
|
46
|
+
Post.create(body: "I thought up an ending for my #book.")
|
|
47
|
+
Picture.create(caption: "Some who have read the #book.")
|
|
48
|
+
end
|
|
49
|
+
it "their numbers" do
|
|
50
|
+
tag = SimpleHashtag::Hashtag.find_by_name("book")
|
|
51
|
+
tag.hashtaggables.size.should eq 2
|
|
52
|
+
tag.hashtaggables.first.body.should eq "I thought up an ending for my #book."
|
|
53
|
+
tag.hashtaggables.last.caption.should eq "Some who have read the #book."
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "their types" do
|
|
57
|
+
tag = SimpleHashtag::Hashtag.find_by_name("book")
|
|
58
|
+
tag.hashtagged_types.size.should eq 2
|
|
59
|
+
tag.hashtagged_types.should eq ["Post", "Picture"]
|
|
60
|
+
tag.hashtagged_ids_for_type("Post").should include(Post.last.id)
|
|
61
|
+
tag.hashtagged_ids_for_type("Picture").should include(Picture.last.id)
|
|
62
|
+
end
|
|
52
63
|
end
|
|
53
64
|
|
|
54
65
|
it "can clean the DB from orphan tags" do
|