acts-as-taggable-on 1.0.19 → 2.0.6
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.
- data/CHANGELOG +5 -2
- data/Gemfile +10 -0
- data/README.rdoc +48 -26
- data/Rakefile +45 -15
- data/VERSION +1 -1
- data/lib/acts-as-taggable-on.rb +30 -7
- data/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb +53 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb +132 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/core.rb +241 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb +101 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on/related.rb +65 -0
- data/lib/acts_as_taggable_on/acts_as_taggable_on.rb +43 -395
- data/lib/acts_as_taggable_on/acts_as_tagger.rb +60 -45
- data/lib/acts_as_taggable_on/compatibility/Gemfile +8 -0
- data/lib/acts_as_taggable_on/compatibility/active_record_backports.rb +17 -0
- data/lib/acts_as_taggable_on/compatibility/postgresql.rb +44 -0
- data/lib/acts_as_taggable_on/tag.rb +70 -34
- data/lib/acts_as_taggable_on/tag_list.rb +79 -79
- data/lib/acts_as_taggable_on/tagging.rb +23 -13
- data/lib/acts_as_taggable_on/tags_helper.rb +13 -9
- data/lib/generators/acts_as_taggable_on/migration/migration_generator.rb +32 -0
- data/lib/generators/acts_as_taggable_on/migration/templates/active_record/migration.rb +28 -0
- data/rails/init.rb +1 -5
- data/spec/acts_as_taggable_on/acts_as_taggable_on_spec.rb +104 -43
- data/spec/acts_as_taggable_on/acts_as_tagger_spec.rb +48 -6
- data/spec/acts_as_taggable_on/tag_list_spec.rb +5 -5
- data/spec/acts_as_taggable_on/tag_spec.rb +71 -29
- data/spec/acts_as_taggable_on/taggable_spec.rb +166 -87
- data/spec/acts_as_taggable_on/tagger_spec.rb +74 -6
- data/spec/acts_as_taggable_on/tagging_spec.rb +18 -12
- data/spec/acts_as_taggable_on/tags_helper_spec.rb +4 -6
- data/spec/bm.rb +52 -0
- data/spec/database.yml +17 -0
- data/spec/database.yml.sample +17 -0
- data/spec/models.rb +31 -0
- data/spec/schema.rb +13 -2
- data/spec/spec_helper.rb +49 -48
- metadata +29 -9
- data/lib/acts_as_taggable_on/group_helper.rb +0 -12
- data/spec/acts_as_taggable_on/group_helper_spec.rb +0 -18
- data/spec/spec.opts +0 -3
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
== 2010-02-17
|
|
2
|
+
* Converted the plugin to be compatible with Rails3
|
|
3
|
+
|
|
1
4
|
== 2009-12-02
|
|
2
5
|
|
|
3
6
|
* PostgreSQL is now supported (via morgoth)
|
|
@@ -12,10 +15,10 @@
|
|
|
12
15
|
* Removed extraneous down migration cruft (azabaj)
|
|
13
16
|
|
|
14
17
|
== 2008-06-09
|
|
15
|
-
|
|
18
|
+
|
|
16
19
|
* Added support for Single Table Inheritance
|
|
17
20
|
* Adding gemspec and rails/init.rb for gemified plugin
|
|
18
|
-
|
|
21
|
+
|
|
19
22
|
== 2007-12-12
|
|
20
23
|
|
|
21
24
|
* Added ability to use dynamic tag contexts
|
data/Gemfile
ADDED
data/README.rdoc
CHANGED
|
@@ -15,33 +15,53 @@ was used.
|
|
|
15
15
|
|
|
16
16
|
== Installation
|
|
17
17
|
|
|
18
|
-
===
|
|
18
|
+
=== Rails 2.3.x
|
|
19
|
+
|
|
20
|
+
Acts As Taggable On is tested to work in Rails 2.3.5.
|
|
21
|
+
|
|
22
|
+
==== Plugin
|
|
19
23
|
|
|
20
24
|
Acts As Taggable On is available both as a gem and as a traditional plugin. For the
|
|
21
|
-
traditional plugin you can install like so
|
|
25
|
+
traditional plugin you can install like so:
|
|
22
26
|
|
|
23
27
|
script/plugin install git://github.com/mbleigh/acts-as-taggable-on.git
|
|
24
|
-
|
|
25
|
-
=== GemPlugin
|
|
26
28
|
|
|
27
29
|
Acts As Taggable On is also available as a gem plugin using Rails 2.1's gem dependencies.
|
|
28
30
|
To install the gem, add this to your config/environment.rb:
|
|
29
31
|
|
|
30
|
-
config.gem "acts-as-taggable-on", :source => "http://gemcutter.org"
|
|
32
|
+
config.gem "acts-as-taggable-on", :source => "http://gemcutter.org", :version => '2.0.0.rc1'
|
|
31
33
|
|
|
32
34
|
After that, you can run "rake gems:install" to install the gem if you don't already have it.
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
==== Post Installation
|
|
35
37
|
|
|
36
38
|
1. script/generate acts_as_taggable_on_migration
|
|
37
39
|
2. rake db:migrate
|
|
38
40
|
|
|
39
|
-
===
|
|
41
|
+
=== Rails 3.0
|
|
42
|
+
|
|
43
|
+
Acts As Taggable On is now useable in Rails 3.0, thanks to the excellent work of Szymon Nowak
|
|
44
|
+
and Jelle Vandebeeck.
|
|
45
|
+
|
|
46
|
+
To use it, add it to your Gemfile:
|
|
47
|
+
|
|
48
|
+
gem 'acts-as-taggable-on'
|
|
49
|
+
|
|
50
|
+
==== Post Installation
|
|
51
|
+
|
|
52
|
+
1. rails generate acts_as_taggable_on:migration
|
|
53
|
+
2. rake db:migrate
|
|
54
|
+
|
|
55
|
+
== Testing
|
|
40
56
|
|
|
41
57
|
Acts As Taggable On uses RSpec for its test coverage. Inside the plugin
|
|
42
|
-
directory, you can run the specs with:
|
|
58
|
+
directory, you can run the specs for RoR 3.0.0 with:
|
|
59
|
+
|
|
60
|
+
rake spec
|
|
61
|
+
|
|
62
|
+
If you want to test the plugin for Rails 2.3.x, use:
|
|
43
63
|
|
|
44
|
-
rake spec
|
|
64
|
+
rake rails2.3:spec
|
|
45
65
|
|
|
46
66
|
If you already have RSpec on your application, the specs will run while using:
|
|
47
67
|
|
|
@@ -51,35 +71,29 @@ rake spec:plugins
|
|
|
51
71
|
== Usage
|
|
52
72
|
|
|
53
73
|
class User < ActiveRecord::Base
|
|
54
|
-
acts_as_taggable_on :tags
|
|
74
|
+
# Alias for <tt>acts_as_taggable_on :tags</tt>:
|
|
75
|
+
acts_as_taggable
|
|
76
|
+
acts_as_taggable_on :skills, :interests
|
|
55
77
|
end
|
|
56
78
|
|
|
57
79
|
@user = User.new(:name => "Bobby")
|
|
58
80
|
@user.tag_list = "awesome, slick, hefty" # this should be familiar
|
|
59
81
|
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
|
|
60
|
-
@user.skill_list
|
|
82
|
+
@user.skill_list # => ["joking","clowning","boxing"] as TagList
|
|
61
83
|
@user.save
|
|
62
84
|
|
|
63
85
|
@user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
|
|
64
86
|
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
|
|
65
87
|
|
|
66
|
-
# The old way
|
|
67
|
-
User.find_tagged_with("awesome", :on => :tags) # => [@user]
|
|
68
|
-
User.find_tagged_with("awesome", :on => :skills) # => []
|
|
69
|
-
|
|
70
|
-
# The better way (utilizes named_scope)
|
|
71
|
-
User.tagged_with("awesome", :on => :tags) # => [@user]
|
|
72
|
-
User.tagged_with("awesome", :on => :skills) # => []
|
|
73
|
-
|
|
74
88
|
@frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
|
|
75
89
|
User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
|
|
76
90
|
@frankie.skill_counts
|
|
77
91
|
|
|
78
92
|
=== Finding Tagged Objects
|
|
79
93
|
|
|
80
|
-
Acts As Taggable On utilizes
|
|
81
|
-
|
|
82
|
-
|
|
94
|
+
Acts As Taggable On utilizes named_scopes to create an association for tags.
|
|
95
|
+
This way you can mix and match to filter down your results, and it also improves
|
|
96
|
+
compatibility with the will_paginate gem:
|
|
83
97
|
|
|
84
98
|
class User < ActiveRecord::Base
|
|
85
99
|
acts_as_taggable_on :tags
|
|
@@ -89,6 +103,12 @@ also improves compatibility with the will_paginate gem:
|
|
|
89
103
|
User.tagged_with("awesome").by_date
|
|
90
104
|
User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)
|
|
91
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
|
+
|
|
92
112
|
=== Relationships
|
|
93
113
|
|
|
94
114
|
You can find objects of the same type based on similar tags on certain contexts.
|
|
@@ -119,7 +139,7 @@ to allow for dynamic tag contexts (this could be user generated tag contexts!)
|
|
|
119
139
|
@user.save
|
|
120
140
|
@user.tags_on(:customs) # => [<Tag name='same'>,...]
|
|
121
141
|
@user.tag_counts_on(:customs)
|
|
122
|
-
User.
|
|
142
|
+
User.tagged_with("same", :on => :customs) # => [@user]
|
|
123
143
|
|
|
124
144
|
=== Tag Ownership
|
|
125
145
|
|
|
@@ -154,7 +174,7 @@ Here is an example that generates a tag cloud.
|
|
|
154
174
|
Helper:
|
|
155
175
|
|
|
156
176
|
module PostsHelper
|
|
157
|
-
include TagsHelper
|
|
177
|
+
include ActsAsTaggableOn::TagsHelper
|
|
158
178
|
end
|
|
159
179
|
|
|
160
180
|
Controller:
|
|
@@ -182,11 +202,13 @@ CSS:
|
|
|
182
202
|
|
|
183
203
|
* TomEric (i76) - Maintainer
|
|
184
204
|
* Michael Bleigh - Original Author
|
|
205
|
+
* Szymon Nowak - Rails 3.0 compatibility
|
|
206
|
+
* Jelle Vandebeeck - Rails 3.0 compatibility
|
|
185
207
|
* Brendan Lim - Related Objects
|
|
186
208
|
* Pradeep Elankumaran - Taggers
|
|
187
209
|
* Sinclair Bain - Patch King
|
|
188
210
|
|
|
189
|
-
|
|
211
|
+
=== Patch Contributors
|
|
190
212
|
|
|
191
213
|
* tristanzdunn - Related objects of other classes
|
|
192
214
|
* azabaj - Fixed migrate down
|
|
@@ -196,4 +218,4 @@ CSS:
|
|
|
196
218
|
* lawrencepit - cached tag work
|
|
197
219
|
* sobrinho - fixed tag_cloud helper
|
|
198
220
|
|
|
199
|
-
Copyright (c) 2007-
|
|
221
|
+
Copyright (c) 2007-2010 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license
|
data/Rakefile
CHANGED
|
@@ -1,4 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
begin
|
|
2
|
+
# Rspec 1.3.0
|
|
3
|
+
require 'spec/rake/spectask'
|
|
4
|
+
|
|
5
|
+
desc 'Default: run specs'
|
|
6
|
+
task :default => :spec
|
|
7
|
+
Spec::Rake::SpecTask.new do |t|
|
|
8
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
|
12
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
|
13
|
+
t.rcov = true
|
|
14
|
+
t.rcov_opts = ['--exclude', 'spec']
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
rescue LoadError
|
|
18
|
+
# Rspec 2.0
|
|
19
|
+
require 'rspec/core/rake_task'
|
|
20
|
+
|
|
21
|
+
desc 'Default: run specs'
|
|
22
|
+
task :default => :spec
|
|
23
|
+
Rspec::Core::RakeTask.new do |t|
|
|
24
|
+
t.pattern = "spec/**/*_spec.rb"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
Rspec::Core::RakeTask.new('rcov') do |t|
|
|
28
|
+
t.pattern = "spec/**/*_spec.rb"
|
|
29
|
+
t.rcov = true
|
|
30
|
+
t.rcov_opts = ['--exclude', 'spec']
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
rescue LoadError
|
|
34
|
+
puts "Rspec not available. Install it with: gem install rspec"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
namespace 'rails2.3' do
|
|
38
|
+
task :spec do
|
|
39
|
+
gemfile = File.join(File.dirname(__FILE__), 'lib', 'acts_as_taggable_on', 'compatibility', 'Gemfile')
|
|
40
|
+
ENV['BUNDLE_GEMFILE'] = gemfile
|
|
41
|
+
Rake::Task['spec'].invoke
|
|
42
|
+
end
|
|
43
|
+
end
|
|
2
44
|
|
|
3
45
|
begin
|
|
4
46
|
require 'jeweler'
|
|
@@ -13,17 +55,5 @@ begin
|
|
|
13
55
|
end
|
|
14
56
|
Jeweler::GemcutterTasks.new
|
|
15
57
|
rescue LoadError
|
|
16
|
-
puts "Jeweler not available. Install it with:
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
desc 'Default: run specs'
|
|
20
|
-
task :default => :spec
|
|
21
|
-
Spec::Rake::SpecTask.new do |t|
|
|
22
|
-
t.spec_files = FileList["spec/**/*_spec.rb"]
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
Spec::Rake::SpecTask.new('rcov') do |t|
|
|
26
|
-
t.spec_files = FileList["spec/**/*_spec.rb"]
|
|
27
|
-
t.rcov = true
|
|
28
|
-
t.rcov_opts = ['--exclude', 'spec']
|
|
29
|
-
end
|
|
58
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
|
59
|
+
end
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
2.0.6
|
data/lib/acts-as-taggable-on.rb
CHANGED
|
@@ -1,7 +1,30 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
require
|
|
7
|
-
|
|
1
|
+
require "active_record"
|
|
2
|
+
require "action_view"
|
|
3
|
+
|
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
5
|
+
|
|
6
|
+
require "acts_as_taggable_on/compatibility/active_record_backports" if ActiveRecord::VERSION::MAJOR < 3
|
|
7
|
+
|
|
8
|
+
require "acts_as_taggable_on/acts_as_taggable_on"
|
|
9
|
+
require "acts_as_taggable_on/acts_as_taggable_on/core"
|
|
10
|
+
require "acts_as_taggable_on/acts_as_taggable_on/collection"
|
|
11
|
+
require "acts_as_taggable_on/acts_as_taggable_on/cache"
|
|
12
|
+
require "acts_as_taggable_on/acts_as_taggable_on/ownership"
|
|
13
|
+
require "acts_as_taggable_on/acts_as_taggable_on/related"
|
|
14
|
+
|
|
15
|
+
require "acts_as_taggable_on/acts_as_tagger"
|
|
16
|
+
require "acts_as_taggable_on/tag"
|
|
17
|
+
require "acts_as_taggable_on/tag_list"
|
|
18
|
+
require "acts_as_taggable_on/tags_helper"
|
|
19
|
+
require "acts_as_taggable_on/tagging"
|
|
20
|
+
|
|
21
|
+
$LOAD_PATH.shift
|
|
22
|
+
|
|
23
|
+
if defined?(ActiveRecord::Base)
|
|
24
|
+
ActiveRecord::Base.extend ActsAsTaggableOn::Taggable
|
|
25
|
+
ActiveRecord::Base.send :include, ActsAsTaggableOn::Tagger
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
if defined?(ActionView::Base)
|
|
29
|
+
ActionView::Base.send :include, ActsAsTaggableOn::TagsHelper
|
|
30
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module ActsAsTaggableOn::Taggable
|
|
2
|
+
module Cache
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# Skip adding caching capabilities if table not exists or no cache columns exist
|
|
5
|
+
return unless base.table_exists? && base.tag_types.any? { |context| base.column_names.include?("cached_#{context.to_s.singularize}_list") }
|
|
6
|
+
|
|
7
|
+
base.send :include, ActsAsTaggableOn::Taggable::Cache::InstanceMethods
|
|
8
|
+
base.extend ActsAsTaggableOn::Taggable::Cache::ClassMethods
|
|
9
|
+
|
|
10
|
+
base.class_eval do
|
|
11
|
+
before_save :save_cached_tag_list
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
base.intialize_acts_as_taggable_on_cache
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def intialize_acts_as_taggable_on_cache
|
|
19
|
+
tag_types.map(&:to_s).each do |tag_type|
|
|
20
|
+
class_eval %(
|
|
21
|
+
def self.caching_#{tag_type.singularize}_list?
|
|
22
|
+
caching_tag_list_on?("#{tag_type}")
|
|
23
|
+
end
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def acts_as_taggable_on(*args)
|
|
29
|
+
super(*args)
|
|
30
|
+
intialize_acts_as_taggable_on_cache
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def caching_tag_list_on?(context)
|
|
34
|
+
column_names.include?("cached_#{context.to_s.singularize}_list")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
module InstanceMethods
|
|
39
|
+
def save_cached_tag_list
|
|
40
|
+
tag_types.map(&:to_s).each do |tag_type|
|
|
41
|
+
if self.class.send("caching_#{tag_type.singularize}_list?")
|
|
42
|
+
if tag_list_cache_set_on(tag_type)
|
|
43
|
+
list = tag_list_cache_on(tag_type.singularize).to_a.flatten.compact.join(', ')
|
|
44
|
+
self["cached_#{tag_type.singularize}_list"] = list
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
true
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
module ActsAsTaggableOn::Taggable
|
|
2
|
+
module Collection
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.send :include, ActsAsTaggableOn::Taggable::Collection::InstanceMethods
|
|
5
|
+
base.extend ActsAsTaggableOn::Taggable::Collection::ClassMethods
|
|
6
|
+
base.initialize_acts_as_taggable_on_collection
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module ClassMethods
|
|
10
|
+
def initialize_acts_as_taggable_on_collection
|
|
11
|
+
tag_types.map(&:to_s).each do |tag_type|
|
|
12
|
+
class_eval %(
|
|
13
|
+
def self.#{tag_type.singularize}_counts(options={})
|
|
14
|
+
tag_counts_on('#{tag_type}', options)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def #{tag_type.singularize}_counts(options = {})
|
|
18
|
+
tag_counts_on('#{tag_type}', options)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def top_#{tag_type}(limit = 10)
|
|
22
|
+
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.top_#{tag_type}(limit = 10)
|
|
26
|
+
tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
|
|
27
|
+
end
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def acts_as_taggable_on(*args)
|
|
33
|
+
super(*args)
|
|
34
|
+
initialize_acts_as_taggable_on_collection
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def tag_counts_on(context, options = {})
|
|
38
|
+
all_tag_counts(options.merge({:on => context.to_s}))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
##
|
|
42
|
+
# Calculate the tag counts for all tags.
|
|
43
|
+
#
|
|
44
|
+
# @param [Hash] options Options:
|
|
45
|
+
# * :start_at - Restrict the tags to those created after a certain time
|
|
46
|
+
# * :end_at - Restrict the tags to those created before a certain time
|
|
47
|
+
# * :conditions - A piece of SQL conditions to add to the query
|
|
48
|
+
# * :limit - The maximum number of tags to return
|
|
49
|
+
# * :order - A piece of SQL to order by. Eg 'tags.count desc' or 'taggings.created_at desc'
|
|
50
|
+
# * :at_least - Exclude tags with a frequency less than the given value
|
|
51
|
+
# * :at_most - Exclude tags with a frequency greater than the given value
|
|
52
|
+
# * :on - Scope the find to only include a certain context
|
|
53
|
+
def all_tag_counts(options = {})
|
|
54
|
+
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on, :id
|
|
55
|
+
|
|
56
|
+
scope = if ActiveRecord::VERSION::MAJOR >= 3
|
|
57
|
+
{}
|
|
58
|
+
else
|
|
59
|
+
scope(:find) || {}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
## Generate conditions:
|
|
63
|
+
options[:conditions] = sanitize_sql(options[:conditions]) if options[:conditions]
|
|
64
|
+
|
|
65
|
+
start_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
|
|
66
|
+
end_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
|
|
67
|
+
|
|
68
|
+
taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name])
|
|
69
|
+
taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_id = ?", options.delete(:id)]) if options[:id]
|
|
70
|
+
|
|
71
|
+
conditions = [
|
|
72
|
+
taggable_conditions,
|
|
73
|
+
options[:conditions],
|
|
74
|
+
scope[:conditions],
|
|
75
|
+
start_at_conditions,
|
|
76
|
+
end_at_conditions
|
|
77
|
+
].compact.reverse
|
|
78
|
+
|
|
79
|
+
## Generate joins:
|
|
80
|
+
tagging_join = "LEFT OUTER JOIN #{ActsAsTaggableOn::Tagging.table_name} ON #{ActsAsTaggableOn::Tag.table_name}.id = #{ActsAsTaggableOn::Tagging.table_name}.tag_id"
|
|
81
|
+
tagging_join << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", options.delete(:on).to_s]) if options[:on]
|
|
82
|
+
|
|
83
|
+
taggable_join = "INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id"
|
|
84
|
+
taggable_join << " AND #{table_name}.#{inheritance_column} = '#{name}'" unless descends_from_active_record? # Current model is STI descendant, so add type checking to the join condition
|
|
85
|
+
|
|
86
|
+
joins = [
|
|
87
|
+
tagging_join,
|
|
88
|
+
taggable_join,
|
|
89
|
+
scope[:joins]
|
|
90
|
+
].compact
|
|
91
|
+
|
|
92
|
+
joins = joins.reverse if ActiveRecord::VERSION::MAJOR < 3
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
## Generate scope:
|
|
96
|
+
scope = ActsAsTaggableOn::Tag.scoped(:select => "#{ActsAsTaggableOn::Tag.table_name}.*, COUNT(*) AS count").order(options[:order]).limit(options[:limit])
|
|
97
|
+
|
|
98
|
+
# Joins and conditions
|
|
99
|
+
joins.each { |join| scope = scope.joins(join) }
|
|
100
|
+
conditions.each { |condition| scope = scope.where(condition) }
|
|
101
|
+
|
|
102
|
+
# GROUP BY and HAVING clauses:
|
|
103
|
+
at_least = sanitize_sql(['COUNT(*) >= ?', options.delete(:at_least)]) if options[:at_least]
|
|
104
|
+
at_most = sanitize_sql(['COUNT(*) <= ?', options.delete(:at_most)]) if options[:at_most]
|
|
105
|
+
having = [at_least, at_most].compact.join(' AND ')
|
|
106
|
+
|
|
107
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
|
108
|
+
# Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore:
|
|
109
|
+
scoped_select = "#{table_name}.#{primary_key}"
|
|
110
|
+
scope = scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{select(scoped_select).to_sql})")
|
|
111
|
+
|
|
112
|
+
# We have having() in RoR 3.0 so use it:
|
|
113
|
+
having = having.blank? ? "COUNT(*) > 0" : "COUNT(*) > 0 AND #{having}"
|
|
114
|
+
scope = scope.group(grouped_column_names_for(ActsAsTaggableOn::Tag)).having(having)
|
|
115
|
+
else
|
|
116
|
+
# Having is not available in 2.3.x:
|
|
117
|
+
group_by = "#{grouped_column_names_for(ActsAsTaggableOn::Tag)} HAVING COUNT(*) > 0"
|
|
118
|
+
group_by << " AND #{having}" unless having.blank?
|
|
119
|
+
scope = scope.group(group_by)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
scope
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
module InstanceMethods
|
|
127
|
+
def tag_counts_on(context, options={})
|
|
128
|
+
self.class.tag_counts_on(context, options.merge(:id => id))
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|