forked from scambra/railscollab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.rb
106 lines (83 loc) · 3.4 KB
/
tag.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#==
# RailsCollab
# Copyright (C) 2007 - 2008 James S Urquhart
# Portions Copyright (C) René Scheibe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
class Tag < ActiveRecord::Base
include ActionController::UrlWriter
belongs_to :project
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_id'
belongs_to :rel_object, :polymorphic => true
acts_as_ferret :fields => [:tag, :project_id, :is_private], :store_class_name => true
before_create :process_params
def process_params
end
def objects
return Tag.find_objects(self.name)
end
def object_name
self.tag
end
def object_url(host = nil)
url_for :only_path => host.nil?, :host => host, :controller => 'projects', :action => 'tags', :id => self.tag, :active_project => self.project_id
end
def self.priv_scope(include_private)
if include_private
yield
else
with_scope :find => { :conditions => ['is_private = ?', false] } do
yield
end
end
end
def self.find_objects(tag_name, project, is_public)
tag_conditions = is_public ?
['project_id = ? AND tag = ? AND is_private = ?', project.id, tag_name, false] :
['project_id = ? AND tag = ?', project.id, tag_name]
Tag.all(:conditions => tag_conditions).collect{ |tag| tag.rel_object }
end
def self.clear_by_object(object)
Tag.delete_all(['project_id = ? AND rel_object_type = ? AND rel_object_id = ?', object.project_id, object.class.to_s, object.id])
end
def self.set_to_object(object, taglist, force_user=0)
self.clear_by_object(object)
set_private = object.is_private.nil? ? false : object.is_private
set_user = force_user == 0 ? (object.updated_by.nil? ? object.created_by : object.updated_by) : force_user
Tag.transaction do
taglist.each do |tag_name|
Tag.create(:tag => tag_name.strip, :project => object.project, :rel_object => object, :created_by => set_user, :is_private => set_private)
end
end
end
def self.list_by_object(object)
tags = Tag.all(:conditions => ['rel_object_type = ? AND rel_object_id = ?', object.class.to_s, object.id])
tags.collect{ |tag| tag.tag }
end
def self.list_by_project(project, is_public, to_text=true)
tag_conditions = is_public ?
['project_id = ? AND is_private = ?', project.id, false] :
['project_id = ?', project.id]
tags = Tag.all(:group => 'tag', :conditions => tag_conditions, :order => 'tag', :select => 'tag')
to_text ? tags.collect{ |tag| tag.tag } : tags
end
def self.count_by(tag_name, project, is_public)
tag_conditions = is_public ?
['project_id = ? AND is_private = ? AND tag = ?', project.id, false, tag_name] :
['project_id = ? AND tag = ?', project.id, tag_name]
tags = Tag.all(:conditions => tag_conditions, :select => 'id')
tags.length
end
end