rylwin-acts-as-taggable-on 2.1.1a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.gitignore +8 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +10 -0
  4. data/CHANGELOG +35 -0
  5. data/Gemfile +3 -0
  6. data/Guardfile +5 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +222 -0
  9. data/Rakefile +13 -0
  10. data/acts-as-taggable-on.gemspec +28 -0
  11. data/generators/acts_as_taggable_on_migration/acts_as_taggable_on_migration_generator.rb +7 -0
  12. data/generators/acts_as_taggable_on_migration/templates/migration.rb +29 -0
  13. data/lib/acts-as-taggable-on/version.rb +4 -0
  14. data/lib/acts-as-taggable-on.rb +36 -0
  15. data/lib/acts_as_taggable_on/acts_as_tagger.rb +67 -0
  16. data/lib/acts_as_taggable_on/compatibility/Gemfile +8 -0
  17. data/lib/acts_as_taggable_on/compatibility/active_record_backports.rb +21 -0
  18. data/lib/acts_as_taggable_on/tag.rb +81 -0
  19. data/lib/acts_as_taggable_on/tag_list.rb +96 -0
  20. data/lib/acts_as_taggable_on/taggable/acts_as_taggable_on/cache.rb +53 -0
  21. data/lib/acts_as_taggable_on/taggable/acts_as_taggable_on/collection.rb +139 -0
  22. data/lib/acts_as_taggable_on/taggable/acts_as_taggable_on/core.rb +284 -0
  23. data/lib/acts_as_taggable_on/taggable/acts_as_taggable_on/ownership.rb +105 -0
  24. data/lib/acts_as_taggable_on/taggable/acts_as_taggable_on/related.rb +73 -0
  25. data/lib/acts_as_taggable_on/taggable/acts_as_tagger.rb +67 -0
  26. data/lib/acts_as_taggable_on/taggable/cache.rb +53 -0
  27. data/lib/acts_as_taggable_on/taggable/collection.rb +139 -0
  28. data/lib/acts_as_taggable_on/taggable/compatibility/Gemfile +8 -0
  29. data/lib/acts_as_taggable_on/taggable/compatibility/active_record_backports.rb +21 -0
  30. data/lib/acts_as_taggable_on/taggable/core.rb +284 -0
  31. data/lib/acts_as_taggable_on/taggable/ownership.rb +105 -0
  32. data/lib/acts_as_taggable_on/taggable/related.rb +73 -0
  33. data/lib/acts_as_taggable_on/taggable/tag.rb +81 -0
  34. data/lib/acts_as_taggable_on/taggable/tag_list.rb +96 -0
  35. data/lib/acts_as_taggable_on/taggable/tagging.rb +24 -0
  36. data/lib/acts_as_taggable_on/taggable/tags_helper.rb +17 -0
  37. data/lib/acts_as_taggable_on/taggable/utils.rb +31 -0
  38. data/lib/acts_as_taggable_on/taggable.rb +63 -0
  39. data/lib/acts_as_taggable_on/tagging.rb +24 -0
  40. data/lib/acts_as_taggable_on/tags_helper.rb +17 -0
  41. data/lib/acts_as_taggable_on/utils.rb +31 -0
  42. data/lib/generators/acts_as_taggable_on/migration/migration_generator.rb +39 -0
  43. data/lib/generators/acts_as_taggable_on/migration/templates/active_record/migration.rb +28 -0
  44. data/rails/init.rb +1 -0
  45. data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +366 -0
  46. data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +114 -0
  47. data/spec/acts_as_taggable_on/tag_list_spec.rb +74 -0
  48. data/spec/acts_as_taggable_on/tag_spec.rb +136 -0
  49. data/spec/acts_as_taggable_on/taggable_spec.rb +410 -0
  50. data/spec/acts_as_taggable_on/tagger_spec.rb +105 -0
  51. data/spec/acts_as_taggable_on/tagging_spec.rb +31 -0
  52. data/spec/acts_as_taggable_on/tags_helper_spec.rb +28 -0
  53. data/spec/acts_as_taggable_on/utils_spec.rb +22 -0
  54. data/spec/bm.rb +52 -0
  55. data/spec/database.yml.sample +19 -0
  56. data/spec/generators/acts_as_taggable_on/migration/migration_generator_spec.rb +22 -0
  57. data/spec/models.rb +44 -0
  58. data/spec/schema.rb +56 -0
  59. data/spec/spec_helper.rb +81 -0
  60. data/uninstall.rb +1 -0
  61. metadata +245 -0
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.log
2
+ *.sqlite3
3
+ /pkg/*
4
+ .bundle
5
+ .rvmrc
6
+ Gemfile.lock
7
+ spec/database.yml
8
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ script: "cp spec/database.yml.sample spec/database.yml && bundle install && bundle exec rake"
2
+ rvm:
3
+ - 1.8.7
4
+ - ree
5
+ - 1.9.2
6
+ - rbx
7
+ env:
8
+ - DB=sqlite3
9
+ - DB=mysql
10
+ - DB=postgresql
data/CHANGELOG ADDED
@@ -0,0 +1,35 @@
1
+ == 2011-08-21
2
+ * escape _ and % for mysql and postgres (@tilsammans)
3
+ * Now depends on mysql2 gem
4
+ * tagged_with :any is chainable now (@jeffreyiacono)
5
+ * tagged_with(nil) returns scoped object
6
+ * Case-insensitivity for TaggedModel.tagged_with for PostgreSQL database
7
+ * tagged_with(' ') returns scoped object
8
+ * remove warning for rails 3.1 about class_inheritable_attribute
9
+ * use ActiveRecord migration_number to avoid clashs (@atd)
10
+
11
+ == 2010-02-17
12
+ * Converted the plugin to be compatible with Rails3
13
+
14
+ == 2009-12-02
15
+
16
+ * PostgreSQL is now supported (via morgoth)
17
+
18
+ == 2008-07-17
19
+
20
+ * Can now use a named_scope to find tags!
21
+
22
+ == 2008-06-23
23
+
24
+ * Can now find related objects of another class (tristanzdunn)
25
+ * Removed extraneous down migration cruft (azabaj)
26
+
27
+ == 2008-06-09
28
+
29
+ * Added support for Single Table Inheritance
30
+ * Adding gemspec and rails/init.rb for gemified plugin
31
+
32
+ == 2007-12-12
33
+
34
+ * Added ability to use dynamic tag contexts
35
+ * Fixed missing migration generator
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
3
+
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb})
3
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/MIT-LICENSE 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.rdoc ADDED
@@ -0,0 +1,222 @@
1
+ = ActsAsTaggableOn
2
+
3
+ This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney.
4
+ It has evolved substantially since that point, but all credit goes to him for the
5
+ initial tagging functionality that so many people have used.
6
+
7
+ For instance, in a social network, a user might have tags that are called skills,
8
+ interests, sports, and more. There is no real way to differentiate between tags and
9
+ so an implementation of this type is not possible with acts as taggable on steroids.
10
+
11
+ Enter Acts as Taggable On. Rather than tying functionality to a specific keyword
12
+ (namely "tags"), acts as taggable on allows you to specify an arbitrary number of
13
+ tag "contexts" that can be used locally or in combination in the same way steroids
14
+ was used.
15
+
16
+ == Installation
17
+
18
+ === Rails 2.3.x
19
+
20
+ The last version of gem that is compatible with Rails 2.3.x is 2.1.0.
21
+
22
+ We are planning no longer to support Rails 2, but if you want to contribute something only for Rails 2, we left rails2 branch for that.
23
+
24
+ ==== Plugin
25
+
26
+ Acts As Taggable On is available both as a gem and as a traditional plugin. For the
27
+ traditional plugin you can install like so:
28
+
29
+ script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git
30
+
31
+ Acts As Taggable On is also available as a gem plugin using Rails 2.1's gem dependencies.
32
+ To install the gem, add this to your config/environment.rb:
33
+
34
+ config.gem "acts-as-taggable-on", :source => "http://gemcutter.org", :version => '2.1.0'
35
+
36
+ After that, you can run "rake gems:install" to install the gem if you don't already have it.
37
+
38
+ ==== Post Installation
39
+
40
+ 1. script/generate acts_as_taggable_on_migration
41
+ 2. rake db:migrate
42
+
43
+ === Rails 3.x
44
+
45
+ To use it, add it to your Gemfile:
46
+
47
+ gem 'acts-as-taggable-on', '~>2.1.0'
48
+
49
+ ==== Post Installation
50
+
51
+ 1. rails generate acts_as_taggable_on:migration
52
+ 2. rake db:migrate
53
+
54
+ == Testing
55
+
56
+ Acts As Taggable On uses RSpec for its test coverage. Inside the gem
57
+ directory, you can run the specs for RoR 3.x with:
58
+
59
+ rake spec
60
+
61
+ == Usage
62
+
63
+ class User < ActiveRecord::Base
64
+ # Alias for <tt>acts_as_taggable_on :tags</tt>:
65
+ acts_as_taggable
66
+ acts_as_taggable_on :skills, :interests
67
+ end
68
+
69
+ @user = User.new(:name => "Bobby")
70
+ @user.tag_list = "awesome, slick, hefty" # this should be familiar
71
+ @user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
72
+ @user.skill_list # => ["joking","clowning","boxing"] as TagList
73
+ @user.save
74
+
75
+ @user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
76
+ @user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
77
+
78
+ @frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
79
+ User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
80
+ @frankie.skill_counts
81
+
82
+ === Finding Tagged Objects
83
+
84
+ Acts As Taggable On utilizes named_scopes to create an association for tags.
85
+ This way you can mix and match to filter down your results, and it also improves
86
+ compatibility with the will_paginate gem:
87
+
88
+ class User < ActiveRecord::Base
89
+ acts_as_taggable_on :tags, :skills
90
+ named_scope :by_join_date, :order => "created_at DESC"
91
+ end
92
+
93
+ User.tagged_with("awesome").by_date
94
+ User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)
95
+
96
+ # Find a user with matching all tags, not just one
97
+ User.tagged_with(["awesome", "cool"], :match_all => :true)
98
+
99
+ # Find a user with any of the tags:
100
+ User.tagged_with(["awesome", "cool"], :any => true)
101
+
102
+ # Find a user with any of tags based on context:
103
+ User.tagged_with(['awesome, cool'], :on => :tags, :any => true).tagged_with(['smart', 'shy'], :on => :skills, :any => true)
104
+
105
+ === Relationships
106
+
107
+ You can find objects of the same type based on similar tags on certain contexts.
108
+ Also, objects will be returned in descending order based on the total number of
109
+ matched tags.
110
+
111
+ @bobby = User.find_by_name("Bobby")
112
+ @bobby.skill_list # => ["jogging", "diving"]
113
+
114
+ @frankie = User.find_by_name("Frankie")
115
+ @frankie.skill_list # => ["hacking"]
116
+
117
+ @tom = User.find_by_name("Tom")
118
+ @tom.skill_list # => ["hacking", "jogging", "diving"]
119
+
120
+ @tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
121
+ @bobby.find_related_skills # => [<User name="Tom">]
122
+ @frankie.find_related_skills # => [<User name="Tom">]
123
+
124
+ === Dynamic Tag Contexts
125
+
126
+ In addition to the generated tag contexts in the definition, it is also possible
127
+ to allow for dynamic tag contexts (this could be user generated tag contexts!)
128
+
129
+ @user = User.new(:name => "Bobby")
130
+ @user.set_tag_list_on(:customs, "same, as, tag, list")
131
+ @user.tag_list_on(:customs) # => ["same","as","tag","list"]
132
+ @user.save
133
+ @user.tags_on(:customs) # => [<Tag name='same'>,...]
134
+ @user.tag_counts_on(:customs)
135
+ User.tagged_with("same", :on => :customs) # => [@user]
136
+
137
+ === Tag Ownership
138
+
139
+ Tags can have owners:
140
+
141
+ class User < ActiveRecord::Base
142
+ acts_as_tagger
143
+ end
144
+
145
+ class Photo < ActiveRecord::Base
146
+ acts_as_taggable_on :locations
147
+ end
148
+
149
+ @some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
150
+ @some_user.owned_taggings
151
+ @some_user.owned_tags
152
+ @some_photo.locations_from(@some_user) # => ["paris", "normandy"]
153
+ @some_photo.owner_tags_on(@some_user, :locations) # => [#<ActsAsTaggableOn::Tag id: 1, name: "paris">...]
154
+ @some_photo.owner_tags_on(nil, :locations) # => Ownerships equivalent to saying @some_photo.locations
155
+
156
+ === Tag cloud calculations
157
+
158
+ To construct tag clouds, the frequency of each tag needs to be calculated.
159
+ Because we specified +acts_as_taggable_on+ on the <tt>User</tt> class, we can
160
+ get a calculation of all the tag counts by using <tt>User.tag_counts_on(:customs)</tt>. But what if we wanted a tag count for
161
+ an single user's posts? To achieve this we call tag_counts on the association:
162
+
163
+ User.find(:first).posts.tag_counts_on(:tags)
164
+
165
+ A helper is included to assist with generating tag clouds.
166
+
167
+ Here is an example that generates a tag cloud.
168
+
169
+ Helper:
170
+
171
+ module PostsHelper
172
+ include ActsAsTaggableOn::TagsHelper
173
+ end
174
+
175
+ Controller:
176
+
177
+ class PostController < ApplicationController
178
+ def tag_cloud
179
+ @tags = Post.tag_counts_on(:tags)
180
+ end
181
+ end
182
+
183
+ View:
184
+
185
+ <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
186
+ <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
187
+ <% end %>
188
+
189
+ CSS:
190
+
191
+ .css1 { font-size: 1.0em; }
192
+ .css2 { font-size: 1.2em; }
193
+ .css3 { font-size: 1.4em; }
194
+ .css4 { font-size: 1.6em; }
195
+
196
+ == Why TaggableModel.joins(:tags) doesn't work (only for Rails >=3.0.10)?
197
+
198
+ When you define acts_as_taggable_on in your model it add <tt>has_many :through</tt> associations to your model. In this +has_many+ declaration we join 'tags' table using rails ":include" statement, also we use additional ":conditions" statement where we refer to joined 'tags' table. In rails 3.0.10 they changed logic, and {now}[https://github.com/rails/rails/commit/caec63929fabe0368da71128c96e74c174791eb9] <tt>joins</tt> which is mode on model, substitutes 'include' from has_many declaration and place 'joins' at the end of sql, after 'conditions'. So Joining is happened but after 'Where'.
199
+ While we are trying to figure out how to fix this, please use Rails 3.0.9 or don't use Model.joins(:tags)
200
+
201
+ == Contributors
202
+
203
+ * Artem Kramarenko (artemk) - Maintainer
204
+ * TomEric (i76) - Maintainer
205
+ * Michael Bleigh - Original Author
206
+ * Szymon Nowak - Rails 3.0 compatibility
207
+ * Jelle Vandebeeck - Rails 3.0 compatibility
208
+ * Brendan Lim - Related Objects
209
+ * Pradeep Elankumaran - Taggers
210
+ * Sinclair Bain - Patch King
211
+
212
+ === Patch Contributors
213
+
214
+ * tristanzdunn - Related objects of other classes
215
+ * azabaj - Fixed migrate down
216
+ * Peter Cooper - named_scope fix
217
+ * slainer68 - STI fix
218
+ * harrylove - migration instructions and fix-ups
219
+ * lawrencepit - cached tag work
220
+ * sobrinho - fixed tag_cloud helper
221
+
222
+ Copyright (c) 2007-2011 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup :default, :development
4
+
5
+ desc 'Default: run specs'
6
+ task :default => :spec
7
+
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "spec/**/*_spec.rb"
11
+ end
12
+
13
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,28 @@
1
+ $:.push File.dirname(__FILE__) + '/lib'
2
+ require 'acts-as-taggable-on/version'
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = %q{rylwin-acts-as-taggable-on}
6
+ gem.authors = ["Michael Bleigh", "Ryan Winograd"]
7
+ gem.date = %q{2010-05-19}
8
+ gem.description = %q{ActsAsTaggableOn, with Expected /lib/acts_as_taggable_on/acts_as_taggable_on.rb to define ActsAsTaggableOn::ActsAsTaggableOn fixed as described by rymai (https://github.com/mbleigh/acts-as-taggable-on/issues/79#issuecomment-262073).}
9
+ gem.summary = "Advanced tagging for Rails."
10
+ gem.email = %q{ryan@thewinograds.com}
11
+ gem.homepage = ''
12
+
13
+ gem.add_runtime_dependency 'rails'
14
+ gem.add_development_dependency 'rspec', '~> 2.5'
15
+ gem.add_development_dependency 'ammeter', '~> 0.1.3'
16
+ gem.add_development_dependency 'sqlite3'
17
+ gem.add_development_dependency 'mysql2', '< 0.3'
18
+ gem.add_development_dependency 'pg'
19
+ gem.add_development_dependency 'guard'
20
+ gem.add_development_dependency 'guard-rspec'
21
+
22
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ gem.files = `git ls-files`.split("\n")
24
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ gem.name = "rylwin-acts-as-taggable-on"
26
+ gem.require_paths = ['lib']
27
+ gem.version = ActsAsTaggableOn::VERSION
28
+ end
@@ -0,0 +1,7 @@
1
+ class ActsAsTaggableOnMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "acts_as_taggable_on_migration"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ class ActsAsTaggableOnMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tags do |t|
4
+ t.column :name, :string
5
+ end
6
+
7
+ create_table :taggings do |t|
8
+ t.column :tag_id, :integer
9
+ t.column :taggable_id, :integer
10
+ t.column :tagger_id, :integer
11
+ t.column :tagger_type, :string
12
+
13
+ # You should make sure that the column created is
14
+ # long enough to store the required class names.
15
+ t.column :taggable_type, :string
16
+ t.column :context, :string
17
+
18
+ t.column :created_at, :datetime
19
+ end
20
+
21
+ add_index :taggings, :tag_id
22
+ add_index :taggings, [:taggable_id, :taggable_type, :context]
23
+ end
24
+
25
+ def self.down
26
+ drop_table :taggings
27
+ drop_table :tags
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module ActsAsTaggableOn
2
+ VERSION = '2.1.1a'
3
+ end
4
+
@@ -0,0 +1,36 @@
1
+ require "active_record"
2
+ require "active_record/version"
3
+ require "action_view"
4
+ RAILS_3 = ::ActiveRecord::VERSION::MAJOR >= 3
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require "acts_as_taggable_on/compatibility/active_record_backports" unless RAILS_3
9
+
10
+ require "acts_as_taggable_on/utils"
11
+
12
+ require "acts_as_taggable_on/taggable"
13
+ require "acts_as_taggable_on/taggable/core"
14
+ require "acts_as_taggable_on/taggable/collection"
15
+ require "acts_as_taggable_on/taggable/cache"
16
+ require "acts_as_taggable_on/taggable/ownership"
17
+ require "acts_as_taggable_on/taggable/related"
18
+
19
+ #require "acts_as_taggable_on/utils"
20
+ require "acts_as_taggable_on/acts_as_tagger"
21
+ require "acts_as_taggable_on/tag"
22
+ require "acts_as_taggable_on/tag_list"
23
+ require "acts_as_taggable_on/tags_helper"
24
+ require "acts_as_taggable_on/tagging"
25
+
26
+ $LOAD_PATH.shift
27
+
28
+
29
+ if defined?(ActiveRecord::Base)
30
+ ActiveRecord::Base.extend ActsAsTaggableOn::Taggable
31
+ ActiveRecord::Base.send :include, ActsAsTaggableOn::Tagger
32
+ end
33
+
34
+ if defined?(ActionView::Base)
35
+ ActionView::Base.send :include, ActsAsTaggableOn::TagsHelper
36
+ end
@@ -0,0 +1,67 @@
1
+ module ActsAsTaggableOn
2
+ module Tagger
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ ##
9
+ # Make a model a tagger. This allows an instance of a model to claim ownership
10
+ # of tags.
11
+ #
12
+ # Example:
13
+ # class User < ActiveRecord::Base
14
+ # acts_as_tagger
15
+ # end
16
+ def acts_as_tagger(opts={})
17
+ class_eval do
18
+ has_many :owned_taggings, opts.merge(:as => :tagger, :dependent => :destroy,
19
+ :include => :tag, :class_name => "ActsAsTaggableOn::Tagging")
20
+ has_many :owned_tags, :through => :owned_taggings, :source => :tag, :uniq => true, :class_name => "ActsAsTaggableOn::Tag"
21
+ end
22
+
23
+ include ActsAsTaggableOn::Tagger::InstanceMethods
24
+ extend ActsAsTaggableOn::Tagger::SingletonMethods
25
+ end
26
+
27
+ def is_tagger?
28
+ false
29
+ end
30
+ end
31
+
32
+ module InstanceMethods
33
+ ##
34
+ # Tag a taggable model with tags that are owned by the tagger.
35
+ #
36
+ # @param taggable The object that will be tagged
37
+ # @param [Hash] options An hash with options. Available options are:
38
+ # * <tt>:with</tt> - The tags that you want to
39
+ # * <tt>:on</tt> - The context on which you want to tag
40
+ #
41
+ # Example:
42
+ # @user.tag(@photo, :with => "paris, normandy", :on => :locations)
43
+ def tag(taggable, opts={})
44
+ opts.reverse_merge!(:force => true)
45
+
46
+ return false unless taggable.respond_to?(:is_taggable?) && taggable.is_taggable?
47
+
48
+ raise "You need to specify a tag context using :on" unless opts.has_key?(:on)
49
+ raise "You need to specify some tags using :with" unless opts.has_key?(:with)
50
+ raise "No context :#{opts[:on]} defined in #{taggable.class.to_s}" unless (opts[:force] || taggable.tag_types.include?(opts[:on]))
51
+
52
+ taggable.set_owner_tag_list_on(self, opts[:on].to_s, opts[:with])
53
+ taggable.save
54
+ end
55
+
56
+ def is_tagger?
57
+ self.class.is_tagger?
58
+ end
59
+ end
60
+
61
+ module SingletonMethods
62
+ def is_tagger?
63
+ true
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,8 @@
1
+ source :gemcutter
2
+
3
+ # Rails 2.3
4
+ gem 'rails', '2.3.5'
5
+ gem 'rspec', '1.3.0', :require => 'spec'
6
+ gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
7
+ gem 'mysql2', '~> 0.2.7'
8
+ gem 'pg'
@@ -0,0 +1,21 @@
1
+ module ActsAsTaggableOn
2
+ module ActiveRecord
3
+ module Backports
4
+ def self.included(base)
5
+ base.class_eval do
6
+ named_scope :where, lambda { |conditions| { :conditions => conditions } }
7
+ named_scope :joins, lambda { |joins| { :joins => joins } }
8
+ named_scope :group, lambda { |group| { :group => group } }
9
+ named_scope :order, lambda { |order| { :order => order } }
10
+ named_scope :select, lambda { |select| { :select => select } }
11
+ named_scope :limit, lambda { |limit| { :limit => limit } }
12
+ named_scope :readonly, lambda { |readonly| { :readonly => readonly } }
13
+
14
+ def self.to_sql
15
+ construct_finder_sql({})
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,81 @@
1
+ module ActsAsTaggableOn
2
+ class Tag < ::ActiveRecord::Base
3
+ include ActsAsTaggableOn::ActiveRecord::Backports if ::ActiveRecord::VERSION::MAJOR < 3
4
+ include ActsAsTaggableOn::Utils
5
+
6
+ attr_accessible :name
7
+
8
+ ### ASSOCIATIONS:
9
+
10
+ has_many :taggings, :dependent => :destroy, :class_name => 'ActsAsTaggableOn::Tagging'
11
+
12
+ ### VALIDATIONS:
13
+
14
+ validates_presence_of :name
15
+ validates_uniqueness_of :name
16
+
17
+ ### SCOPES:
18
+
19
+ def self.named(name)
20
+ where(["name #{like_operator} ?", escape_like(name)])
21
+ end
22
+
23
+ def self.named_any(list)
24
+ where(list.map { |tag| sanitize_sql(["name #{like_operator} ?", escape_like(tag.to_s)]) }.join(" OR "))
25
+ end
26
+
27
+ def self.named_like(name)
28
+ where(["name #{like_operator} ?", "%#{escape_like(name)}%"])
29
+ end
30
+
31
+ def self.named_like_any(list)
32
+ where(list.map { |tag| sanitize_sql(["name #{like_operator} ?", "%#{escape_like(tag.to_s)}%"]) }.join(" OR "))
33
+ end
34
+
35
+ ### CLASS METHODS:
36
+
37
+ def self.find_or_create_with_like_by_name(name)
38
+ named_like(name).first || create(:name => name)
39
+ end
40
+
41
+ def self.find_or_create_all_with_like_by_name(*list)
42
+ list = [list].flatten
43
+
44
+ return [] if list.empty?
45
+
46
+ existing_tags = Tag.named_any(list).all
47
+ new_tag_names = list.reject do |name|
48
+ name = comparable_name(name)
49
+ existing_tags.any? { |tag| comparable_name(tag.name) == name }
50
+ end
51
+ created_tags = new_tag_names.map { |name| Tag.create(:name => name) }
52
+
53
+ existing_tags + created_tags
54
+ end
55
+
56
+ ### INSTANCE METHODS:
57
+
58
+ def ==(object)
59
+ super || (object.is_a?(Tag) && name == object.name)
60
+ end
61
+
62
+ def to_s
63
+ name
64
+ end
65
+
66
+ def count
67
+ read_attribute(:count).to_i
68
+ end
69
+
70
+ def safe_name
71
+ name.gsub(/[^a-zA-Z0-9]/, '')
72
+ end
73
+
74
+ class << self
75
+ private
76
+ def comparable_name(str)
77
+ RUBY_VERSION >= "1.9" ? str.downcase : str.mb_chars.downcase
78
+ end
79
+ end
80
+ end
81
+ end