1 module RedminePublications
2 # Patches Redmine's Issues dynamically. Adds a +after_save+ filter.
5 def self.included(base) # :nodoc:
6 base.extend(ClassMethods)
8 base.send(:include, InstanceMethods)
10 # Same as typing in the class
12 unloadable # Send unloadable so it will not be unloaded in development
14 after_save :update_relations
15 # after_destroy :check_relations
17 # Add visible to Redmine 0.8.x
18 unless respond_to?(:visible)
19 named_scope :visible, lambda {|*args| { :include => :project,
20 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
29 module InstanceMethods
32 @source_files = self.lookup_source_files.map { |pub| pub.source_file }
37 def source_files=(value)
41 def lookup_source_files
44 "JOIN issue_publications ON (issue_publications.publication_id = publications.id)",
46 [" issue_publications.issue_id = ? ", self.id] )
51 current_assocs = self.lookup_source_files
52 new_assocs_names = self.source_files.split(' ')
54 # delete unused relations
55 deleted = current_assocs.select { |v| not (new_assocs_names.include?(v.source_file)) }
56 deleted.each { |pub| IssuePublication.delete_all(
57 :contitions => ["issue_publications.issue_id = ? AND issue_publicatons.publication_id = ?",
60 new_assocs_names.each do |name|
61 pub = Publication.find_or_create_by_source_file(name)
62 IssuePublication.find_or_create_by_publication_id_and_issue_id(pub.id, self.id)