sb-acts-as-taggable-on 6.5.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +39 -0
- data/Appraisals +15 -0
- data/CHANGELOG.md +330 -0
- data/CONTRIBUTING.md +57 -0
- data/Gemfile +11 -0
- data/Guardfile +5 -0
- data/LICENSE.md +20 -0
- data/README.md +555 -0
- data/Rakefile +21 -0
- data/UPGRADING.md +8 -0
- data/acts-as-taggable-on.gemspec +32 -0
- data/db/migrate/1_acts_as_taggable_on_migration.rb +36 -0
- data/db/migrate/2_add_missing_unique_indices.rb +25 -0
- data/db/migrate/3_add_taggings_counter_cache_to_tags.rb +19 -0
- data/db/migrate/4_add_missing_taggable_index.rb +14 -0
- data/db/migrate/5_change_collation_for_tag_names.rb +14 -0
- data/db/migrate/6_add_missing_indexes_on_taggings.rb +22 -0
- data/gemfiles/activerecord_5.0.gemfile +21 -0
- data/gemfiles/activerecord_5.1.gemfile +21 -0
- data/gemfiles/activerecord_5.2.gemfile +21 -0
- data/gemfiles/activerecord_6.0.gemfile +21 -0
- data/lib/acts-as-taggable-on.rb +133 -0
- data/lib/acts_as_taggable_on.rb +6 -0
- data/lib/acts_as_taggable_on/default_parser.rb +79 -0
- data/lib/acts_as_taggable_on/engine.rb +4 -0
- data/lib/acts_as_taggable_on/generic_parser.rb +19 -0
- data/lib/acts_as_taggable_on/tag.rb +139 -0
- data/lib/acts_as_taggable_on/tag_list.rb +106 -0
- data/lib/acts_as_taggable_on/taggable.rb +101 -0
- data/lib/acts_as_taggable_on/taggable/cache.rb +90 -0
- data/lib/acts_as_taggable_on/taggable/collection.rb +183 -0
- data/lib/acts_as_taggable_on/taggable/core.rb +322 -0
- data/lib/acts_as_taggable_on/taggable/ownership.rb +136 -0
- data/lib/acts_as_taggable_on/taggable/related.rb +71 -0
- data/lib/acts_as_taggable_on/taggable/tag_list_type.rb +4 -0
- data/lib/acts_as_taggable_on/taggable/tagged_with_query.rb +16 -0
- data/lib/acts_as_taggable_on/taggable/tagged_with_query/all_tags_query.rb +111 -0
- data/lib/acts_as_taggable_on/taggable/tagged_with_query/any_tags_query.rb +70 -0
- data/lib/acts_as_taggable_on/taggable/tagged_with_query/exclude_tags_query.rb +82 -0
- data/lib/acts_as_taggable_on/taggable/tagged_with_query/query_base.rb +61 -0
- data/lib/acts_as_taggable_on/tagger.rb +89 -0
- data/lib/acts_as_taggable_on/tagging.rb +36 -0
- data/lib/acts_as_taggable_on/tags_helper.rb +15 -0
- data/lib/acts_as_taggable_on/utils.rb +37 -0
- data/lib/acts_as_taggable_on/version.rb +3 -0
- data/lib/tasks/tags_collate_utf8.rake +21 -0
- data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +285 -0
- data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +112 -0
- data/spec/acts_as_taggable_on/caching_spec.rb +129 -0
- data/spec/acts_as_taggable_on/default_parser_spec.rb +47 -0
- data/spec/acts_as_taggable_on/dirty_spec.rb +142 -0
- data/spec/acts_as_taggable_on/generic_parser_spec.rb +14 -0
- data/spec/acts_as_taggable_on/related_spec.rb +99 -0
- data/spec/acts_as_taggable_on/single_table_inheritance_spec.rb +231 -0
- data/spec/acts_as_taggable_on/tag_list_spec.rb +176 -0
- data/spec/acts_as_taggable_on/tag_spec.rb +340 -0
- data/spec/acts_as_taggable_on/taggable_spec.rb +817 -0
- data/spec/acts_as_taggable_on/tagger_spec.rb +153 -0
- data/spec/acts_as_taggable_on/tagging_spec.rb +117 -0
- data/spec/acts_as_taggable_on/tags_helper_spec.rb +45 -0
- data/spec/acts_as_taggable_on/utils_spec.rb +23 -0
- data/spec/internal/app/models/altered_inheriting_taggable_model.rb +5 -0
- data/spec/internal/app/models/cached_model.rb +3 -0
- data/spec/internal/app/models/cached_model_with_array.rb +11 -0
- data/spec/internal/app/models/columns_override_model.rb +5 -0
- data/spec/internal/app/models/company.rb +15 -0
- data/spec/internal/app/models/inheriting_taggable_model.rb +4 -0
- data/spec/internal/app/models/market.rb +2 -0
- data/spec/internal/app/models/non_standard_id_taggable_model.rb +8 -0
- data/spec/internal/app/models/ordered_taggable_model.rb +4 -0
- data/spec/internal/app/models/other_cached_model.rb +3 -0
- data/spec/internal/app/models/other_taggable_model.rb +4 -0
- data/spec/internal/app/models/student.rb +4 -0
- data/spec/internal/app/models/taggable_model.rb +14 -0
- data/spec/internal/app/models/untaggable_model.rb +3 -0
- data/spec/internal/app/models/user.rb +3 -0
- data/spec/internal/config/database.yml.sample +19 -0
- data/spec/internal/db/schema.rb +110 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/0-helpers.rb +32 -0
- data/spec/support/array.rb +9 -0
- data/spec/support/database.rb +36 -0
- data/spec/support/database_cleaner.rb +21 -0
- metadata +269 -0
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
__Copyright (c) 2007 Michael Bleigh and Intridea Inc.__
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,555 @@
|
|
1
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
2
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
3
|
+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
|
4
|
+
|
5
|
+
- [ActsAsTaggableOn](#actsastaggableon)
|
6
|
+
- [Installation](#installation)
|
7
|
+
- [Post Installation](#post-installation)
|
8
|
+
- [For MySql users](#for-mysql-users)
|
9
|
+
- [Usage](#usage)
|
10
|
+
- [Finding most or least used tags](#finding-most-or-least-used-tags)
|
11
|
+
- [Finding Tagged Objects](#finding-tagged-objects)
|
12
|
+
- [Relationships](#relationships)
|
13
|
+
- [Dynamic Tag Contexts](#dynamic-tag-contexts)
|
14
|
+
- [Tag Parsers](#tag-parsers)
|
15
|
+
- [Tag Ownership](#tag-ownership)
|
16
|
+
- [Working with Owned Tags](#working-with-owned-tags)
|
17
|
+
- [Adding owned tags](#adding-owned-tags)
|
18
|
+
- [Removing owned tags](#removing-owned-tags)
|
19
|
+
- [Dirty objects](#dirty-objects)
|
20
|
+
- [Tag cloud calculations](#tag-cloud-calculations)
|
21
|
+
- [Configuration](#configuration)
|
22
|
+
- [Upgrading](#upgrading)
|
23
|
+
- [Contributors](#contributors)
|
24
|
+
- [Compatibility](#compatibility)
|
25
|
+
- [TODO](#todo)
|
26
|
+
- [Testing](#testing)
|
27
|
+
- [License](#license)
|
28
|
+
|
29
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
30
|
+
|
31
|
+
# ActsAsTaggableOn
|
32
|
+
|
33
|
+
[](https://gitter.im/mbleigh/acts-as-taggable-on?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
34
|
+
[](http://badge.fury.io/rb/acts-as-taggable-on)
|
35
|
+
[](http://travis-ci.org/mbleigh/acts-as-taggable-on)
|
36
|
+
[](https://codeclimate.com/github/mbleigh/acts-as-taggable-on)
|
37
|
+
[](http://inch-ci.org/github/mbleigh/acts-as-taggable-on)
|
38
|
+
[](https://hakiri.io/github/mbleigh/acts-as-taggable-on/master)
|
39
|
+
|
40
|
+
This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney.
|
41
|
+
It has evolved substantially since that point, but all credit goes to him for the
|
42
|
+
initial tagging functionality that so many people have used.
|
43
|
+
|
44
|
+
For instance, in a social network, a user might have tags that are called skills,
|
45
|
+
interests, sports, and more. There is no real way to differentiate between tags and
|
46
|
+
so an implementation of this type is not possible with acts as taggable on steroids.
|
47
|
+
|
48
|
+
Enter Acts as Taggable On. Rather than tying functionality to a specific keyword
|
49
|
+
(namely `tags`), acts as taggable on allows you to specify an arbitrary number of
|
50
|
+
tag "contexts" that can be used locally or in combination in the same way steroids
|
51
|
+
was used.
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
|
57
|
+
To use it, add it to your Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'acts-as-taggable-on', '~> 6.0'
|
61
|
+
```
|
62
|
+
|
63
|
+
and bundle:
|
64
|
+
|
65
|
+
```shell
|
66
|
+
bundle
|
67
|
+
```
|
68
|
+
|
69
|
+
#### Post Installation
|
70
|
+
|
71
|
+
Install migrations
|
72
|
+
|
73
|
+
```shell
|
74
|
+
# For the latest versions :
|
75
|
+
rake acts_as_taggable_on_engine:install:migrations
|
76
|
+
```
|
77
|
+
|
78
|
+
Review the generated migrations then migrate :
|
79
|
+
```shell
|
80
|
+
rake db:migrate
|
81
|
+
```
|
82
|
+
|
83
|
+
#### For MySql users
|
84
|
+
You can circumvent at any time the problem of special characters [issue 623](https://github.com/mbleigh/acts-as-taggable-on/issues/623) by setting in an initializer file:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
ActsAsTaggableOn.force_binary_collation = true
|
88
|
+
```
|
89
|
+
|
90
|
+
Or by running this rake task:
|
91
|
+
|
92
|
+
```shell
|
93
|
+
rake acts_as_taggable_on_engine:tag_names:collate_bin
|
94
|
+
```
|
95
|
+
|
96
|
+
See the Configuration section for more details, and a general note valid for older
|
97
|
+
version of the gem.
|
98
|
+
|
99
|
+
|
100
|
+
## Usage
|
101
|
+
|
102
|
+
Setup
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class User < ActiveRecord::Base
|
106
|
+
acts_as_taggable # Alias for acts_as_taggable_on :tags
|
107
|
+
acts_as_taggable_on :skills, :interests
|
108
|
+
end
|
109
|
+
|
110
|
+
class UsersController < ApplicationController
|
111
|
+
def user_params
|
112
|
+
params.require(:user).permit(:name, :tag_list) ## Rails 4 strong params usage
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
@user = User.new(:name => "Bobby")
|
117
|
+
```
|
118
|
+
|
119
|
+
Add and remove a single tag
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
@user.tag_list.add("awesome") # add a single tag. alias for <<
|
123
|
+
@user.tag_list.remove("awesome") # remove a single tag
|
124
|
+
@user.save # save to persist tag_list
|
125
|
+
```
|
126
|
+
|
127
|
+
Add and remove multiple tags in an array
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
@user.tag_list.add("awesome", "slick")
|
131
|
+
@user.tag_list.remove("awesome", "slick")
|
132
|
+
@user.save
|
133
|
+
```
|
134
|
+
|
135
|
+
You can also add and remove tags in format of String. This would
|
136
|
+
be convenient in some cases such as handling tag input param in a String.
|
137
|
+
|
138
|
+
Pay attention you need to add `parse: true` as option in this case.
|
139
|
+
|
140
|
+
You may also want to take a look at delimiter in the string. The default
|
141
|
+
is comma `,` so you don't need to do anything here. However, if you made
|
142
|
+
a change on delimiter setting, make sure the string will match. See
|
143
|
+
[configuration](#configuration) for more about delimiter.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
@user.tag_list.add("awesome, slick", parse: true)
|
147
|
+
@user.tag_list.remove("awesome, slick", parse: true)
|
148
|
+
```
|
149
|
+
|
150
|
+
You can also add and remove tags by direct assignment. Note this will
|
151
|
+
remove existing tags so use it with attention.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
@user.tag_list = "awesome, slick, hefty"
|
155
|
+
@user.save
|
156
|
+
@user.reload
|
157
|
+
@user.tags
|
158
|
+
=> [#<ActsAsTaggableOn::Tag id: 1, name: "awesome", taggings_count: 1>,
|
159
|
+
#<ActsAsTaggableOn::Tag id: 2, name: "slick", taggings_count: 1>,
|
160
|
+
#<ActsAsTaggableOn::Tag id: 3, name: "hefty", taggings_count: 1>]
|
161
|
+
```
|
162
|
+
|
163
|
+
With the defined context in model, you have multiple new methods at disposal
|
164
|
+
to manage and view the tags in the context. For example, with `:skill` context
|
165
|
+
these methods are added to the model: `skill_list`(and `skill_list.add`, `skill_list.remove`
|
166
|
+
`skill_list=`), `skills`(plural), `skill_counts`.
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
@user.skill_list = "joking, clowning, boxing"
|
170
|
+
@user.save
|
171
|
+
@user.reload
|
172
|
+
@user.skills
|
173
|
+
=> [#<ActsAsTaggableOn::Tag id: 1, name: "joking", taggings_count: 1>,
|
174
|
+
#<ActsAsTaggableOn::Tag id: 2, name: "clowning", taggings_count: 1>,
|
175
|
+
#<ActsAsTaggableOn::Tag id: 3, name: "boxing", taggings_count: 1>]
|
176
|
+
|
177
|
+
@user.skill_list.add("coding")
|
178
|
+
|
179
|
+
@user.skill_list
|
180
|
+
# => ["joking", "clowning", "boxing", "coding"]
|
181
|
+
|
182
|
+
@another_user = User.new(:name => "Alice")
|
183
|
+
@another_user.skill_list.add("clowning")
|
184
|
+
@another_user.save
|
185
|
+
|
186
|
+
User.skill_counts
|
187
|
+
=> [#<ActsAsTaggableOn::Tag id: 1, name: "joking", taggings_count: 1>,
|
188
|
+
#<ActsAsTaggableOn::Tag id: 2, name: "clowning", taggings_count: 2>,
|
189
|
+
#<ActsAsTaggableOn::Tag id: 3, name: "boxing", taggings_count: 1>]
|
190
|
+
```
|
191
|
+
|
192
|
+
To preserve the order in which tags are created use `acts_as_ordered_taggable`:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
class User < ActiveRecord::Base
|
196
|
+
# Alias for acts_as_ordered_taggable_on :tags
|
197
|
+
acts_as_ordered_taggable
|
198
|
+
acts_as_ordered_taggable_on :skills, :interests
|
199
|
+
end
|
200
|
+
|
201
|
+
@user = User.new(:name => "Bobby")
|
202
|
+
@user.tag_list = "east, south"
|
203
|
+
@user.save
|
204
|
+
|
205
|
+
@user.tag_list = "north, east, south, west"
|
206
|
+
@user.save
|
207
|
+
|
208
|
+
@user.reload
|
209
|
+
@user.tag_list # => ["north", "east", "south", "west"]
|
210
|
+
```
|
211
|
+
|
212
|
+
### Finding most or least used tags
|
213
|
+
|
214
|
+
You can find the most or least used tags by using:
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
ActsAsTaggableOn::Tag.most_used
|
218
|
+
ActsAsTaggableOn::Tag.least_used
|
219
|
+
```
|
220
|
+
|
221
|
+
You can also filter the results by passing the method a limit, however the default limit is 20.
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
ActsAsTaggableOn::Tag.most_used(10)
|
225
|
+
ActsAsTaggableOn::Tag.least_used(10)
|
226
|
+
```
|
227
|
+
|
228
|
+
### Finding Tagged Objects
|
229
|
+
|
230
|
+
Acts As Taggable On uses scopes to create an association for tags.
|
231
|
+
This way you can mix and match to filter down your results.
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
class User < ActiveRecord::Base
|
235
|
+
acts_as_taggable_on :tags, :skills
|
236
|
+
scope :by_join_date, order("created_at DESC")
|
237
|
+
end
|
238
|
+
|
239
|
+
User.tagged_with("awesome").by_join_date
|
240
|
+
User.tagged_with("awesome").by_join_date.paginate(:page => params[:page], :per_page => 20)
|
241
|
+
|
242
|
+
# Find users that matches all given tags:
|
243
|
+
# NOTE: This only matches users that have the exact set of specified tags. If a user has additional tags, they are not returned.
|
244
|
+
User.tagged_with(["awesome", "cool"], :match_all => true)
|
245
|
+
|
246
|
+
# Find users with any of the specified tags:
|
247
|
+
User.tagged_with(["awesome", "cool"], :any => true)
|
248
|
+
|
249
|
+
# Find users that have not been tagged with awesome or cool:
|
250
|
+
User.tagged_with(["awesome", "cool"], :exclude => true)
|
251
|
+
|
252
|
+
# Find users with any of the tags based on context:
|
253
|
+
User.tagged_with(['awesome', 'cool'], :on => :tags, :any => true).tagged_with(['smart', 'shy'], :on => :skills, :any => true)
|
254
|
+
```
|
255
|
+
|
256
|
+
You can also use `:wild => true` option along with `:any` or `:exclude` option. It will be looking for `%awesome%` and `%cool%` in SQL.
|
257
|
+
|
258
|
+
__Tip:__ `User.tagged_with([])` or `User.tagged_with('')` will return `[]`, an empty set of records.
|
259
|
+
|
260
|
+
|
261
|
+
### Relationships
|
262
|
+
|
263
|
+
You can find objects of the same type based on similar tags on certain contexts.
|
264
|
+
Also, objects will be returned in descending order based on the total number of
|
265
|
+
matched tags.
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
@bobby = User.find_by_name("Bobby")
|
269
|
+
@bobby.skill_list # => ["jogging", "diving"]
|
270
|
+
|
271
|
+
@frankie = User.find_by_name("Frankie")
|
272
|
+
@frankie.skill_list # => ["hacking"]
|
273
|
+
|
274
|
+
@tom = User.find_by_name("Tom")
|
275
|
+
@tom.skill_list # => ["hacking", "jogging", "diving"]
|
276
|
+
|
277
|
+
@tom.find_related_skills # => [<User name="Bobby">, <User name="Frankie">]
|
278
|
+
@bobby.find_related_skills # => [<User name="Tom">]
|
279
|
+
@frankie.find_related_skills # => [<User name="Tom">]
|
280
|
+
```
|
281
|
+
|
282
|
+
### Dynamic Tag Contexts
|
283
|
+
|
284
|
+
In addition to the generated tag contexts in the definition, it is also possible
|
285
|
+
to allow for dynamic tag contexts (this could be user generated tag contexts!)
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
@user = User.new(:name => "Bobby")
|
289
|
+
@user.set_tag_list_on(:customs, "same, as, tag, list")
|
290
|
+
@user.tag_list_on(:customs) # => ["same", "as", "tag", "list"]
|
291
|
+
@user.save
|
292
|
+
@user.tags_on(:customs) # => [<Tag name='same'>,...]
|
293
|
+
@user.tag_counts_on(:customs)
|
294
|
+
User.tagged_with("same", :on => :customs) # => [@user]
|
295
|
+
```
|
296
|
+
|
297
|
+
### Tag Parsers
|
298
|
+
|
299
|
+
If you want to change how tags are parsed, you can define your own implementation:
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
class MyParser < ActsAsTaggableOn::GenericParser
|
303
|
+
def parse
|
304
|
+
ActsAsTaggableOn::TagList.new.tap do |tag_list|
|
305
|
+
tag_list.add @tag_list.split('|')
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
```
|
310
|
+
|
311
|
+
Now you can use this parser, passing it as parameter:
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
@user = User.new(:name => "Bobby")
|
315
|
+
@user.tag_list = "east, south"
|
316
|
+
@user.tag_list.add("north|west", parser: MyParser)
|
317
|
+
@user.tag_list # => ["north", "east", "south", "west"]
|
318
|
+
|
319
|
+
# Or also:
|
320
|
+
@user.tag_list.parser = MyParser
|
321
|
+
@user.tag_list.add("north|west")
|
322
|
+
@user.tag_list # => ["north", "east", "south", "west"]
|
323
|
+
```
|
324
|
+
|
325
|
+
Or change it globally:
|
326
|
+
|
327
|
+
```ruby
|
328
|
+
ActsAsTaggableOn.default_parser = MyParser
|
329
|
+
@user = User.new(:name => "Bobby")
|
330
|
+
@user.tag_list = "east|south"
|
331
|
+
@user.tag_list # => ["east", "south"]
|
332
|
+
```
|
333
|
+
|
334
|
+
### Tag Ownership
|
335
|
+
|
336
|
+
Tags can have owners:
|
337
|
+
|
338
|
+
```ruby
|
339
|
+
class User < ActiveRecord::Base
|
340
|
+
acts_as_tagger
|
341
|
+
end
|
342
|
+
|
343
|
+
class Photo < ActiveRecord::Base
|
344
|
+
acts_as_taggable_on :locations
|
345
|
+
end
|
346
|
+
|
347
|
+
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
|
348
|
+
@some_user.owned_taggings
|
349
|
+
@some_user.owned_tags
|
350
|
+
Photo.tagged_with("paris", :on => :locations, :owned_by => @some_user)
|
351
|
+
@some_photo.locations_from(@some_user) # => ["paris", "normandy"]
|
352
|
+
@some_photo.owner_tags_on(@some_user, :locations) # => [#<ActsAsTaggableOn::Tag id: 1, name: "paris">...]
|
353
|
+
@some_photo.owner_tags_on(nil, :locations) # => Ownerships equivalent to saying @some_photo.locations
|
354
|
+
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations, :skip_save => true) #won't save @some_photo object
|
355
|
+
```
|
356
|
+
|
357
|
+
#### Working with Owned Tags
|
358
|
+
Note that `tag_list` only returns tags whose taggings do not have an owner. Continuing from the above example:
|
359
|
+
```ruby
|
360
|
+
@some_photo.tag_list # => []
|
361
|
+
```
|
362
|
+
To retrieve all tags of an object (regardless of ownership) or if only one owner can tag the object, use `all_tags_list`.
|
363
|
+
|
364
|
+
##### Adding owned tags
|
365
|
+
Note that **owned tags** are added all at once, in the form of ***comma seperated tags*** in string.
|
366
|
+
Also, when you try to add **owned tags** again, it simply overwrites the previous set of **owned tags**.
|
367
|
+
So to append tags in previously existing **owned tags** list, go as follows:
|
368
|
+
```ruby
|
369
|
+
def add_owned_tag
|
370
|
+
@some_item = Item.find(params[:id])
|
371
|
+
owned_tag_list = @some_item.all_tags_list - @some_item.tag_list
|
372
|
+
owned_tag_list += [(params[:tag])]
|
373
|
+
@tag_owner.tag(@some_item, :with => stringify(owned_tag_list), :on => :tags)
|
374
|
+
@some_item.save
|
375
|
+
end
|
376
|
+
|
377
|
+
def stringify(tag_list)
|
378
|
+
tag_list.inject('') { |memo, tag| memo += (tag + ',') }[0..-1]
|
379
|
+
end
|
380
|
+
```
|
381
|
+
##### Removing owned tags
|
382
|
+
Similarly as above, removing will be as follows:
|
383
|
+
```ruby
|
384
|
+
def remove_owned_tag
|
385
|
+
@some_item = Item.find(params[:id])
|
386
|
+
owned_tag_list = @some_item.all_tags_list - @some_item.tag_list
|
387
|
+
owned_tag_list -= [(params[:tag])]
|
388
|
+
@tag_owner.tag(@some_item, :with => stringify(owned_tag_list), :on => :tags)
|
389
|
+
@some_item.save
|
390
|
+
end
|
391
|
+
```
|
392
|
+
|
393
|
+
### Dirty objects
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
@bobby = User.find_by_name("Bobby")
|
397
|
+
@bobby.skill_list # => ["jogging", "diving"]
|
398
|
+
|
399
|
+
@bobby.skill_list_changed? #=> false
|
400
|
+
@bobby.changes #=> {}
|
401
|
+
|
402
|
+
@bobby.skill_list = "swimming"
|
403
|
+
@bobby.changes.should == {"skill_list"=>["jogging, diving", ["swimming"]]}
|
404
|
+
@bobby.skill_list_changed? #=> true
|
405
|
+
|
406
|
+
@bobby.skill_list_change.should == ["jogging, diving", ["swimming"]]
|
407
|
+
```
|
408
|
+
|
409
|
+
### Tag cloud calculations
|
410
|
+
|
411
|
+
To construct tag clouds, the frequency of each tag needs to be calculated.
|
412
|
+
Because we specified `acts_as_taggable_on` on the `User` class, we can
|
413
|
+
get a calculation of all the tag counts by using `User.tag_counts_on(:customs)`. But what if we wanted a tag count for
|
414
|
+
a single user's posts? To achieve this we call tag_counts on the association:
|
415
|
+
|
416
|
+
```ruby
|
417
|
+
User.find(:first).posts.tag_counts_on(:tags)
|
418
|
+
```
|
419
|
+
|
420
|
+
A helper is included to assist with generating tag clouds.
|
421
|
+
|
422
|
+
Here is an example that generates a tag cloud.
|
423
|
+
|
424
|
+
Helper:
|
425
|
+
|
426
|
+
```ruby
|
427
|
+
module PostsHelper
|
428
|
+
include ActsAsTaggableOn::TagsHelper
|
429
|
+
end
|
430
|
+
```
|
431
|
+
|
432
|
+
Controller:
|
433
|
+
|
434
|
+
```ruby
|
435
|
+
class PostController < ApplicationController
|
436
|
+
def tag_cloud
|
437
|
+
@tags = Post.tag_counts_on(:tags)
|
438
|
+
end
|
439
|
+
end
|
440
|
+
```
|
441
|
+
|
442
|
+
View:
|
443
|
+
|
444
|
+
```erb
|
445
|
+
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
|
446
|
+
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
|
447
|
+
<% end %>
|
448
|
+
```
|
449
|
+
|
450
|
+
CSS:
|
451
|
+
|
452
|
+
```css
|
453
|
+
.css1 { font-size: 1.0em; }
|
454
|
+
.css2 { font-size: 1.2em; }
|
455
|
+
.css3 { font-size: 1.4em; }
|
456
|
+
.css4 { font-size: 1.6em; }
|
457
|
+
```
|
458
|
+
|
459
|
+
## Configuration
|
460
|
+
|
461
|
+
If you would like to remove unused tag objects after removing taggings, add:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
ActsAsTaggableOn.remove_unused_tags = true
|
465
|
+
```
|
466
|
+
|
467
|
+
If you want force tags to be saved downcased:
|
468
|
+
|
469
|
+
```ruby
|
470
|
+
ActsAsTaggableOn.force_lowercase = true
|
471
|
+
```
|
472
|
+
|
473
|
+
If you want tags to be saved parametrized (you can redefine to_param as well):
|
474
|
+
|
475
|
+
```ruby
|
476
|
+
ActsAsTaggableOn.force_parameterize = true
|
477
|
+
```
|
478
|
+
|
479
|
+
If you would like tags to be case-sensitive and not use LIKE queries for creation:
|
480
|
+
|
481
|
+
```ruby
|
482
|
+
ActsAsTaggableOn.strict_case_match = true
|
483
|
+
```
|
484
|
+
|
485
|
+
If you would like to have an exact match covering special characters with MySql:
|
486
|
+
|
487
|
+
```ruby
|
488
|
+
ActsAsTaggableOn.force_binary_collation = true
|
489
|
+
```
|
490
|
+
|
491
|
+
If you would like to specify table names:
|
492
|
+
|
493
|
+
```ruby
|
494
|
+
ActsAsTaggableOn.tags_table = 'aato_tags'
|
495
|
+
ActsAsTaggableOn.taggings_table = 'aato_taggings'
|
496
|
+
```
|
497
|
+
|
498
|
+
If you want to change the default delimiter (it defaults to ','). You can also pass in an array of delimiters such as ([',', '|']):
|
499
|
+
|
500
|
+
```ruby
|
501
|
+
ActsAsTaggableOn.delimiter = ','
|
502
|
+
```
|
503
|
+
|
504
|
+
*NOTE 1: SQLite by default can't upcase or downcase multibyte characters, resulting in unwanted behavior. Load the SQLite ICU extension for proper handle of such characters. [See docs](http://www.sqlite.org/src/artifact?ci=trunk&filename=ext/icu/README.txt)*
|
505
|
+
|
506
|
+
*NOTE 2: the option `force_binary_collation` is strongest than `strict_case_match` and when
|
507
|
+
set to true, the `strict_case_match` is ignored.
|
508
|
+
To roughly apply the `force_binary_collation` behaviour with a version of the gem <= 3.4.4, execute the following commands in the MySql console:*
|
509
|
+
|
510
|
+
```shell
|
511
|
+
USE my_wonderful_app_db;
|
512
|
+
ALTER TABLE tags MODIFY name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin;
|
513
|
+
```
|
514
|
+
|
515
|
+
#### Upgrading
|
516
|
+
|
517
|
+
see [UPGRADING](UPGRADING.md)
|
518
|
+
|
519
|
+
## Contributors
|
520
|
+
|
521
|
+
We have a long list of valued contributors. [Check them all](https://github.com/mbleigh/acts-as-taggable-on/contributors)
|
522
|
+
|
523
|
+
## Compatibility
|
524
|
+
|
525
|
+
Versions 2.x are compatible with Ruby 1.8.7+ and Rails 3.
|
526
|
+
|
527
|
+
Versions 2.4.1 and up are compatible with Rails 4 too (thanks to arabonradar and cwoodcox).
|
528
|
+
|
529
|
+
Versions >= 3.x are compatible with Ruby 1.9.3+ and Rails 3 and 4.
|
530
|
+
|
531
|
+
Versions >= 4.x are compatible with Ruby 2.0.0+ and Rails 4 and 5.
|
532
|
+
|
533
|
+
For an up-to-date roadmap, see https://github.com/mbleigh/acts-as-taggable-on/milestones
|
534
|
+
|
535
|
+
## TODO
|
536
|
+
|
537
|
+
- Write benchmark script
|
538
|
+
- Resolve concurrency issues
|
539
|
+
|
540
|
+
## Testing
|
541
|
+
|
542
|
+
Acts As Taggable On uses RSpec for its test coverage. Inside the gem
|
543
|
+
directory, you can run the specs with:
|
544
|
+
|
545
|
+
```shell
|
546
|
+
bundle
|
547
|
+
rake spec
|
548
|
+
```
|
549
|
+
|
550
|
+
You can run all the tests across all the Rails versions by running `rake appraise`. If you'd also like to [run the tests across all rubies and databases as configured for Travis CI, install and run `wwtd`](https://github.com/grosser/wwtd).
|
551
|
+
|
552
|
+
|
553
|
+
## License
|
554
|
+
|
555
|
+
See [LICENSE](https://github.com/mbleigh/acts-as-taggable-on/blob/master/LICENSE.md)
|