acts-as-taggable-on 2.3.3 → 2.4.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fc369b068cad606293b057e8743504a9e0f4964
4
+ data.tar.gz: 28589a0ecd010e0e1a4cc642086e8940fff307c6
5
+ SHA512:
6
+ metadata.gz: d12a89bc1bae6f87f1b1bb7327b76592fb18b3b5f06951e727cfd7396a501836fab305c2eff30498397fa1f0dc3cb7107e7ce7b39f88faa5b57138c4e6053781
7
+ data.tar.gz: 91f00cc7dc2f93a5918fc7a2b37e40974569ae8becc86fd1971395723778a6b73c2d5905b3212a412dc9d6d2623cb01e63248d66837e2b007e416ab0cb121e16
data/.gitignore CHANGED
@@ -2,10 +2,10 @@
2
2
  *.sqlite3
3
3
  /pkg/*
4
4
  .bundle
5
- .rvmrc
5
+ .ruby-version
6
6
  Gemfile.lock
7
7
  spec/database.yml
8
8
  tmp*.sw?
9
9
  *.sw?
10
10
  tmp
11
- *.gem
11
+ *.gem
@@ -1,4 +1,4 @@
1
- == 2011-08-21
1
+ ## 2011-08-21
2
2
  * escape _ and % for mysql and postgres (@tilsammans)
3
3
  * Now depends on mysql2 gem
4
4
  * tagged_with :any is chainable now (@jeffreyiacono)
@@ -8,28 +8,28 @@
8
8
  * remove warning for rails 3.1 about class_inheritable_attribute
9
9
  * use ActiveRecord migration_number to avoid clashs (@atd)
10
10
 
11
- == 2010-02-17
11
+ ## 2010-02-17
12
12
  * Converted the plugin to be compatible with Rails3
13
13
 
14
- == 2009-12-02
14
+ ## 2009-12-02
15
15
 
16
16
  * PostgreSQL is now supported (via morgoth)
17
17
 
18
- == 2008-07-17
18
+ ## 2008-07-17
19
19
 
20
20
  * Can now use a named_scope to find tags!
21
21
 
22
- == 2008-06-23
22
+ ## 2008-06-23
23
23
 
24
24
  * Can now find related objects of another class (tristanzdunn)
25
25
  * Removed extraneous down migration cruft (azabaj)
26
26
 
27
- == 2008-06-09
27
+ ## 2008-06-09
28
28
 
29
29
  * Added support for Single Table Inheritance
30
30
  * Adding gemspec and rails/init.rb for gemified plugin
31
31
 
32
- == 2007-12-12
32
+ ## 2007-12-12
33
33
 
34
34
  * Added ability to use dynamic tag contexts
35
- * Fixed missing migration generator
35
+ * Fixed missing migration generator
@@ -1,4 +1,4 @@
1
- Copyright (c) 2007 Michael Bleigh and Intridea Inc.
1
+ __Copyright (c) 2007 Michael Bleigh and Intridea Inc.__
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,297 @@
1
+ # ActsAsTaggableOn
2
+ [![Build Status](https://secure.travis-ci.org/mbleigh/acts-as-taggable-on.png)](http://travis-ci.org/mbleigh/acts-as-taggable-on)
3
+
4
+ This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney.
5
+ It has evolved substantially since that point, but all credit goes to him for the
6
+ initial tagging functionality that so many people have used.
7
+
8
+ For instance, in a social network, a user might have tags that are called skills,
9
+ interests, sports, and more. There is no real way to differentiate between tags and
10
+ so an implementation of this type is not possible with acts as taggable on steroids.
11
+
12
+ Enter Acts as Taggable On. Rather than tying functionality to a specific keyword
13
+ (namely `tags`), acts as taggable on allows you to specify an arbitrary number of
14
+ tag "contexts" that can be used locally or in combination in the same way steroids
15
+ was used.
16
+
17
+ ## Installation
18
+
19
+ ### Rails 2.x
20
+
21
+ Not supported any more! It is time for update guys.
22
+
23
+ ### Rails 3.x
24
+
25
+ To use it, add it to your Gemfile:
26
+
27
+ ```ruby
28
+ gem 'acts-as-taggable-on', '~> 2.3.1'
29
+ ```
30
+
31
+ #### Post Installation
32
+
33
+ ```shell
34
+ rails generate acts_as_taggable_on:migration
35
+ rake db:migrate
36
+ ```
37
+
38
+ ## Testing
39
+
40
+ Acts As Taggable On uses RSpec for its test coverage. Inside the gem
41
+ directory, you can run the specs for RoR 3.x with:
42
+
43
+ ```shell
44
+ rake spec
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ```ruby
50
+ class User < ActiveRecord::Base
51
+ # Alias for acts_as_taggable_on :tags
52
+ acts_as_taggable
53
+ acts_as_taggable_on :skills, :interests
54
+ end
55
+
56
+ @user = User.new(:name => "Bobby")
57
+ @user.tag_list = "awesome, slick, hefty" # this should be familiar
58
+ @user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
59
+
60
+ @user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
61
+ @user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
62
+ @user.skill_list # => ["joking","clowning","boxing"] as TagList
63
+
64
+ @user.tag_list.remove("awesome") # remove a single tag
65
+ @user.tag_list.remove("awesome, slick") # works with arrays too
66
+ @user.tag_list.add("awesomer") # add a single tag. alias for <<
67
+ @user.tag_list.add("awesomer, slicker") # also works with arrays
68
+
69
+ User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
70
+ ```
71
+
72
+ To preserve the order in which tags are created use `acts_as_ordered_taggable`:
73
+
74
+ ```ruby
75
+ class User < ActiveRecord::Base
76
+ # Alias for acts_as_ordered_taggable_on :tags
77
+ acts_as_ordered_taggable
78
+ acts_as_ordered_taggable_on :skills, :interests
79
+ end
80
+
81
+ @user = User.new(:name => "Bobby")
82
+ @user.tag_list = "east, south"
83
+ @user.save
84
+
85
+ @user.tag_list = "north, east, south, west"
86
+ @user.save
87
+
88
+ @user.reload
89
+ @user.tag_list # => ["north", "east", "south", "west"]
90
+ ```
91
+
92
+ ### Finding Tagged Objects
93
+
94
+ Acts As Taggable On uses scopes to create an association for tags.
95
+ This way you can mix and match to filter down your results.
96
+
97
+ ```ruby
98
+ class User < ActiveRecord::Base
99
+ acts_as_taggable_on :tags, :skills
100
+ scope :by_join_date, order("created_at DESC")
101
+ end
102
+
103
+ User.tagged_with("awesome").by_join_date
104
+ User.tagged_with("awesome").by_join_date.paginate(:page => params[:page], :per_page => 20)
105
+
106
+ # Find a user with matching all tags, not just one
107
+ User.tagged_with(["awesome", "cool"], :match_all => true)
108
+
109
+ # Find a user with any of the tags:
110
+ User.tagged_with(["awesome", "cool"], :any => true)
111
+
112
+ # Find a user that not tags with awesome or cool:
113
+ User.tagged_with(["awesome", "cool"], :exclude => true)
114
+
115
+ # Find a user with any of tags based on context:
116
+ User.tagged_with(['awesome, cool'], :on => :tags, :any => true).tagged_with(['smart', 'shy'], :on => :skills, :any => true)
117
+ ```
118
+
119
+ You can also use `:wild => true` option along with `:any` or `:exclude` option. It will looking for `%awesome%` and `%cool%` in sql.
120
+
121
+ __Tip:__ `User.tagged_with([])` or '' will return `[]`, but not all records.
122
+
123
+ ### Relationships
124
+
125
+ You can find objects of the same type based on similar tags on certain contexts.
126
+ Also, objects will be returned in descending order based on the total number of
127
+ matched tags.
128
+
129
+ ```ruby
130
+ @bobby = User.find_by_name("Bobby")
131
+ @bobby.skill_list # => ["jogging", "diving"]
132
+
133
+ @frankie = User.find_by_name("Frankie")
134
+ @frankie.skill_list # => ["hacking"]
135
+
136
+ @tom = User.find_by_name("Tom")
137
+ @tom.skill_list # => ["hacking", "jogging", "diving"]
138
+
139
+ @tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
140
+ @bobby.find_related_skills # => [<User name="Tom">]
141
+ @frankie.find_related_skills # => [<User name="Tom">]
142
+ ```
143
+
144
+ ### Dynamic Tag Contexts
145
+
146
+ In addition to the generated tag contexts in the definition, it is also possible
147
+ to allow for dynamic tag contexts (this could be user generated tag contexts!)
148
+
149
+ ```ruby
150
+ @user = User.new(:name => "Bobby")
151
+ @user.set_tag_list_on(:customs, "same, as, tag, list")
152
+ @user.tag_list_on(:customs) # => ["same","as","tag","list"]
153
+ @user.save
154
+ @user.tags_on(:customs) # => [<Tag name='same'>,...]
155
+ @user.tag_counts_on(:customs)
156
+ User.tagged_with("same", :on => :customs) # => [@user]
157
+ ```
158
+
159
+ ### Tag Ownership
160
+
161
+ Tags can have owners:
162
+
163
+ ```ruby
164
+ class User < ActiveRecord::Base
165
+ acts_as_tagger
166
+ end
167
+
168
+ class Photo < ActiveRecord::Base
169
+ acts_as_taggable_on :locations
170
+ end
171
+
172
+ @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
173
+ @some_user.owned_taggings
174
+ @some_user.owned_tags
175
+ Photo.tagged_with("paris", :on => :locations, :owned_by => @some_user)
176
+ @some_photo.locations_from(@some_user) # => ["paris", "normandy"]
177
+ @some_photo.owner_tags_on(@some_user, :locations) # => [#<ActsAsTaggableOn::Tag id: 1, name: "paris">...]
178
+ @some_photo.owner_tags_on(nil, :locations) # => Ownerships equivalent to saying @some_photo.locations
179
+ @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations, :skip_save => true) #won't save @some_photo object
180
+ ```
181
+
182
+ ### Dirty objects
183
+
184
+ ```ruby
185
+ @bobby = User.find_by_name("Bobby")
186
+ @bobby.skill_list # => ["jogging", "diving"]
187
+
188
+ @bobby.skill_list_changed? #=> false
189
+ @bobby.changes #=> {}
190
+
191
+ @bobby.skill_list = "swimming"
192
+ @bobby.changes.should == {"skill_list"=>["jogging, diving", ["swimming"]]}
193
+ @bobby.skill_list_changed? #=> true
194
+
195
+ @bobby.skill_list_change.should == ["jogging, diving", ["swimming"]]
196
+ ```
197
+
198
+ ### Tag cloud calculations
199
+
200
+ To construct tag clouds, the frequency of each tag needs to be calculated.
201
+ Because we specified `acts_as_taggable_on` on the `User` class, we can
202
+ get a calculation of all the tag counts by using `User.tag_counts_on(:customs)`. But what if we wanted a tag count for
203
+ an single user's posts? To achieve this we call tag_counts on the association:
204
+
205
+ ```ruby
206
+ User.find(:first).posts.tag_counts_on(:tags)
207
+ ```
208
+
209
+ A helper is included to assist with generating tag clouds.
210
+
211
+ Here is an example that generates a tag cloud.
212
+
213
+ Helper:
214
+
215
+ ```ruby
216
+ module PostsHelper
217
+ include ActsAsTaggableOn::TagsHelper
218
+ end
219
+ ```
220
+
221
+ Controller:
222
+
223
+ ```ruby
224
+ class PostController < ApplicationController
225
+ def tag_cloud
226
+ @tags = Post.tag_counts_on(:tags)
227
+ end
228
+ end
229
+ ```
230
+
231
+ View:
232
+
233
+ ```erb
234
+ <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
235
+ <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
236
+ <% end %>
237
+ ```
238
+
239
+ CSS:
240
+
241
+ ```css
242
+ .css1 { font-size: 1.0em; }
243
+ .css2 { font-size: 1.2em; }
244
+ .css3 { font-size: 1.4em; }
245
+ .css4 { font-size: 1.6em; }
246
+ ```
247
+
248
+ ## Configuration
249
+
250
+ If you would like to remove unused tag objects after removing taggings, add:
251
+
252
+ ```ruby
253
+ ActsAsTaggableOn.remove_unused_tags = true
254
+ ```
255
+
256
+ If you want force tags to be saved downcased:
257
+
258
+ ```ruby
259
+ ActsAsTaggableOn.force_lowercase = true
260
+ ```
261
+
262
+ If you want tags to be saved parametrized (you can redefine to_param as well):
263
+
264
+ ```ruby
265
+ ActsAsTaggableOn.force_parameterize = true
266
+ ```
267
+
268
+ If you would like tags to be case-sensitive and not use LIKE queries for creation:
269
+
270
+ ```ruby
271
+ ActsAsTaggableOn.strict_case_match = true
272
+ ```
273
+
274
+ If you want to change the default delimiter (it defaults to ','). You can also pass in an array of delimiters such as ([',', '|']):
275
+
276
+ ```ruby
277
+ ActsAsTaggableOn.delimiter = ','
278
+ ```
279
+
280
+ ## Changelog
281
+
282
+ See [CHANGELOG](https://github.com/mbleigh/acts-as-taggable-on/blob/master/CHANGELOG.md).
283
+
284
+ ## Contributors
285
+
286
+ We have a long list of valued contributors. [Check them all](https://github.com/mbleigh/acts-as-taggable-on/contributors)
287
+
288
+ ## Maintainers
289
+
290
+ * [Artem Kramarenko](https://github.com/artemk) (artemk)
291
+ * [Joost Baaij](https://github.com/tilsammans)
292
+
293
+ ## Author
294
+
295
+ * [Michael Bleigh](https://github.com/mbleigh)
296
+
297
+ Copyright (c) 2007-2011 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the [MIT license](https://github.com/mbleigh/acts-as-taggable-on/blob/master/MIT-LICENSE.md)
@@ -16,6 +16,9 @@ module ActsAsTaggableOn
16
16
  mattr_accessor :force_parameterize
17
17
  @@force_parameterize = false
18
18
 
19
+ mattr_accessor :strict_case_match
20
+ @@strict_case_match = false
21
+
19
22
  mattr_accessor :remove_unused_tags
20
23
  self.remove_unused_tags = false
21
24
 
@@ -1,4 +1,4 @@
1
1
  module ActsAsTaggableOn
2
- VERSION = '2.3.3'
2
+ VERSION = '2.4.0.beta'
3
3
  end
4
4
 
@@ -17,11 +17,11 @@ module ActsAsTaggableOn::Taggable
17
17
  module ClassMethods
18
18
  def initialize_acts_as_taggable_on_cache
19
19
  tag_types.map(&:to_s).each do |tag_type|
20
- class_eval %(
20
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
21
21
  def self.caching_#{tag_type.singularize}_list?
22
22
  caching_tag_list_on?("#{tag_type}")
23
23
  end
24
- )
24
+ RUBY
25
25
  end
26
26
  end
27
27
 
@@ -9,7 +9,7 @@ module ActsAsTaggableOn::Taggable
9
9
  module ClassMethods
10
10
  def initialize_acts_as_taggable_on_collection
11
11
  tag_types.map(&:to_s).each do |tag_type|
12
- class_eval %(
12
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
13
13
  def self.#{tag_type.singularize}_counts(options={})
14
14
  tag_counts_on('#{tag_type}', options)
15
15
  end
@@ -25,7 +25,7 @@ module ActsAsTaggableOn::Taggable
25
25
  def self.top_#{tag_type}(limit = 10)
26
26
  tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
27
27
  end
28
- )
28
+ RUBY
29
29
  end
30
30
  end
31
31
 
@@ -37,6 +37,62 @@ module ActsAsTaggableOn::Taggable
37
37
  def tag_counts_on(context, options = {})
38
38
  all_tag_counts(options.merge({:on => context.to_s}))
39
39
  end
40
+
41
+ def tags_on(context, options = {})
42
+ all_tags(options.merge({:on => context.to_s}))
43
+ end
44
+
45
+ ##
46
+ # Calculate the tag names.
47
+ # To be used when you don't need tag counts and want to avoid the taggable joins.
48
+ #
49
+ # @param [Hash] options Options:
50
+ # * :start_at - Restrict the tags to those created after a certain time
51
+ # * :end_at - Restrict the tags to those created before a certain time
52
+ # * :conditions - A piece of SQL conditions to add to the query. Note we don't join the taggable objects for performance reasons.
53
+ # * :limit - The maximum number of tags to return
54
+ # * :order - A piece of SQL to order by. Eg 'tags.count desc' or 'taggings.created_at desc'
55
+ # * :on - Scope the find to only include a certain context
56
+ def all_tags(options = {})
57
+ options.assert_valid_keys :start_at, :end_at, :conditions, :order, :limit, :on
58
+
59
+ ## Generate conditions:
60
+ options[:conditions] = sanitize_sql(options[:conditions]) if options[:conditions]
61
+
62
+ start_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
63
+ end_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
64
+
65
+ taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name])
66
+ taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", options.delete(:on).to_s]) if options[:on]
67
+
68
+ tagging_conditions = [
69
+ taggable_conditions,
70
+ start_at_conditions,
71
+ end_at_conditions
72
+ ].compact.reverse
73
+
74
+ tag_conditions = [
75
+ options[:conditions]
76
+ ].compact.reverse
77
+
78
+ ## Generate scope:
79
+ tagging_scope = ActsAsTaggableOn::Tagging.select("#{ActsAsTaggableOn::Tagging.table_name}.tag_id")
80
+ tag_scope = ActsAsTaggableOn::Tag.select("#{ActsAsTaggableOn::Tag.table_name}.*").order(options[:order]).limit(options[:limit])
81
+
82
+ # Joins and conditions
83
+ tagging_conditions.each { |condition| tagging_scope = tagging_scope.where(condition) }
84
+ tag_conditions.each { |condition| tag_scope = tag_scope.where(condition) }
85
+
86
+ group_columns = "#{ActsAsTaggableOn::Tagging.table_name}.tag_id"
87
+
88
+ # Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore:
89
+ scoped_select = "#{table_name}.#{primary_key}"
90
+ tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{select(scoped_select).to_sql})").
91
+ group(group_columns)
92
+
93
+ tag_scope = tag_scope.joins("JOIN (#{tagging_scope.to_sql}) AS #{ActsAsTaggableOn::Tagging.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id")
94
+ tag_scope
95
+ end
40
96
 
41
97
  ##
42
98
  # Calculate the tag counts for all tags.
@@ -100,18 +156,21 @@ module ActsAsTaggableOn::Taggable
100
156
  tag_conditions.each { |condition| tag_scope = tag_scope.where(condition) }
101
157
 
102
158
  # GROUP BY and HAVING clauses:
103
- at_least = sanitize_sql(['tags_count >= ?', options.delete(:at_least)]) if options[:at_least]
104
- at_most = sanitize_sql(['tags_count <= ?', options.delete(:at_most)]) if options[:at_most]
159
+ at_least = sanitize_sql(["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) >= ?", options.delete(:at_least)]) if options[:at_least]
160
+ at_most = sanitize_sql(["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) <= ?", options.delete(:at_most)]) if options[:at_most]
105
161
  having = ["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) > 0", at_least, at_most].compact.join(' AND ')
106
162
 
107
163
  group_columns = "#{ActsAsTaggableOn::Tagging.table_name}.tag_id"
108
164
 
109
165
  # Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore:
110
166
  scoped_select = "#{table_name}.#{primary_key}"
111
- tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{select(scoped_select).to_sql})").
112
- group(group_columns).
113
- having(having)
167
+ select_query = "#{select(scoped_select).to_sql}"
168
+
169
+ res = ActiveRecord::Base.connection.select_all(select_query).map { |item| item.values }.flatten.join(",")
170
+ res = "NULL" if res.blank?
114
171
 
172
+ tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{res})")
173
+ tagging_scope = tagging_scope.group(group_columns).having(having)
115
174
 
116
175
  tag_scope = tag_scope.joins("JOIN (#{tagging_scope.to_sql}) AS #{ActsAsTaggableOn::Tagging.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id")
117
176
  tag_scope