forked from FormulaMonks/is_taggable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
243 lines (176 loc) · 8.74 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
is_taggable
===============================================================================
This plugin is almost entirely based on acts-as-taggable-on by Michael Bleigh +
contributors.
is_taggable supports the awesome contextual tagging of acts-as-taggable-on, but
with a couple substantial changes to the underlying architecture.
Architecture Differences
===============================================================================
We had a couple key issues with the underlying architecture in
acts-as-taggable-on which we felt deserved a forking into a new direction.
There are two main architectural differences between is_taggable and
acts-as-taggable-on:
1) is_taggable does *not* use a normalized data model -- there is one table
"taggings" and not two tables "taggings" and "tags". This means that tags are
duplicated in the taggings table. The reason behind this was that we could not
find a reason justifying the join that couldn't be easily overcome with unique
indexing and grouped selects.
2) (This is the big one) -- is_taggable skips validations and callbacks on the
Tagging model when saving. This totally breaks the normal AR behavior and the
behavior used by all other AR tagging plugins. The reason for this is that the
taggings are updated with a multi-insert -- which also means this plugin can
only be used on databases which support multi-insert (MySQL, Postgres, Oracle,
...). The reason we use a multi-insert is because we ran into massive problems
with large numbers of writes on taggings. Take this scenario:
I upload a photo and fill in my tags when I upload it. I want people to find it
so I tag it with about 20 different keywords. In order to save the tags on my
photo the original plugin would have to (at least):
2) do 20 SELECTs on the taggings table for validates_uniqueness_of
4) do some number (at most 20) of INSERTs on the tags table to save the tags
3) do 20 INSERTs on the taggings table to save the taggings
So best case 20 INSERTs, 20 SELECTs -- worst case 40 INSERTs, 20 SELECTs.
Now get a few users adding lots of tags on things concurrently and you can see
writes quickly becoming a problem, and as the number of tags I'm adding grows,
the problem gets worse. Individual INSERTs are fast, but once you have
concurrency you have lock waits and the the problem gets massively compounded.
By using a multi-insert, a non-normalized table, and a "manual" validation (do
all the validates_uniqueness_of checks at once) we can get it down to:
2) do 1 SELECT to check for duplicated taggings
3) do 1 multi-INSERT to INSERT all the taggings
Furthermore, no matter how many tags your inserting, it's always 1 SELECT and 1
INSERT.
Another thing to consider is the impact of updating tags. In the original
plugin this was done as a loop causing multiple DELETEs followed by multiple
INSERTs. We've taken this down to 1 DELETE followed by 1 multi-INSERT.
Compatibility
===============================================================================
is_taggable requires that your underlying database support multi-INSERT
statements (i.e. INSERT INTO taggings (tag) VALUES ('foo', 'bar', 'baz')). Most
"major" databases do -- including MySQL, PostgreSQL and Oracle.
It has only been tested with Rails 2.1+ and makes use of named_scope (introduced
in Rails 2.1).
Installation
===============================================================================
GemPlugin
-------------------------------------------------------------------------------
Rails 2.1+ introduces gem dependencies, to use them add this line to
environment.rb:
config.gem "citrusbyte-is_taggable", :source => "http://gems.github.com", :lib => "is_taggable"
Then run "rake gems:install" to install the gem.
Plugin
-------------------------------------------------------------------------------
script/plugin install git://github.com/citrusbyte/is_taggable.git
Gem
-------------------------------------------------------------------------------
gem install citrusbyte-is_taggable --source http://gems.github.com
Post Installation
-------------------------------------------------------------------------------
1. script/generate is_taggable_migration
2. rake db/migrate
Testing
===============================================================================
is_taggable uses RSpec for its test coverage, if you're using RSpec type:
rake spec:plugins
Examples (all stolen from acts-as-taggable-on docs)
===============================================================================
class User < ActiveRecord::Base
is_taggable :tags, :skills, :interests
end
@user = User.new(:name => "Bobby")
@user.tag_list = "awesome, slick, hefty" # this should be familiar
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
@user.skill_list # => ["joking","clowning","boxing"] as TagList
@user.save
@user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
# The old way
User.find_tagged_with("awesome", :on => :tags) # => [@user]
User.find_tagged_with("awesome", :on => :skills) # => []
# The better way (utilizes named_scope)
User.tagged_with("awesome", :on => :tags) # => [@user]
User.tagged_with("awesome", :on => :skills) # => []
@frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
@frankie.skill_counts
Finding Tagged Objects
======================
is_taggable utilizes Rails 2.1's named_scope to create an association
for tags. This way you can mix and match to filter down your results, and it
also improves compatibility with the will_paginate gem:
class User < ActiveRecord::Base
is_taggable :tags
named_scope :by_join_date, :order => "created_at DESC"
end
User.tagged_with("awesome").by_date
User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)
Relationships
=============
You can find objects of the same type based on similar tags on certain contexts.
Also, objects will be returned in descending order based on the total number of
matched tags.
@bobby = User.find_by_name("Bobby")
@bobby.skill_list # => ["jogging", "diving"]
@frankie = User.find_by_name("Frankie")
@frankie.skill_list # => ["hacking"]
@tom = User.find_by_name("Tom")
@tom.skill_list # => ["hacking", "jogging", "diving"]
@tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
@bobby.find_related_skills # => [<User name="Tom">]
@frankie.find_related_skills # => [<User name="Tom">]
Dynamic Tag Contexts
====================
In addition to the generated tag contexts in the definition, it is also possible
to allow for dynamic tag contexts (this could be user generated tag contexts!)
@user = User.new(:name => "Bobby")
@user.set_tag_list_on(:customs, "same, as, tag, list")
@user.tag_list_on(:customs) # => ["same","as","tag","list"]
@user.save
@user.tags_on(:customs) # => [<Tag name='same'>,...]
@user.tag_counts_on(:customs)
User.find_tagged_with("same", :on => :customs) # => [@user]
Tag Ownership
=============
Tags can have owners:
class User < ActiveRecord::Base
is_tagger
end
class Photo < ActiveRecord::Base
is_taggable :locations
end
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
@some_user.owned_taggings
@some_user.owned_tags
@some_photo.locations_from(@some_user)
Caveats
===============================================================================
1) Your underlying database *must* support multi-INSERT
2) You probably need Rails 2.1+, but seriously named_scope is so cool you need
it anyways.
3) You cannot use callbacks/validations on the Tagging model (you can still use
them like normal on your Taggables and Taggers...)
Contributors
===============================================================================
is_taggable:
* Ben Alavi & Michel Martens - Ruthless hackers of acts-as-taggable-on
acts-as-taggable-on:
* Michael Bleigh - Original Author
* Brendan Lim - Related Objects
* Pradeep Elankumaran - Taggers
* Sinclair Bain - Patch King
Patch Contributors
-------------------------------------------------------------------------------
acts-as-taggable-on:
* tristanzdunn - Related objects of other classes
* azabaj - Fixed migrate down
* Peter Cooper - named_scope fix
* slainer68 - STI fix
* harrylove - migration instructions and fix-ups
* lawrencepit - cached tag work
Resources
===============================================================================
* GitHub - http://github.com/citrusbyte/is_taggable
* Lighthouse - http://citrusbyte.lighthouseapp.com/projects/
is_taggable:
Copyright (c) 2008 Citrusbyte, LLC, released under the MIT license
acts-as-taggable-on:
Copyright (c) 2007 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license