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
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
33 self.publications.map { |pub| pub.name }
39 def publication_names=(value)
40 @pubnames = value.sort!
46 "JOIN issue_publications ON (issue_publications.publication_id = publications.id)",
48 ["issue_publications.issue_id = ? ", self.id] )
52 current_names = self.publication_names
55 pubs = Publication.find_all_by_name(current_names).map {|i| i.name}
56 missing = current_names.select {|name| not pubs.include?name }
59 errors.add("publications", "Missing publication(s): " + missing.join(', '))
65 old = self.publications
66 current_names = self.publication_names
68 # delete unused relations
69 deleted = old.select { |v| not (current_names.include?(v.name)) }
71 IssuePublication.delete_all(["issue_publications.issue_id = ? AND issue_publications.publication_id = ?", self.id, pub.id])
74 current_names.each do |name|
75 pub = Publication.find_by_name(name)
76 IssuePublication.find_or_create_by_publication_id_and_issue_id(pub.id, self.id)