simple_hashtag 0.1.5 → 0.1.6
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 -25
- data/lib/simple_hashtag/hashtag.rb +10 -0
- data/lib/simple_hashtag/version.rb +1 -1
- 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: eec58dc17a78471028537ef4fc03ea4a446db720
|
4
|
+
data.tar.gz: 50e1b28583c8993d4e43cedeaaaa1685cd971c4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98b8e1d7a86a89236b1d60841902cad7fd44f9c06279320dddb660d6c177317b248b7d4b5ba7b90d296b4bceedf5a6ce911a4ff58a285c309658dd067cfddbfc
|
7
|
+
data.tar.gz: 2b5291d27256460be5efda4a3c389928ae40fe4f7c38ca450bec62d8cb8d18b7b4dfc4fa96780aa7aa4afdf5c53e457efba0e9029119bba49ee33f3c4c508d29
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Ruby gem for Rails that parses, stores, retreives and formats hashtags in your model.
|
7
7
|
|
8
|
-
_Simple Hashtag_ is a mix between–well–hashtags, as we know them, and categories.
|
8
|
+
_Simple Hashtag_ is a mix between–well–hashtags, as we know them, and categories.
|
9
9
|
It will scan your Active Record attribute for a hashtag, store it in an index, and display a page with each object containing the tag.
|
10
10
|
|
11
11
|
It's simple, and like all things simple, it can create a nice effect, quickly.
|
@@ -27,21 +27,21 @@ Then you have to generate the migration files:
|
|
27
27
|
$ rails g simple_hashtag:migration
|
28
28
|
```
|
29
29
|
|
30
|
-
This will create two migration files, one for the `hashtags` table and one for the `hashtagging` table.
|
30
|
+
This will create two migration files, one for the `hashtags` table and one for the `hashtagging` table.
|
31
31
|
You will need to run `rake db:migrate` to actually create the tables.
|
32
32
|
|
33
|
-
__Optionnally__, you can create views,
|
33
|
+
__Optionnally__, you can create views,
|
34
34
|
if only to guide you through your own implementation:
|
35
35
|
```shell
|
36
36
|
$ rails g simple_hashtag:views
|
37
37
|
```
|
38
38
|
|
39
|
-
This will create a __basic controller__, a __short index view__ and a __small helper__.
|
39
|
+
This will create a __basic controller__, a __short index view__ and a __small helper__.
|
40
40
|
It assume your views follow the convention of having a directory named after your model's plural, and a partial named after your model's name.
|
41
41
|
```
|
42
42
|
app
|
43
43
|
|-- views
|
44
|
-
| |-- posts
|
44
|
+
| |-- posts
|
45
45
|
| | |-- _post.html.erb
|
46
46
|
```
|
47
47
|
|
@@ -50,7 +50,7 @@ app
|
|
50
50
|
|
51
51
|
Just add `include SimpleHashtag::Hashtaggable` in your model.
|
52
52
|
|
53
|
-
_Simple Hasthag_ will parse the `body` attribute by default:
|
53
|
+
_Simple Hasthag_ will parse the `body` attribute by default:
|
54
54
|
|
55
55
|
```ruby
|
56
56
|
class Post < ActiveRecord::Base
|
@@ -59,7 +59,7 @@ end
|
|
59
59
|
```
|
60
60
|
|
61
61
|
|
62
|
-
If you need to parse another attribute instead,
|
62
|
+
If you need to parse another attribute instead,
|
63
63
|
add `hashtaggable_attribute` followed by the name of your attribute, i.e.:
|
64
64
|
```ruby
|
65
65
|
class Picture < ActiveRecord::Base
|
@@ -68,12 +68,12 @@ class Picture < ActiveRecord::Base
|
|
68
68
|
end
|
69
69
|
```
|
70
70
|
|
71
|
-
From here on, if your text contains a hashtag, say _#RubyRocks_,
|
72
|
-
_Simple Hasthag_ will find it, store it in a table and retreive it and its associated object if asked.
|
71
|
+
From here on, if your text contains a hashtag, say _#RubyRocks_,
|
72
|
+
_Simple Hasthag_ will find it, store it in a table and retreive it and its associated object if asked.
|
73
73
|
Helpers are also available to create a link when displaying the text.
|
74
74
|
|
75
75
|
### Controller and Views
|
76
|
-
If you don't want to bother looking at the genrerated controller and views, here is a quick peek.
|
76
|
+
If you don't want to bother looking at the genrerated controller and views, here is a quick peek.
|
77
77
|
In a Controller, display all hashtags, or search for a Hashtag and its associated records:
|
78
78
|
```ruby
|
79
79
|
class HashtagsController < ApplicationController
|
@@ -129,20 +129,20 @@ The helper generating the link relies on it.
|
|
129
129
|
|
130
130
|
|
131
131
|
### Spring Cleaning
|
132
|
-
There is a class method `SimpleHashtag::Hashtag#clean_orphans` to remove unused hashtags from the DB.
|
133
|
-
It is currently not hooked, for two reasons:
|
134
|
-
- It is not optimised at all, DB-wise.
|
135
|
-
- Destructive method should be called explicitly.
|
132
|
+
There is a class method `SimpleHashtag::Hashtag#clean_orphans` to remove unused hashtags from the DB.
|
133
|
+
It is currently not hooked, for two reasons:
|
134
|
+
- It is not optimised at all, DB-wise.
|
135
|
+
- Destructive method should be called explicitly.
|
136
136
|
|
137
|
-
Knowing all this, you can hook it after each change, or automate it with a Cron job, or even spring-clean manually once in a while.
|
137
|
+
Knowing all this, you can hook it after each change, or automate it with a Cron job, or even spring-clean manually once in a while.
|
138
138
|
|
139
139
|
Improvements for this method are listed in the Todo section below.
|
140
140
|
|
141
141
|
|
142
142
|
## Gotchas
|
143
143
|
### Association Query
|
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),
|
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
146
|
we perform a DB query for each hashtaggable, resulting in an n+1 query.
|
147
147
|
|
148
148
|
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:
|
@@ -153,11 +153,20 @@ posts_and_picts = hashtag.hattaggables
|
|
153
153
|
posts_and_picts.sort_by! { |p| p.created_at }
|
154
154
|
```
|
155
155
|
|
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
|
+
|
156
165
|
### find_by
|
157
166
|
|
158
|
-
To preserve coherence, Hashtags are stored downcased.
|
159
|
-
To ensure coherence, they are also searched downcased.
|
160
|
-
Internally, the model overrides `find_by_name` to perform the downcase query.
|
167
|
+
To preserve coherence, Hashtags are stored downcased.
|
168
|
+
To ensure coherence, they are also searched downcased.
|
169
|
+
Internally, the model overrides `find_by_name` to perform the downcase query.
|
161
170
|
Should you search Hashtags manually you should use the `SimpleHashtag::Hashtag#find_by_name` method, instead of `SimpleHashtag::Hashtag.find_by(name: 'RubyRocks')`
|
162
171
|
|
163
172
|
|
@@ -166,9 +175,9 @@ Should you search Hashtags manually you should use the `SimpleHashtag::Hashtag#f
|
|
166
175
|
_Simple Hashtag_ is in its very early stage and would need a lot of love to reach 1.0.0.
|
167
176
|
Among the many improvement areas:
|
168
177
|
|
169
|
-
- Make the Regex that parses the text for the hashtag much more robust.
|
170
|
-
This is how Twitter does it:
|
171
|
-
[https://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb](https://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb)
|
178
|
+
- Make the Regex that parses the text for the hashtag much more robust.
|
179
|
+
This is how Twitter does it:
|
180
|
+
[https://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb](https://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb)
|
172
181
|
(Yes, that's 362 lines of regex. Neat.)
|
173
182
|
- Allow for multiple hashtagable attributes on the same model
|
174
183
|
- Allow a change in the name of the classes and tables to avoid conflicts
|
@@ -179,8 +188,8 @@ Among the many improvement areas:
|
|
179
188
|
|
180
189
|
## Contributing
|
181
190
|
|
182
|
-
All contributions are welcome.
|
183
|
-
I might not develop new features (unless a project does require it),
|
191
|
+
All contributions are welcome.
|
192
|
+
I might not develop new features (unless a project does require it),
|
184
193
|
but I will definitely merge any interesting feature or bug fix quickly.
|
185
194
|
|
186
195
|
You know the drill:
|
@@ -31,6 +31,16 @@ module SimpleHashtag
|
|
31
31
|
self.hashtaggings.collect { |h| h.hashtaggable }
|
32
32
|
end
|
33
33
|
|
34
|
+
def hashtagged_ids_for_type(type)
|
35
|
+
hashtagged_ids ||= Array.new
|
36
|
+
self.hashtaggings.each do |h|
|
37
|
+
if h.hashtaggable_type == type
|
38
|
+
hashtagged_ids << h.hashtaggable_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return hashtagged_ids
|
42
|
+
end
|
43
|
+
|
34
44
|
def to_s
|
35
45
|
name
|
36
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_hashtag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raphael Campardou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|