To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / plugins / redmine_tags / lib / redmine_tags / patches / project_patch.rb @ 1249:774beb9b79da

History | View | Annotate | Download (2 KB)

1 727:e3e958595e07 luis
# C4DM
2
3
require_dependency 'project'
4
5
module RedmineTags
6
  module Patches
7
    module ProjectPatch
8 729:291c4d7dc0b6 luis
      def self.included(base) # :nodoc:
9 727:e3e958595e07 luis
        base.extend(ClassMethods)
10 730:4a9bb3b8d5cd luis
        base.send(:include, InstanceMethods)
11 727:e3e958595e07 luis
12
        base.class_eval do
13
          unloadable
14
          acts_as_taggable
15 730:4a9bb3b8d5cd luis
16 1249:774beb9b79da luis
          Project.safe_attributes 'tag_list'
17
18
          # TODO: review need for this callback (uneeded on update) ~lf.03042013
19
          after_create :save_tags
20 727:e3e958595e07 luis
        end
21
      end
22
23 730:4a9bb3b8d5cd luis
      module InstanceMethods
24 1249:774beb9b79da luis
        def save_tags
25
          self.tags = Tag.transaction do
26
            @tag_list.each(&:save)
27
          end
28
        end
29 730:4a9bb3b8d5cd luis
      end
30 729:291c4d7dc0b6 luis
31 727:e3e958595e07 luis
      module ClassMethods
32 1244:a3ed5c4d90f0 luis
        TAGGING_IDS_LIMIT_SQL = <<-SQL
33
            tag_id IN (
34
                SELECT #{ActsAsTaggableOn::Tagging.table_name}.tag_id
35
                FROM #{ActsAsTaggableOn::Tagging.table_name}
36
                WHERE #{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN (?)
37
            )
38
        SQL
39
40 739:b7ac21913927 luis
        def search_by_question(question)
41
          if question.length > 1
42
            search(RedmineProjectFiltering.calculate_tokens(question), nil, :all_words => true).first.sort_by(&:lft)
43
          else
44
            all(:order => 'lft')
45
          end
46
        end
47
48 1244:a3ed5c4d90f0 luis
        # Returns available project tags
49
        # Does not return tags from private projects
50
        # === Parameters
51
        # * <i>options</i> = (optional) Options hash of
52
        #   * name_like - String. Substring to filter found tags.
53
        def available_tags( options = {} )
54
          ids_scope = Project.visible
55 739:b7ac21913927 luis
56 1244:a3ed5c4d90f0 luis
          conditions = [""]
57 746:2ced57750157 luis
58 1244:a3ed5c4d90f0 luis
          # limit to the tags matching given %name_like%
59
          if options[:name_like]
60
            conditions[0] << "#{ActsAsTaggableOn::Tag.table_name}.name LIKE ? AND "
61
            conditions << "%#{options[:name_like].downcase}%"
62 746:2ced57750157 luis
          end
63
64 1244:a3ed5c4d90f0 luis
          conditions[0] << TAGGING_IDS_LIMIT_SQL
65
          conditions << ids_scope.map{ |issue| issue.id }.push(-1)
66 746:2ced57750157 luis
67 1244:a3ed5c4d90f0 luis
          self.all_tag_counts(:conditions => conditions)
68 727:e3e958595e07 luis
        end
69
      end
70
    end
71
  end
72
end