acts_as_taggable_on_steroids 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +212 -0
- data/MIT-LICENSE +20 -0
- data/README +151 -0
- data/Rakefile +22 -0
- data/acts_as_taggable_on_steroids.gemspec +54 -0
- data/generators/acts_as_taggable_migration/acts_as_taggable_migration_generator.rb +11 -0
- data/generators/acts_as_taggable_migration/templates/migration.rb +26 -0
- data/init.rb +1 -0
- data/lib/acts_as_taggable.rb +230 -0
- data/lib/tag.rb +71 -0
- data/lib/tag_list.rb +108 -0
- data/lib/tagging.rb +12 -0
- data/lib/tags_helper.rb +13 -0
- data/test/abstract_unit.rb +102 -0
- data/test/acts_as_taggable_test.rb +384 -0
- data/test/database.yml +10 -0
- data/test/fixtures/magazine.rb +3 -0
- data/test/fixtures/magazines.yml +5 -0
- data/test/fixtures/photo.rb +8 -0
- data/test/fixtures/photos.yml +19 -0
- data/test/fixtures/post.rb +7 -0
- data/test/fixtures/posts.yml +27 -0
- data/test/fixtures/special_post.rb +2 -0
- data/test/fixtures/subscription.rb +4 -0
- data/test/fixtures/subscriptions.yml +3 -0
- data/test/fixtures/taggings.yml +107 -0
- data/test/fixtures/tags.yml +14 -0
- data/test/fixtures/user.rb +7 -0
- data/test/fixtures/users.yml +5 -0
- data/test/schema.rb +37 -0
- data/test/tag_list_test.rb +119 -0
- data/test/tag_test.rb +62 -0
- data/test/tagging_test.rb +11 -0
- data/test/tags_helper_test.rb +25 -0
- metadata +88 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
[11 Jun 09]
|
2
|
+
|
3
|
+
* Remove deprecated TagCountsExtension.
|
4
|
+
|
5
|
+
* Update tests to use foxy fixtures [Jonas Wagner]
|
6
|
+
|
7
|
+
* Allow hash conditions to be passed to Tag.counts [Jonas Wagner]
|
8
|
+
|
9
|
+
[3 Jun 09]
|
10
|
+
|
11
|
+
* Upgrade tests for Rails 2.3
|
12
|
+
|
13
|
+
[18 Mar 09]
|
14
|
+
|
15
|
+
* Change callbacks used to save tags.
|
16
|
+
|
17
|
+
[18 Feb 09]
|
18
|
+
|
19
|
+
* Greatly improve speed when using find_tagged_with and :match_all [notonthehighstreet.com].
|
20
|
+
|
21
|
+
[17 Sep 08]
|
22
|
+
|
23
|
+
* Sanitize scope conditions in find_options_for_tag_counts [Rémy-Christophe Schermesser]
|
24
|
+
|
25
|
+
[23 Aug 08]
|
26
|
+
|
27
|
+
* Fix tag_counts instance method when no tags are present.
|
28
|
+
|
29
|
+
* Make tag_counts instance_method merge any :conditions passed to it.
|
30
|
+
|
31
|
+
[30 Mar 08]
|
32
|
+
|
33
|
+
* Make TagList.from accept array arguments.
|
34
|
+
|
35
|
+
[29 Mar 08]
|
36
|
+
|
37
|
+
* Improve parsing of quotes inside tags [Arturas Slajus].
|
38
|
+
|
39
|
+
* Add Tag.counts method.
|
40
|
+
|
41
|
+
[28 Mar 08]
|
42
|
+
|
43
|
+
* Make Tag#taggings :dependent => :destroy.
|
44
|
+
|
45
|
+
[27 Mar 08]
|
46
|
+
|
47
|
+
* Fix documentation for tag_counts.
|
48
|
+
|
49
|
+
[18 Mar 08]
|
50
|
+
|
51
|
+
* Add TagList#toggle [Pete Yandell].
|
52
|
+
|
53
|
+
# Add find_related_tags method [Austin Foncaier].
|
54
|
+
|
55
|
+
[30 Jan 08]
|
56
|
+
|
57
|
+
* Fix Tag.destroy_unused on Rails 2.0.
|
58
|
+
|
59
|
+
[23 October 2007]
|
60
|
+
|
61
|
+
* Make find_options_for_tag_counts and find_options_for_tagged_with dup their options.
|
62
|
+
|
63
|
+
* Apply conditions properly in find_options_for_tag_counts.
|
64
|
+
|
65
|
+
* Fix tag_cloud when no tags are present.
|
66
|
+
|
67
|
+
[22 October 2007]
|
68
|
+
|
69
|
+
* Fix find_tagged_with using :match_all and :include.
|
70
|
+
|
71
|
+
* Use inner joins instead of left outer joins.
|
72
|
+
|
73
|
+
[15 October 2007]
|
74
|
+
|
75
|
+
* Make find_tagged_with correctly apply :conditions
|
76
|
+
|
77
|
+
* Add Tag.destroy_unused option.
|
78
|
+
|
79
|
+
[11 October 2007]
|
80
|
+
|
81
|
+
* Make tag_counts work correctly with STI.
|
82
|
+
|
83
|
+
[3 October 2007]
|
84
|
+
|
85
|
+
* Improve documentation.
|
86
|
+
|
87
|
+
* Fix TagsHelper and test.
|
88
|
+
|
89
|
+
[2 October 2007]
|
90
|
+
|
91
|
+
* Remove TagList.parse, use TagList.from instead.
|
92
|
+
|
93
|
+
* Add :parse option to TagList#new, TagList#add, and TagList#remove.
|
94
|
+
|
95
|
+
tag_list = TagList.new("One, Two", :parse => true) # ["One", "Two"]
|
96
|
+
|
97
|
+
tag_list # ["One", "Two"]
|
98
|
+
tag_list.add("Three, Four", :parse => true) # ["One", "Two", "Three", "Four"]
|
99
|
+
|
100
|
+
* Remove TagList#names.
|
101
|
+
|
102
|
+
[29 September 2007]
|
103
|
+
|
104
|
+
* Add TagsHelper to assist with generating tag clouds and provide a simple example.
|
105
|
+
|
106
|
+
[27 September 2007]
|
107
|
+
|
108
|
+
* Add #tag_counts method to get tag counts for a specific object's tags.
|
109
|
+
|
110
|
+
* BACKWARDS INCOMPATIBILITY: Rename #find_options_for_tagged_with to #find_options_for_find_tagged_with
|
111
|
+
|
112
|
+
[17 September 2007]
|
113
|
+
|
114
|
+
* Fix clearing of cached tag list when all tags removed.
|
115
|
+
|
116
|
+
[12 September 2007]
|
117
|
+
|
118
|
+
* Make the TagList class inherit from Array.
|
119
|
+
|
120
|
+
* Deprecate obsolete TagList#names.
|
121
|
+
|
122
|
+
[6 September 2007]
|
123
|
+
|
124
|
+
* Add TagList#include? and TagList#empty?
|
125
|
+
|
126
|
+
[26 August 2007]
|
127
|
+
|
128
|
+
* Remove deprecated Tag.delimiter. Use TagList.delimiter instead.
|
129
|
+
|
130
|
+
[25 August 2007]
|
131
|
+
|
132
|
+
* Make tag_counts work with has_many :through
|
133
|
+
|
134
|
+
[23 August 2007]
|
135
|
+
|
136
|
+
* Make search comparisons case-insensitive across different databases. [Moisés Machado]
|
137
|
+
|
138
|
+
* Improve compatiblity with STI. [Moisés Machado]
|
139
|
+
|
140
|
+
[25 July 2007]
|
141
|
+
|
142
|
+
* Respect custom table names for the Tag and Tagging classes.
|
143
|
+
|
144
|
+
* Fix the :exclude option for find_tagged_with
|
145
|
+
|
146
|
+
[17 July 2007]
|
147
|
+
|
148
|
+
* Make the migration work on edge rails
|
149
|
+
|
150
|
+
[8 July 2007]
|
151
|
+
|
152
|
+
* find_options_for_tagged_with should not alter its arguments
|
153
|
+
|
154
|
+
[1 July 2007]
|
155
|
+
|
156
|
+
* Fix incorrect tagging when the case of the tag list is changed.
|
157
|
+
|
158
|
+
* Fix deprecated Tag.delimiter accessor.
|
159
|
+
|
160
|
+
[23 June 2007]
|
161
|
+
|
162
|
+
* Add validation to Tag model.
|
163
|
+
|
164
|
+
* find_options_for_tagged_with should always return a hash.
|
165
|
+
|
166
|
+
* find_tagged_with passing in no tags should return an empty array.
|
167
|
+
|
168
|
+
* Improve compatibility with PostgreSQL.
|
169
|
+
|
170
|
+
[21 June 2007]
|
171
|
+
|
172
|
+
* Remove extra .rb from generated migration file name.
|
173
|
+
|
174
|
+
[15 June 2007]
|
175
|
+
|
176
|
+
* Introduce TagList class.
|
177
|
+
|
178
|
+
* Various cleanups and improvements.
|
179
|
+
|
180
|
+
* Use TagList.delimiter now, not Tag.delimiter. Tag.delimiter will be removed at some stage.
|
181
|
+
|
182
|
+
[11 June 2007]
|
183
|
+
|
184
|
+
* Restructure the creation of the options for find_tagged_with [Thijs Cadier]
|
185
|
+
|
186
|
+
* Add an example migration with a generator.
|
187
|
+
|
188
|
+
* Add caching.
|
189
|
+
|
190
|
+
* Fix compatibility with Ruby < 1.8.6
|
191
|
+
|
192
|
+
[23 April 2007]
|
193
|
+
|
194
|
+
* Make tag_list to respect Tag.delimiter
|
195
|
+
|
196
|
+
[31 March 2007]
|
197
|
+
|
198
|
+
* Add Tag.delimiter accessor to change how tags are parsed.
|
199
|
+
|
200
|
+
* Fix :include => :tags when used with find_tagged_with
|
201
|
+
|
202
|
+
[7 March 2007]
|
203
|
+
|
204
|
+
* Fix tag_counts for SQLServer [Brad Young]
|
205
|
+
|
206
|
+
[21 Feb 2007]
|
207
|
+
|
208
|
+
* Use scoping instead of TagCountsExtension [Michael Schuerig]
|
209
|
+
|
210
|
+
[7 Jan 2007]
|
211
|
+
|
212
|
+
* Add :match_all to find_tagged_with [Michael Sheakoski]
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Jonathan Viney
|
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
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
= acts_as_taggable_on_steroids
|
2
|
+
|
3
|
+
If you find this plugin useful, please consider a donation to show your support!
|
4
|
+
|
5
|
+
http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
|
6
|
+
|
7
|
+
Email address: jonathan.viney@gmail.com
|
8
|
+
|
9
|
+
== Instructions
|
10
|
+
|
11
|
+
This plugin is based on acts_as_taggable by DHH but includes extras
|
12
|
+
such as tests, smarter tag assignment, and tag cloud calculations.
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
ruby script/plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git
|
17
|
+
|
18
|
+
== Usage
|
19
|
+
|
20
|
+
=== Prepare database
|
21
|
+
|
22
|
+
Generate and apply the migration:
|
23
|
+
|
24
|
+
ruby script/generate acts_as_taggable_migration
|
25
|
+
rake db:migrate
|
26
|
+
|
27
|
+
=== Basic tagging
|
28
|
+
|
29
|
+
Let's suppose users have many posts and we want those posts to have tags.
|
30
|
+
The first step is to add +acts_as_taggable+ to the Post class:
|
31
|
+
|
32
|
+
class Post < ActiveRecord::Base
|
33
|
+
acts_as_taggable
|
34
|
+
|
35
|
+
belongs_to :user
|
36
|
+
end
|
37
|
+
|
38
|
+
We can now use the tagging methods provided by acts_as_taggable, <tt>#tag_list</tt> and <tt>#tag_list=</tt>. Both these
|
39
|
+
methods work like regular attribute accessors.
|
40
|
+
|
41
|
+
p = Post.find(:first)
|
42
|
+
p.tag_list # []
|
43
|
+
p.tag_list = "Funny, Silly"
|
44
|
+
p.save
|
45
|
+
p.tag_list # ["Funny", "Silly"]
|
46
|
+
|
47
|
+
You can also add or remove arrays of tags.
|
48
|
+
|
49
|
+
p.tag_list.add("Great", "Awful")
|
50
|
+
p.tag_list.remove("Funny")
|
51
|
+
|
52
|
+
In your views you should use something like the following:
|
53
|
+
|
54
|
+
<%= f.label :tag_list %>
|
55
|
+
<%= f.text_field :tag_list, :size => 80 %>
|
56
|
+
|
57
|
+
=== Finding tagged objects
|
58
|
+
|
59
|
+
To retrieve objects tagged with a certain tag, use find_tagged_with.
|
60
|
+
|
61
|
+
Post.find_tagged_with('Funny, Silly')
|
62
|
+
|
63
|
+
By default, find_tagged_with will find objects that have any of the given tags. To
|
64
|
+
find only objects that are tagged with all the given tags, use match_all.
|
65
|
+
|
66
|
+
Post.find_tagged_with('Funny, Silly', :match_all => true)
|
67
|
+
|
68
|
+
See <tt>ActiveRecord::Acts::Taggable::InstanceMethods</tt> for more methods and options.
|
69
|
+
|
70
|
+
=== Tag cloud calculations
|
71
|
+
|
72
|
+
To construct tag clouds, the frequency of each tag needs to be calculated.
|
73
|
+
Because we specified +acts_as_taggable+ on the <tt>Post</tt> class, we can
|
74
|
+
get a calculation of all the tag counts by using <tt>Post.tag_counts</tt>. But what if we wanted a tag count for
|
75
|
+
an single user's posts? To achieve this we call tag_counts on the association:
|
76
|
+
|
77
|
+
User.find(:first).posts.tag_counts
|
78
|
+
|
79
|
+
A helper is included to assist with generating tag clouds. Include it in your helper file:
|
80
|
+
|
81
|
+
module ApplicationHelper
|
82
|
+
include TagsHelper
|
83
|
+
end
|
84
|
+
|
85
|
+
You can also use the <tt>counts</tt> method on <tt>Tag</tt> to get the counts for all tags in the database.
|
86
|
+
|
87
|
+
Tag.counts
|
88
|
+
|
89
|
+
Here is an example that generates a tag cloud.
|
90
|
+
|
91
|
+
Controller:
|
92
|
+
|
93
|
+
class PostController < ApplicationController
|
94
|
+
def tag_cloud
|
95
|
+
@tags = Post.tag_counts
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
View:
|
100
|
+
<% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
|
101
|
+
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
|
102
|
+
<% end %>
|
103
|
+
|
104
|
+
CSS:
|
105
|
+
|
106
|
+
.css1 { font-size: 1.0em; }
|
107
|
+
.css2 { font-size: 1.2em; }
|
108
|
+
.css3 { font-size: 1.4em; }
|
109
|
+
.css4 { font-size: 1.6em; }
|
110
|
+
|
111
|
+
=== Caching
|
112
|
+
|
113
|
+
It is useful to cache the list of tags to reduce the number of queries executed. To do this,
|
114
|
+
add a column named <tt>cached_tag_list</tt> to the model which is being tagged. The column should be long enough to hold
|
115
|
+
the full tag list and must have a default value of null, not an empty string.
|
116
|
+
|
117
|
+
class CachePostTagList < ActiveRecord::Migration
|
118
|
+
def self.up
|
119
|
+
add_column :posts, :cached_tag_list, :string
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class Post < ActiveRecord::Base
|
124
|
+
acts_as_taggable
|
125
|
+
|
126
|
+
# The caching column defaults to cached_tag_list, but can be changed:
|
127
|
+
#
|
128
|
+
# set_cached_tag_list_column_name "my_caching_column_name"
|
129
|
+
end
|
130
|
+
|
131
|
+
The details of the caching are handled for you. Just continue to use the tag_list accessor as you normally would.
|
132
|
+
Note that the cached tag list will not be updated if you directly create Tagging objects or manually append to the
|
133
|
+
<tt>tags</tt> or <tt>taggings</tt> associations. To update the cached tag list you should call <tt>save_cached_tag_list</tt> manually.
|
134
|
+
|
135
|
+
=== Delimiter
|
136
|
+
|
137
|
+
If you want to change the delimiter used to parse and present tags, set TagList.delimiter.
|
138
|
+
For example, to use spaces instead of commas, add the following to config/environment.rb:
|
139
|
+
|
140
|
+
TagList.delimiter = " "
|
141
|
+
|
142
|
+
=== Unused tags
|
143
|
+
|
144
|
+
Set Tag.destroy_unused to remove tags when they are no longer being
|
145
|
+
used to tag any objects. Defaults to false.
|
146
|
+
|
147
|
+
Tag.destroy_unused = true
|
148
|
+
|
149
|
+
=== Other
|
150
|
+
|
151
|
+
Problems, comments, and suggestions all welcome. jonathan.viney@gmail.com
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the acts_as_taggable_on_steroids plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'test'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the acts_as_taggable_on_steroids plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Acts As Taggable On Steroids'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "acts_as_taggable_on_steroids"
|
3
|
+
s.version = "1.1"
|
4
|
+
s.date = "2009-06-11"
|
5
|
+
s.summary = "Rails plugin that is based on acts_as_taggable by DHH but includes extras such as tests, smarter tag assignment, and tag cloud calculations."
|
6
|
+
s.email = "jonathan.viney@gmail.com"
|
7
|
+
s.homepage = "http://github.com/jviney/acts_as_taggable_on_steroids"
|
8
|
+
s.description = "Rails plugin that is based on acts_as_taggable by DHH but includes extras such as tests, smarter tag assignment, and tag cloud calculations."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
|
11
|
+
s.rubyforge_project = "acts_as_taggable_on_steroids"
|
12
|
+
s.authors = "Jonathan Viney"
|
13
|
+
s.files = [
|
14
|
+
"acts_as_taggable_on_steroids.gemspec",
|
15
|
+
"CHANGELOG",
|
16
|
+
"generators/acts_as_taggable_migration",
|
17
|
+
"generators/acts_as_taggable_migration/acts_as_taggable_migration_generator.rb",
|
18
|
+
"generators/acts_as_taggable_migration/templates",
|
19
|
+
"generators/acts_as_taggable_migration/templates/migration.rb",
|
20
|
+
"init.rb",
|
21
|
+
"lib/acts_as_taggable.rb",
|
22
|
+
"lib/tag.rb",
|
23
|
+
"lib/tag_list.rb",
|
24
|
+
"lib/tagging.rb",
|
25
|
+
"lib/tags_helper.rb",
|
26
|
+
"MIT-LICENSE",
|
27
|
+
"Rakefile",
|
28
|
+
"README",
|
29
|
+
]
|
30
|
+
s.test_files = [
|
31
|
+
"test/abstract_unit.rb",
|
32
|
+
"test/acts_as_taggable_test.rb",
|
33
|
+
"test/database.yml",
|
34
|
+
"test/fixtures",
|
35
|
+
"test/fixtures/magazine.rb",
|
36
|
+
"test/fixtures/magazines.yml",
|
37
|
+
"test/fixtures/photo.rb",
|
38
|
+
"test/fixtures/photos.yml",
|
39
|
+
"test/fixtures/post.rb",
|
40
|
+
"test/fixtures/posts.yml",
|
41
|
+
"test/fixtures/special_post.rb",
|
42
|
+
"test/fixtures/subscription.rb",
|
43
|
+
"test/fixtures/subscriptions.yml",
|
44
|
+
"test/fixtures/taggings.yml",
|
45
|
+
"test/fixtures/tags.yml",
|
46
|
+
"test/fixtures/user.rb",
|
47
|
+
"test/fixtures/users.yml",
|
48
|
+
"test/schema.rb",
|
49
|
+
"test/tag_list_test.rb",
|
50
|
+
"test/tag_test.rb",
|
51
|
+
"test/tagging_test.rb",
|
52
|
+
"test/tags_helper_test.rb"
|
53
|
+
]
|
54
|
+
end
|