Don't erase old publcations, so that the relations don't disappear.
[redakcja_redmine.git] / lib / redmine_publications / change_patch.rb
1 module RedminePublications
2   # Patches Redmine's Issues dynamically. Adds a +after_save+ filter.
3
4   module ChangePatch
5     def self.included(base) # :nodoc:
6       base.extend(ClassMethods)
7
8       base.send(:include, InstanceMethods)
9
10       # Same as typing in the class
11       base.class_eval do
12         unloadable # Send unloadable so it will not be unloaded in development
13         after_save :update_publication
14       end
15
16     end
17
18     module ClassMethods
19     end
20
21     module InstanceMethods
22
23       def update_publication
24         if (self.action == 'A') and (self.changeset.repository.project_id == Setting.plugin_redmine_publications[:project].to_i)
25           regexp = Regexp.new(Setting.plugin_redmine_publications[:pattern])
26           match = self.path.match(regexp)
27           if match
28             Rails.logger.info('[INFO] Adding publication: "' << match[1])
29             Publication.find_or_create_by_name(:name => match[1],
30               :source_file => self.path, :repository_id => self.changeset.repository.id )
31           end
32         end      
33       end
34       
35     end
36     
37   end
38
39
40 end