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 validate :check_relations
15 after_save :update_relations
23 module InstanceMethods
27 self.publications.map { |pub| pub.name }
33 def publication_names=(value)
34 @pubnames = value.sort!
40 "JOIN issue_publications ON (issue_publications.publication_id = publications.id)",
42 ["issue_publications.issue_id = ? ", self.id] )
46 current_names = self.publication_names
49 pubs = Publication.find_all_by_name(current_names).map {|i| i.name}
50 missing = current_names.select {|name| not pubs.include?name }
53 errors.add("publications", "Missing publication(s): " + missing.join(', '))
58 old = self.publications
59 current_names = self.publication_names
60 Rails.logger.info('[INFO] Updating relations: old= ' << old.inspect << ' current=' << current_names.inspect)
62 # delete unused relations
63 deleted = old.select { |v| not (current_names.include?(v.name)) }
65 IssuePublication.delete_all(["issue_publications.issue_id = ? AND issue_publications.publication_id = ?", self.id, pub.id])
68 current_names.each do |name|
69 pub = Publication.find_by_name(name)
70 IssuePublication.find_or_create_by_publication_id_and_issue_id(pub.id, self.id)