forked from Restream/redmine_tagging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagging_query_patch.rb
executable file
·73 lines (57 loc) · 2.26 KB
/
tagging_query_patch.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
require_dependency 'query'
module TaggingPlugin
module QueryPatch
def self.included(base) # :nodoc:
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
# Same as typing in the class
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
alias_method_chain :available_filters, :tags
alias_method_chain :sql_for_field, :tags
end
end
module InstanceMethods
def available_filters_with_tags
@available_filters = available_filters_without_tags
if project.nil?
tags = ActsAsTaggableOn::Tag.find(:all, :conditions => "id in (select tag_id from taggings where taggable_type = 'Issue')")
else
tags = ActsAsTaggableOn::Tag.find(:all,
:conditions => ["id in (select tag_id from taggings where taggable_type = 'Issue' and context = ?)", project.identifier.gsub('-', '_')])
end
tags = tags.collect {|tag| [tag.name.gsub(/^#/, ''), tag.name]}
tag_filter = {
"tags" => {
:type => :list,
:values => tags,
:order => 20
}
}
return @available_filters.merge(tag_filter)
end
def sql_for_field_with_tags(field, operator, v, db_table, db_field, is_custom_filter=false)
if field == "tags"
selected_values = values_for(field)
sql = selected_values.collect{|val| "'#{val.downcase.gsub('\'', '')}'"}.join(',')
sql = "(#{Issue.table_name}.id in (select taggable_id from taggings join tags on tags.id = taggings.tag_id where taggable_type='Issue' and tags.name in (#{sql})))"
sql = "(not #{sql})" if operator == '!'
return sql
else
return sql_for_field_without_tags(field, operator, v, db_table, db_field, is_custom_filter)
end
end
end
module ClassMethods
# Setter for +available_columns+ that isn't provided by the core.
def available_columns=(v)
self.available_columns = (v)
end
# Method to add a column to the +available_columns+ that isn't provided by the core.
def add_available_column(column)
self.available_columns << (column)
end
end
end
end
Query.send(:include, TaggingPlugin::QueryPatch) unless Query.included_modules.include? TaggingPlugin::QueryPatch