simple_hashtag 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eec58dc17a78471028537ef4fc03ea4a446db720
4
- data.tar.gz: 50e1b28583c8993d4e43cedeaaaa1685cd971c4f
3
+ metadata.gz: 92cf359e8763bc3f5110ceecae2df02a19de730e
4
+ data.tar.gz: 2745a7c66d0eb823ff9d36291fb081f95b8dc6e8
5
5
  SHA512:
6
- metadata.gz: 98b8e1d7a86a89236b1d60841902cad7fd44f9c06279320dddb660d6c177317b248b7d4b5ba7b90d296b4bceedf5a6ce911a4ff58a285c309658dd067cfddbfc
7
- data.tar.gz: 2b5291d27256460be5efda4a3c389928ae40fe4f7c38ca450bec62d8cb8d18b7b4dfc4fa96780aa7aa4afdf5c53e457efba0e9029119bba49ee33f3c4c508d29
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 hashtagged_ids_for_type(type)
35
- hashtagged_ids ||= Array.new
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
- if h.hashtaggable_type == type
38
- hashtagged_ids << h.hashtaggable_id
39
- end
41
+ hashtagged_ids[h.hashtaggable_type] ||= Array.new
42
+ hashtagged_ids[h.hashtaggable_type] << h.hashtaggable_id
40
43
  end
41
- return hashtagged_ids
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
@@ -1,3 +1,3 @@
1
1
  module SimpleHashtag
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -41,14 +41,25 @@ describe SimpleHashtag do
41
41
  tag.name.should eq "weirdcase"
42
42
  end
43
43
 
44
- it "knows about its hashtaggables" do
45
- Post.create(body: "I thought up an ending for my #book.")
46
- Picture.create(caption: "Some who have read the #book.")
47
-
48
- tag = SimpleHashtag::Hashtag.find_by_name("book")
49
- tag.hashtaggables.size.should eq 2
50
- tag.hashtaggables.first.body.should eq "I thought up an ending for my #book."
51
- tag.hashtaggables.last.caption.should eq "Some who have read the #book."
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_hashtag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Campardou