simple_hashtag_2 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +191 -0
- data/Rakefile +6 -0
- data/lib/generators/simple_hashtag/migration_generator.rb +20 -0
- data/lib/generators/simple_hashtag/templates/migrations/create_hashtaggings_migration.rb +11 -0
- data/lib/generators/simple_hashtag/templates/migrations/create_hashtags_migration.rb +10 -0
- data/lib/generators/simple_hashtag/templates/views/hashtags_controller.rb +12 -0
- data/lib/generators/simple_hashtag/templates/views/hashtags_helper.rb +16 -0
- data/lib/generators/simple_hashtag/templates/views/hashtags_index.html.erb +6 -0
- data/lib/generators/simple_hashtag/templates/views/hashtags_show.html.erb +8 -0
- data/lib/generators/simple_hashtag/views_generator.rb +18 -0
- data/lib/simple_hashtag/hashtag.rb +64 -0
- data/lib/simple_hashtag/hashtaggable.rb +45 -0
- data/lib/simple_hashtag/hashtagging.rb +8 -0
- data/lib/simple_hashtag/version.rb +3 -0
- data/lib/simple_hashtag.rb +6 -0
- data/simple_hashtag.gemspec +29 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/picture.rb +5 -0
- data/spec/dummy/app/models/post.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +56 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +2 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20130919041824_create_posts.rb +9 -0
- data/spec/dummy/db/migrate/20130919041825_create_pictures.rb +9 -0
- data/spec/dummy/db/migrate/20131004085836_create_simple_hashtag_hashtags.rb +9 -0
- data/spec/dummy/db/migrate/20131004085907_create_simple_hashtag_hashtaggings.rb +10 -0
- data/spec/dummy/db/schema.rb +43 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/simple_hashtag_spec.rb +145 -0
- data/spec/spec_helper.rb +13 -0
- metadata +231 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 208e2f35d55cf68aef44b06014fa6468d09dab3d
|
4
|
+
data.tar.gz: 112511d9ebb2226ffbb156dffff19dee06893f34
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85864089d6e05299e4e035a25c0a6be56e7298a89157342c4e9b129fbb02be59dce732004ea88e6da1e75d8bdbf2b4245b3413049f329d2726fd54c4782a6ff4
|
7
|
+
data.tar.gz: 377c7b57cc5cd65ed9cdca804dcf0fc7a4159883c1246f4772af534267e8e501e9c2067f30e9156eb9a41deaad5c5ee2bb2a1d84d94fba352a0ba11362abf75c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Raphael Campardou
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# SimpleHashtag
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/simple_hashtag.png)](http://badge.fury.io/rb/simple_hashtag)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/ralovely/simple_hashtag.png)](https://codeclimate.com/github/ralovely/simple_hashtag)
|
5
|
+
|
6
|
+
Ruby gem for Rails that parses, stores, retreives and formats hashtags in your model.
|
7
|
+
|
8
|
+
_Simple Hashtag_ is a mix between–well–hashtags, as we know them, and categories.
|
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
|
+
|
11
|
+
It's simple, and like all things simple, it can create a nice effect, quickly.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
```ruby
|
17
|
+
gem 'simple_hashtag'
|
18
|
+
```
|
19
|
+
|
20
|
+
And execute:
|
21
|
+
```shell
|
22
|
+
$ bundle
|
23
|
+
```
|
24
|
+
|
25
|
+
Then you have to generate the migration files:
|
26
|
+
```shell
|
27
|
+
$ rails g simple_hashtag:migration
|
28
|
+
```
|
29
|
+
|
30
|
+
This will create two migration files, one for the `hashtags` table and one for the `hashtagging` table.
|
31
|
+
You will need to run `rake db:migrate` to actually create the tables.
|
32
|
+
|
33
|
+
__Optionnally__, you can create views,
|
34
|
+
if only to guide you through your own implementation:
|
35
|
+
```shell
|
36
|
+
$ rails g simple_hashtag:views
|
37
|
+
```
|
38
|
+
|
39
|
+
This will create a __basic controller__, a __short index view__ and a __small helper__.
|
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
|
+
```
|
42
|
+
app
|
43
|
+
|-- views
|
44
|
+
| |-- posts
|
45
|
+
| | |-- _post.html.erb
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
## Usage
|
50
|
+
|
51
|
+
Just add `include SimpleHashtag::Hashtaggable` in your model.
|
52
|
+
|
53
|
+
_Simple Hasthag_ will parse the `body` attribute by default:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Post < ActiveRecord::Base
|
57
|
+
include SimpleHashtag::Hashtaggable
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
If you need to parse another attribute instead,
|
63
|
+
add `hashtaggable_attribute` followed by the name of your attribute, i.e.:
|
64
|
+
```ruby
|
65
|
+
class Picture < ActiveRecord::Base
|
66
|
+
include SimpleHashtag::Hashtaggable
|
67
|
+
hashtaggable_attribute :caption
|
68
|
+
end
|
69
|
+
```
|
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.
|
73
|
+
Helpers are also available to create a link when displaying the text.
|
74
|
+
|
75
|
+
### Controller and Views
|
76
|
+
If you don't want to bother looking at the genrerated controller and views, here is a quick peek.
|
77
|
+
In a Controller, display all hashtags, or search for a Hashtag and its associated records:
|
78
|
+
```ruby
|
79
|
+
class HashtagsController < ApplicationController
|
80
|
+
def index
|
81
|
+
@hashtags = SimpleHashtag::Hashtag.all
|
82
|
+
end
|
83
|
+
|
84
|
+
def show
|
85
|
+
@hashtag = SimpleHashtag::Hashtag.find_by_name(params[:hashtag])
|
86
|
+
@hashtagged = @hashtag.hashtaggables if @hashtag
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
The views could resemble something like this:
|
92
|
+
|
93
|
+
Index:
|
94
|
+
```erb
|
95
|
+
<h1>Hashtags</h1>
|
96
|
+
<ul>
|
97
|
+
<% @hashtags.each do |hashtag| %>
|
98
|
+
<li><%= link_to hashtag.name, hashtag_path(hashtag.name) %></li>
|
99
|
+
<% end -%>
|
100
|
+
</ul>
|
101
|
+
```
|
102
|
+
|
103
|
+
Show:
|
104
|
+
```erb
|
105
|
+
<h1><%= params[:hashtag] %></h1>
|
106
|
+
<% if @hashtagged %>
|
107
|
+
<% @hashtagged.each do |hashtagged| %>
|
108
|
+
<% view = hashtagged.class.to_s.underscore.pluralize %>
|
109
|
+
<% partial = hashtagged.class.to_s.underscore %>
|
110
|
+
<%= render "#{view}/#{partial}", {partial.to_sym => hashtagged} %>
|
111
|
+
<% end -%>
|
112
|
+
<% else -%>
|
113
|
+
<p>There is no match for the <em><%= params[:hashtag] %></em> hashtag.</p>
|
114
|
+
<% end -%>
|
115
|
+
```
|
116
|
+
In the gem it is actually extracted in a helper.
|
117
|
+
|
118
|
+
|
119
|
+
### Routes
|
120
|
+
|
121
|
+
If you use the provided controller and views, the generator will add two routes to your app:
|
122
|
+
```ruby
|
123
|
+
get 'hashtags/', to: 'hashtags#index', as: :hashtags
|
124
|
+
get 'hashtags/:hashtag', to: 'hashtags#show', as: :hashtag
|
125
|
+
```
|
126
|
+
|
127
|
+
The helper generating the link relies on it.
|
128
|
+
|
129
|
+
|
130
|
+
|
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.
|
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.
|
138
|
+
|
139
|
+
Improvements for this method are listed in the Todo section below.
|
140
|
+
|
141
|
+
|
142
|
+
## Gotchas
|
143
|
+
### Association Query
|
144
|
+
The association between a Hashtag and your models is a polymorphic many-to-many.
|
145
|
+
|
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:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
hashtag = SimpleHashtag.find_by_name("RubyRocks")
|
150
|
+
posts_and_picts = hashtag.hattaggables
|
151
|
+
posts_and_picts.sort_by! { |p| p.created_at }
|
152
|
+
```
|
153
|
+
|
154
|
+
### find_by
|
155
|
+
|
156
|
+
To preserve coherence, Hashtags are stored downcased.
|
157
|
+
To ensure coherence, they are also searched downcased.
|
158
|
+
Internally, the model overrides `find_by_name` to perform the downcase query.
|
159
|
+
Should you search Hashtags manually you should use the `SimpleHashtag::Hashtag#find_by_name` method, instead of `SimpleHashtag::Hashtag.find_by(name: 'RubyRocks')`
|
160
|
+
|
161
|
+
|
162
|
+
## ToDo
|
163
|
+
|
164
|
+
_Simple Hashtag_ is in its very early stage and would need a lot of love to reach 1.0.0.
|
165
|
+
Among the many improvement areas:
|
166
|
+
|
167
|
+
- Make the Regex that parses the text for the hashtag much more robust.
|
168
|
+
This is how Twitter does it:
|
169
|
+
[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)
|
170
|
+
(Yes, that's 362 lines of regex. Neat.)
|
171
|
+
- Allow for multiple hashtagable attributes on the same model
|
172
|
+
- Allow a change in the name of the classes and tables to avoid conflicts
|
173
|
+
- Make it ORM agnostic
|
174
|
+
- Add an option so the helper displays the `#` or not, global or per model
|
175
|
+
- Add an option to clean orphans after each edit or not
|
176
|
+
- Improve the `SimpleHashtag::Hashtag#clean_orphans` method to do only one SQL query
|
177
|
+
|
178
|
+
## Contributing
|
179
|
+
|
180
|
+
All contributions are welcome.
|
181
|
+
I might not develop new features (unless a project does require it),
|
182
|
+
but I will definitely merge any interesting feature or bug fix quickly.
|
183
|
+
|
184
|
+
You know the drill:
|
185
|
+
|
186
|
+
1. Fork it
|
187
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
188
|
+
3. Add passing tests
|
189
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
190
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
191
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module SimpleHashtag
|
4
|
+
module Generators
|
5
|
+
class MigrationGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def self.next_migration_number(path)
|
11
|
+
ActiveRecord::Generators::Base.next_migration_number(path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_migration
|
15
|
+
migration_template "migrations/create_hashtags_migration.rb", "db/migrate/create_simple_hashtag_hashtags.rb"
|
16
|
+
migration_template "migrations/create_hashtaggings_migration.rb", "db/migrate/create_simple_hashtag_hashtaggings.rb"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This migration comes from simple_hashtag
|
2
|
+
class CreateSimpleHashtagHashtaggings < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :simple_hashtag_hashtaggings do |t|
|
5
|
+
t.references :hashtag, :index => true
|
6
|
+
t.references :hashtaggable, :polymorphic => true
|
7
|
+
end
|
8
|
+
add_index :simple_hashtag_hashtaggings, ["hashtaggable_id", "hashtaggable_type"],
|
9
|
+
:name => 'index_hashtaggings_hashtaggable_id_hashtaggable_type'
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class HashtagsController < ApplicationController
|
2
|
+
|
3
|
+
def index
|
4
|
+
@hashtags = SimpleHashtag::Hashtag.all
|
5
|
+
end
|
6
|
+
|
7
|
+
def show
|
8
|
+
@hashtag = SimpleHashtag::Hashtag.find_by_name(params[:hashtag])
|
9
|
+
@hashtagged = @hashtag.hashtaggables if @hashtag
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module HashtagsHelper
|
2
|
+
def linkify_hashtags(hashtaggable_content)
|
3
|
+
regex = SimpleHashtag::Hashtag::HASHTAG_REGEX
|
4
|
+
hashtagged_content = hashtaggable_content.to_s.gsub(regex) do
|
5
|
+
link_to($&, hashtag_path($2), {class: :hashtag})
|
6
|
+
end
|
7
|
+
hashtagged_content.html_safe
|
8
|
+
end
|
9
|
+
|
10
|
+
def render_hashtaggable(hashtaggable)
|
11
|
+
klass = hashtaggable.class.to_s.underscore
|
12
|
+
view_dirname = klass.pluralize
|
13
|
+
partial = klass
|
14
|
+
render "#{view_dirname}/#{partial}", {klass.to_sym => hashtaggable}
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SimpleHashtag
|
2
|
+
module Generators
|
3
|
+
class ViewsGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def generate_views
|
8
|
+
copy_file "views/hashtags_controller.rb", "app/controllers/hashtags_controller.rb"
|
9
|
+
copy_file "views/hashtags_helper.rb", "app/helpers/hashtags_helper.rb"
|
10
|
+
copy_file "views/hashtags_index.html.erb", "app/views/hashtags/index.html.erb"
|
11
|
+
copy_file "views/hashtags_show.html.erb", "app/views/hashtags/show.html.erb"
|
12
|
+
|
13
|
+
route 'get "hashtags", to: "hashtags#index", as: :hashtags'
|
14
|
+
route 'get "hashtags/:hashtag", to: "hashtags#show", as: :hashtag'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "protected_attributes"
|
2
|
+
module SimpleHashtag
|
3
|
+
class Hashtag < ActiveRecord::Base
|
4
|
+
self.table_name = "simple_hashtag_hashtags"
|
5
|
+
|
6
|
+
has_many :hashtaggings
|
7
|
+
|
8
|
+
validates :name, uniqueness: true
|
9
|
+
attr_accessible :name
|
10
|
+
|
11
|
+
# TODO Beef up the regex (ie.:what if content is HTML)
|
12
|
+
# this is how Twitter does it:
|
13
|
+
# https://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/regex.rb
|
14
|
+
HASHTAG_REGEX = /(?:\s|^)(#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$))([a-z0-9\-_]+))/i
|
15
|
+
|
16
|
+
def self.find_by_name(name)
|
17
|
+
Hashtag.where("lower(name) =?", name.downcase).first
|
18
|
+
end
|
19
|
+
def self.find_or_create_by_name(name, &block)
|
20
|
+
find_by_name(name) || create(name: name, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def name=(val)
|
25
|
+
write_attribute(:name, val.downcase)
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
read_attribute(:name).downcase
|
30
|
+
end
|
31
|
+
|
32
|
+
def hashtaggables(conditions = {})
|
33
|
+
self.hashtaggings.includes(:hashtaggable).where(conditions).collect {|h| h.hashtaggable}
|
34
|
+
end
|
35
|
+
|
36
|
+
def hashtagged_types
|
37
|
+
self.hashtaggings.pluck(:hashtaggable_type).uniq
|
38
|
+
end
|
39
|
+
|
40
|
+
def hashtagged_ids_by_types
|
41
|
+
hashtagged_ids ||= {}
|
42
|
+
self.hashtaggings.each do |h|
|
43
|
+
hashtagged_ids[h.hashtaggable_type] ||= Array.new
|
44
|
+
hashtagged_ids[h.hashtaggable_type] << h.hashtaggable_id
|
45
|
+
end
|
46
|
+
hashtagged_ids
|
47
|
+
end
|
48
|
+
|
49
|
+
def hashtagged_ids_for_type(type)
|
50
|
+
hashtagged_ids_by_types[type]
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
name
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.clean_orphans # From DB
|
58
|
+
# TODO Make this method call a single SQL query
|
59
|
+
orphans = self.all.select { |h| h.hashtaggables.size == 0 }
|
60
|
+
orphans.map(&:destroy)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module SimpleHashtag
|
2
|
+
module Hashtaggable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
has_many :hashtaggings, as: :hashtaggable, class_name: "SimpleHashtag::Hashtagging", dependent: :destroy
|
7
|
+
has_many :hashtags, through: :hashtaggings, class_name: "SimpleHashtag::Hashtag"
|
8
|
+
|
9
|
+
before_save :update_hashtags
|
10
|
+
|
11
|
+
def hashtaggable_content
|
12
|
+
self.class.hashtaggable_attribute # to ensure it has been called at least once
|
13
|
+
content = self.send(self.class.hashtaggable_attribute_name)
|
14
|
+
content.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_hashtags
|
18
|
+
self.hashtags = parsed_hashtags
|
19
|
+
end
|
20
|
+
|
21
|
+
def parsed_hashtags
|
22
|
+
parsed_hashtags = []
|
23
|
+
array_of_hashtags_as_string = scan_for_hashtags(hashtaggable_content)
|
24
|
+
array_of_hashtags_as_string.each do |s|
|
25
|
+
parsed_hashtags << Hashtag.find_or_create_by_name(s[1])
|
26
|
+
end
|
27
|
+
parsed_hashtags
|
28
|
+
end
|
29
|
+
|
30
|
+
def scan_for_hashtags(content)
|
31
|
+
match = content.scan(Hashtag::HASHTAG_REGEX)
|
32
|
+
match.uniq!
|
33
|
+
match
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
attr_accessor :hashtaggable_attribute_name
|
39
|
+
|
40
|
+
def hashtaggable_attribute(name=nil)
|
41
|
+
self.hashtaggable_attribute_name ||= name || :body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_hashtag/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_hashtag_2"
|
8
|
+
spec.version = SimpleHashtag::VERSION
|
9
|
+
spec.authors = ["Raphael Campardou"]
|
10
|
+
spec.email = ["ralovely@gmail.com"]
|
11
|
+
spec.description = %q{Parse, store retreive and format hashtags in your text.}
|
12
|
+
spec.summary = %q{Simple Hashtag is a mix between–well–hashtags as we know them and categories. It will scan your Active Record attribute for a tag and store it in an index.}
|
13
|
+
spec.homepage = "https://github.com/ralovely/simple_hashtag"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = ">= 1.9.3"
|
22
|
+
spec.add_dependency "rails", "> 3.2.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec-rails"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_dependency "protected_attributes"
|
29
|
+
end
|