test_redmine_vz 0.0.24
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 +18 -0
- data/Gemfile +4 -0
- data/README.md +71 -0
- data/Rakefile +20 -0
- data/config/currency_iso.json +2532 -0
- data/doc/CHANGELOG +55 -0
- data/doc/LICENSE.txt +339 -0
- data/lib/redmine_crm.rb +67 -0
- data/lib/redmine_crm/currency.rb +439 -0
- data/lib/redmine_crm/currency/formatting.rb +227 -0
- data/lib/redmine_crm/currency/heuristics.rb +151 -0
- data/lib/redmine_crm/currency/loader.rb +24 -0
- data/lib/redmine_crm/helpers/tags_helper.rb +15 -0
- data/lib/redmine_crm/helpers/vote_helper.rb +38 -0
- data/lib/redmine_crm/liquid/drops/issues_drop.rb +61 -0
- data/lib/redmine_crm/liquid/drops/news_drop.rb +45 -0
- data/lib/redmine_crm/liquid/drops/projects_drop.rb +78 -0
- data/lib/redmine_crm/liquid/drops/users_drop.rb +59 -0
- data/lib/redmine_crm/liquid/filters.rb +85 -0
- data/lib/redmine_crm/money_helper.rb +67 -0
- data/lib/redmine_crm/rcrm_acts_as_taggable.rb +342 -0
- data/lib/redmine_crm/rcrm_acts_as_viewed.rb +287 -0
- data/lib/redmine_crm/rcrm_acts_as_votable.rb +79 -0
- data/lib/redmine_crm/rcrm_acts_as_voter.rb +27 -0
- data/lib/redmine_crm/tag.rb +81 -0
- data/lib/redmine_crm/tag_list.rb +112 -0
- data/lib/redmine_crm/tagging.rb +20 -0
- data/lib/redmine_crm/version.rb +3 -0
- data/lib/redmine_crm/votable.rb +334 -0
- data/lib/redmine_crm/vote.rb +30 -0
- data/lib/redmine_crm/voter.rb +136 -0
- data/redmine_crm.gemspec +22 -0
- data/test/acts_as_taggable_test.rb +384 -0
- data/test/currency_test.rb +292 -0
- data/test/database.yml +17 -0
- data/test/fixtures/issue.rb +14 -0
- data/test/fixtures/issues.yml +12 -0
- data/test/fixtures/taggings.yml +32 -0
- data/test/fixtures/tags.yml +11 -0
- data/test/fixtures/user.rb +7 -0
- data/test/fixtures/users.yml +5 -0
- data/test/fixtures/votable_caches.yml +2 -0
- data/test/fixtures/votables.yml +4 -0
- data/test/fixtures/vote_classes.rb +54 -0
- data/test/fixtures/voters.yml +6 -0
- data/test/liquid_test.rb +80 -0
- data/test/money_helper_test.rb +12 -0
- data/test/schema.rb +100 -0
- data/test/tag_test.rb +63 -0
- data/test/tagging_test.rb +14 -0
- data/test/tags_helper_test.rb +29 -0
- data/test/test_helper.rb +118 -0
- data/test/viewed_test.rb +45 -0
- data/test/votable_model_test.rb +478 -0
- data/test/votable_test.rb +17 -0
- data/test/vote_helper_test.rb +28 -0
- data/test/voter_model_test.rb +296 -0
- metadata +141 -0
@@ -0,0 +1,287 @@
|
|
1
|
+
# Copyright (c) 2008 Damian Martinelli
|
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.
|
21
|
+
module RedmineCrm
|
22
|
+
module ActiveRecord #:nodoc:
|
23
|
+
module Acts #:nodoc:
|
24
|
+
|
25
|
+
# == acts_as_viewed
|
26
|
+
# Adds views count capabilities to any ActiveRecord object.
|
27
|
+
# It has the ability to work with objects that have or don't special fields to keep a tally of the
|
28
|
+
# viewings for each object.
|
29
|
+
# In addition it will by default use the User model as the viewer object and keep the viewings per-user.
|
30
|
+
# It can be configured to use another class.
|
31
|
+
# The IP address are used to not repeat views from the same ip. Only one view are count by user or IP.
|
32
|
+
#
|
33
|
+
# Special methods are provided to create the viewings table and if needed, to add the special fields needed
|
34
|
+
# to keep per-objects viewings fast for access to viewed objects. Can be easily used in migrations.
|
35
|
+
#
|
36
|
+
# == Example of usage:
|
37
|
+
#
|
38
|
+
# class Video < ActiveRecord::Base
|
39
|
+
# acts_as_viewed
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# In a controller:
|
43
|
+
#
|
44
|
+
# bill = User.find_by_name 'bill'
|
45
|
+
# batman = Video.find_by_title 'Batman'
|
46
|
+
# toystory = Video.find_by_title 'Toy Story'
|
47
|
+
#
|
48
|
+
# batman.view request.remote_addr, bill
|
49
|
+
# toystory.view request.remote_addr, bill
|
50
|
+
#
|
51
|
+
# batman.view_count # => 1
|
52
|
+
#
|
53
|
+
#
|
54
|
+
module Viewed
|
55
|
+
|
56
|
+
class ViewedError < RuntimeError; end
|
57
|
+
|
58
|
+
def self.included(base) #:nodoc:
|
59
|
+
base.extend(ClassMethods)
|
60
|
+
end
|
61
|
+
|
62
|
+
module ClassMethods
|
63
|
+
|
64
|
+
# Make the model viewable.
|
65
|
+
# The Viewing model, holding the details of the viewings, will be created dynamically if it doesn't exist.
|
66
|
+
#
|
67
|
+
# * Adds a <tt>has_many :viewings</tt> association to the model for easy retrieval of the detailed viewings.
|
68
|
+
# * Adds a <tt>has_many :viewers</tt> association to the object.
|
69
|
+
# * Adds a <tt>has_many :viewings</tt> associations to the viewer class.
|
70
|
+
#
|
71
|
+
# === Options
|
72
|
+
# * <tt>:viewing_class</tt> -
|
73
|
+
# class of the model used for the viewings. Defaults to Viewing. This class will be dynamically created if not already defined.
|
74
|
+
# If the class is predefined, it must have in it the following definitions:
|
75
|
+
# <tt>belongs_to :viewed, :polymorphic => true</tt>
|
76
|
+
# <tt>belongs_to :viewer, :class_name => 'User', :foreign_key => :viewer_id</tt> replace user with the viewer class if needed.
|
77
|
+
# * <tt>:viewer_class</tt> -
|
78
|
+
# class of the model that creates the viewing.
|
79
|
+
# Defaults to User This class will NOT be created, so it must be defined in the app.
|
80
|
+
# Use the IP address to prevent multiple viewings from the same client.
|
81
|
+
#
|
82
|
+
def rcrm_acts_as_viewed(options = {})
|
83
|
+
# don't allow multiple calls
|
84
|
+
return if self.included_modules.include?(ActiveRecord::Acts::Viewed::ViewMethods)
|
85
|
+
send :include, ActiveRecord::Acts::Viewed::ViewMethods
|
86
|
+
|
87
|
+
# Create the model for ratings if it doesn't yet exist
|
88
|
+
viewing_class = options[:viewing_class] || 'Viewing'
|
89
|
+
viewer_class = options[:viewer_class] || 'User'
|
90
|
+
|
91
|
+
unless Object.const_defined?(viewing_class)
|
92
|
+
Object.class_eval <<-EOV
|
93
|
+
class #{viewing_class} < ActiveRecord::Base
|
94
|
+
belongs_to :viewed, :polymorphic => true
|
95
|
+
belongs_to :viewer, :class_name => #{viewer_class}, :foreign_key => :viewer_id
|
96
|
+
end
|
97
|
+
EOV
|
98
|
+
end
|
99
|
+
|
100
|
+
# Rails < 3
|
101
|
+
# write_inheritable_attribute( :acts_as_viewed_options ,
|
102
|
+
# { :viewing_class => viewing_class,
|
103
|
+
# :viewer_class => viewer_class } )
|
104
|
+
# class_inheritable_reader :acts_as_viewed_options
|
105
|
+
|
106
|
+
# Rails >= 3
|
107
|
+
class_attribute :acts_as_viewed_options
|
108
|
+
self.acts_as_viewed_options = { :viewing_class => viewing_class,
|
109
|
+
:viewer_class => viewer_class }
|
110
|
+
class_eval do
|
111
|
+
has_many :viewings, :as => :viewed, :dependent => :delete_all, :class_name => viewing_class.to_s
|
112
|
+
has_many(:viewers, :through => :viewings, :class_name => viewer_class.to_s)
|
113
|
+
|
114
|
+
before_create :init_viewing_fields
|
115
|
+
end
|
116
|
+
|
117
|
+
# Add to the User (or whatever the viewer is) a has_many viewings
|
118
|
+
viewer_as_class = viewer_class.constantize
|
119
|
+
return if viewer_as_class.instance_methods.include?('find_in_viewings')
|
120
|
+
viewer_as_class.class_eval <<-EOS
|
121
|
+
has_many :viewings, :foreign_key => :viewer_id, :class_name => #{viewing_class.to_s}
|
122
|
+
EOS
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
module ViewMethods
|
127
|
+
|
128
|
+
def self.included(base) #:nodoc:
|
129
|
+
base.extend ClassMethods
|
130
|
+
end
|
131
|
+
|
132
|
+
# Is this object viewed already?
|
133
|
+
def viewed?
|
134
|
+
return (!self.views.nil? && self.views > 0) if attributes.has_key? 'views'
|
135
|
+
!viewings.first.nil?
|
136
|
+
end
|
137
|
+
|
138
|
+
# Get the number of viewings for this object based on the views field,
|
139
|
+
# or with a SQL query if the viewed objects doesn't have the views field
|
140
|
+
def view_count
|
141
|
+
return ("#{self.total_views}(#{self.views})" || 0) if attributes.has_key? 'views'
|
142
|
+
viewings.count
|
143
|
+
end
|
144
|
+
|
145
|
+
# Change views count (total_views and views) if it's existing in object
|
146
|
+
# If options[:only_total] == true count of unique views doesn't change
|
147
|
+
def increase_views_count(options)
|
148
|
+
if attributes.has_key?('views') && attributes.has_key?('total_views')
|
149
|
+
target = self
|
150
|
+
target.views = ( (target.views || 0) + 1 ) if !options[:only_total]
|
151
|
+
target.total_views = ( (target.total_views || 0) + 1)
|
152
|
+
target.save(:validate => false)
|
153
|
+
# target.save_without_validation
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# View the object with or without a viewer - create new or update as needed
|
158
|
+
#
|
159
|
+
# * <tt>ip</tt> - the viewer ip
|
160
|
+
# * <tt>viewer</tt> - an object of the viewer class. Must be valid and with an id to be used. Or nil
|
161
|
+
def view ip, viewer = nil
|
162
|
+
# Sanity checks for the parameters
|
163
|
+
viewing_class = acts_as_viewed_options[:viewing_class].constantize
|
164
|
+
if viewer && !(acts_as_viewed_options[:viewer_class].constantize === viewer)
|
165
|
+
raise ViewedError, "the viewer object must be the one used when defining acts_as_viewed (or a descendent of it). other objects are not acceptable"
|
166
|
+
end
|
167
|
+
|
168
|
+
viewing_class.transaction do
|
169
|
+
if !viewed_by? ip, viewer
|
170
|
+
view = viewing_class.new
|
171
|
+
view.viewer_id = viewer.id if viewer && !viewer.id.nil?
|
172
|
+
view.ip = ip
|
173
|
+
viewings << view
|
174
|
+
view.save
|
175
|
+
increase_views_count(:only_total => false)
|
176
|
+
else
|
177
|
+
increase_views_count(:only_total => true)
|
178
|
+
end
|
179
|
+
true
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# Check if an item was already viewed by the given viewer
|
184
|
+
def viewed_by? ip, viewer = nil
|
185
|
+
if viewer && !viewer.nil? && !(acts_as_viewed_options[:viewer_class].constantize === viewer)
|
186
|
+
raise ViewedError, "the viewer object must be the one used when defining acts_as_viewed (or a descendent of it). other objects are not acceptable"
|
187
|
+
end
|
188
|
+
if viewer && !viewer.id.nil? && !viewer.anonymous?
|
189
|
+
return viewings.where("viewer_id = '#{viewer.id}'").any?
|
190
|
+
else
|
191
|
+
return viewings.where("ip = '#{ip}'").any?
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def init_viewing_fields #:nodoc:
|
198
|
+
if attributes.has_key? 'views'
|
199
|
+
self.views ||= 0
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
module ClassMethods
|
206
|
+
|
207
|
+
# Generate the viewings columns on a table, to be used when creating the table
|
208
|
+
# in a migration. This is the preferred way to do in a migration that creates
|
209
|
+
# new tables as it will make it as part of the table creation, and not generate
|
210
|
+
# ALTER TABLE calls after the fact
|
211
|
+
def generate_viewings_columns table
|
212
|
+
table.column :views, :integer #uniq views
|
213
|
+
table.column :total_views, :integer
|
214
|
+
end
|
215
|
+
|
216
|
+
# Create the needed columns for acts_as_viewed.
|
217
|
+
# To be used during migration, but can also be used in other places.
|
218
|
+
def add_viewings_columns
|
219
|
+
if !self.content_columns.find { |c| 'views' == c.name }
|
220
|
+
self.connection.add_column table_name, :views, :integer, :default => '0'
|
221
|
+
self.connection.add_column table_name, :total_views, :integer, :default => '0'
|
222
|
+
self.reset_column_information
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
# Remove the acts_as_viewed specific columns added with add_viewings_columns
|
227
|
+
# To be used during migration, but can also be used in other places
|
228
|
+
def remove_viewings_columns
|
229
|
+
if self.content_columns.find { |c| 'views' == c.name }
|
230
|
+
self.connection.remove_column table_name, :views
|
231
|
+
self.connection.remove_column table_name, :total_views
|
232
|
+
self.reset_column_information
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
# Create the viewings table
|
237
|
+
# === Options hash:
|
238
|
+
# * <tt>:table_name</tt> - use a table name other than viewings
|
239
|
+
# To be used during migration, but can also be used in other places
|
240
|
+
def create_viewings_table options = {}
|
241
|
+
name = options[:table_name] || :viewings
|
242
|
+
if !self.connection.table_exists?(name)
|
243
|
+
self.connection.create_table(name) do |t|
|
244
|
+
t.column :viewer_id, :integer
|
245
|
+
t.column :viewed_id, :integer
|
246
|
+
t.column :viewed_type, :string
|
247
|
+
t.column :ip, :string, :limit => '24'
|
248
|
+
t.column :created_at, :datetime
|
249
|
+
end
|
250
|
+
|
251
|
+
self.connection.add_index(name, :viewer_id)
|
252
|
+
self.connection.add_index(name, [:viewed_type, :viewed_id])
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Drop the viewings table.
|
257
|
+
# === Options hash:
|
258
|
+
# * <tt>:table_name</tt> - the name of the viewings table, defaults to viewings
|
259
|
+
# To be used during migration, but can also be used in other places
|
260
|
+
def drop_viewings_table options = {}
|
261
|
+
name = options[:table_name] || :viewings
|
262
|
+
if self.connection.table_exists?(name)
|
263
|
+
self.connection.drop_table(name)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
# Find all viewings for a specific viewer.
|
268
|
+
def find_viewed_by viewer
|
269
|
+
viewing_class = acts_as_viewed_options[:viewing_class].constantize
|
270
|
+
if !(acts_as_viewed_options[:viewer_class].constantize === viewer)
|
271
|
+
raise ViewedError, "The viewer object must be the one used when defining acts_as_viewed (or a descendent of it). other objects are not acceptable"
|
272
|
+
end
|
273
|
+
raise ViewedError, "Viewer must be a valid and existing object" if viewer.nil? || viewer.id.nil?
|
274
|
+
raise ViewedError, 'Viewer must be a valid viewer' if !viewing_class.column_names.include? "viewer_id"
|
275
|
+
viewed_class = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
276
|
+
conds = [ 'viewed_type = ? AND viewer_id = ?', viewed_class, viewer.id ]
|
277
|
+
acts_as_viewed_options[:viewing_class].constantize.find(:all, :conditions => conds).collect {|r| r.viewed_type.constantize.find_by_id r.viewed.id }
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
ActiveRecord::Base.send :include, RedmineCrm::ActiveRecord::Acts::Viewed
|
287
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module RedmineCrm
|
4
|
+
module ActsAsVotable #:nodoc:
|
5
|
+
module Votable #:nodoc:
|
6
|
+
|
7
|
+
def votable?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
|
11
|
+
def rcrm_acts_as_votable
|
12
|
+
require 'redmine_crm/votable'
|
13
|
+
include ActsAsVotable::Votable
|
14
|
+
|
15
|
+
class_eval do
|
16
|
+
def self.votable?
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_index(table_name, column_name)
|
24
|
+
return if self.connection.index_exists?(table_name, column_name)
|
25
|
+
|
26
|
+
self.connection.add_index table_name, column_name
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_votable_table options = {}
|
30
|
+
votes_name_table = options[:votes] || :votes
|
31
|
+
|
32
|
+
if !self.connection.table_exists?(votes_name_table)
|
33
|
+
self.connection.create_table(votes_name_table) do |t|
|
34
|
+
t.references :votable, :polymorphic => true
|
35
|
+
t.references :voter, :polymorphic => true
|
36
|
+
|
37
|
+
t.column :vote_flag, :boolean
|
38
|
+
t.column :vote_scope, :string
|
39
|
+
t.column :vote_weight, :integer
|
40
|
+
|
41
|
+
t.timestamps
|
42
|
+
end
|
43
|
+
else #if table exists - check existence of separate columns
|
44
|
+
fields = {
|
45
|
+
:votable_id => :integer,
|
46
|
+
:votable_type => :string,
|
47
|
+
:voter_id => :integer,
|
48
|
+
:voter_type => :string,
|
49
|
+
:vote_flag => :boolean,
|
50
|
+
:vote_scope => :string,
|
51
|
+
:vote_weight => :integer
|
52
|
+
}
|
53
|
+
fields.each do |name, type|
|
54
|
+
if !self.connection.column_exists?(votes_name_table, name)
|
55
|
+
self.connection.add_column(votes_name_table, name, type)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
if self.parent::VERSION::MAJOR < 4
|
62
|
+
create_index votes_name_table, [:votable_id, :votable_type]
|
63
|
+
create_index votes_name_table, [:voter_id, :voter_type]
|
64
|
+
end
|
65
|
+
|
66
|
+
create_index votes_name_table, [:voter_id, :voter_type, :vote_scope]
|
67
|
+
create_index votes_name_table, [:votable_id, :votable_type, :vote_scope]
|
68
|
+
end
|
69
|
+
|
70
|
+
def drop_votable_table options = {}
|
71
|
+
votes_name_table = options[:votes] || :votes
|
72
|
+
if self.connection.table_exists?(votes_name_table)
|
73
|
+
self.connection.drop_table votes_name_table
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RedmineCrm
|
2
|
+
module ActsAsVotable
|
3
|
+
# module Extenders
|
4
|
+
|
5
|
+
module Voter
|
6
|
+
|
7
|
+
def voter?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
|
11
|
+
def rcrm_acts_as_voter(*args)
|
12
|
+
# byebug
|
13
|
+
require 'redmine_crm/voter'
|
14
|
+
include ActsAsVotable::Voter
|
15
|
+
|
16
|
+
class_eval do
|
17
|
+
def self.voter?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
# end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module RedmineCrm
|
2
|
+
class Tag < ActiveRecord::Base
|
3
|
+
has_many :taggings, :dependent => :destroy
|
4
|
+
|
5
|
+
validates_presence_of :name
|
6
|
+
# validates_uniqueness_of :name
|
7
|
+
validates :name, :uniqueness => { :message => " not uniq tag" }
|
8
|
+
validates :name, :presence => true
|
9
|
+
cattr_accessor :destroy_unused
|
10
|
+
self.destroy_unused = false
|
11
|
+
|
12
|
+
attr_accessible :name if defined?(ActiveModel::MassAssignmentSecurity)
|
13
|
+
|
14
|
+
# LIKE is used for cross-database case-insensitivity
|
15
|
+
def self.find_or_create_with_like_by_name(name)
|
16
|
+
# find(:first, :conditions => ["name LIKE ?", name]) || create(:name => name)
|
17
|
+
where("LOWER(name) LIKE LOWER(?)", name).first || create(:name => name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(object)
|
21
|
+
super || (object.is_a?(Tag) && name == object.name)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def count
|
29
|
+
read_attribute(:count).to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
# Calculate the tag counts for all tags.
|
34
|
+
# :start_at - Restrict the tags to those created after a certain time
|
35
|
+
# :end_at - Restrict the tags to those created before a certain time
|
36
|
+
# :conditions - A piece of SQL conditions to add to the query
|
37
|
+
# :limit - The maximum number of tags to return
|
38
|
+
# :order - A piece of SQL to order by. Eg 'count desc' or 'taggings.created_at desc'
|
39
|
+
# :at_least - Exclude tags with a frequency less than the given value
|
40
|
+
# :at_most - Exclude tags with a frequency greater than the given value
|
41
|
+
def counts(options = {})
|
42
|
+
# find(:all, options_for_counts(options))
|
43
|
+
opt = options_for_counts(options)
|
44
|
+
select(opt[:select]).where(opt[:conditions]).joins(opt[:joins]).group(opt[:group])
|
45
|
+
end
|
46
|
+
|
47
|
+
def options_for_counts(options = {})
|
48
|
+
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :joins
|
49
|
+
options = options.dup
|
50
|
+
|
51
|
+
start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
|
52
|
+
end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
|
53
|
+
|
54
|
+
conditions = [
|
55
|
+
(sanitize_sql(options.delete(:conditions)) if options[:conditions]),
|
56
|
+
start_at,
|
57
|
+
end_at
|
58
|
+
].compact
|
59
|
+
|
60
|
+
conditions = conditions.join(' AND ') if conditions.any?
|
61
|
+
|
62
|
+
joins = ["INNER JOIN #{Tagging.table_name} ON #{Tag.table_name}.id = #{Tagging.table_name}.tag_id"]
|
63
|
+
joins << options.delete(:joins) if options[:joins]
|
64
|
+
|
65
|
+
at_least = sanitize_sql(['COUNT(*) >= ?', options.delete(:at_least)]) if options[:at_least]
|
66
|
+
at_most = sanitize_sql(['COUNT(*) <= ?', options.delete(:at_most)]) if options[:at_most]
|
67
|
+
having = "COUNT(*) > 0"
|
68
|
+
having = [having, at_least, at_most].compact.join(' AND ')
|
69
|
+
group_by = "#{Tag.table_name}.id, #{Tag.table_name}.name"
|
70
|
+
# group_by << " AND #{having}" unless having.blank?
|
71
|
+
|
72
|
+
{ :select => "#{Tag.table_name}.id, #{Tag.table_name}.name, COUNT(*) AS count",
|
73
|
+
:joins => joins.join(" "),
|
74
|
+
:conditions => conditions,
|
75
|
+
:group => group_by,
|
76
|
+
:having => having
|
77
|
+
}.update(options)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|