41ea9596ea0bee2b400212dc72c32e2b49fd561a
[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'
25           regexp = Regexp.new(Setting.plugin_redmine_publications[:pattern])
26           match = self.path.match(regexp)
27           Rails.logger.info('[INFO] Adding publication: "' << match[1])
28           Publication.find_or_create_by_name(:name => match[1],
29             :source_file => self.path, :repository_id => self.changeset.repository.id )
30         end      
31       end
32       
33     end
34     
35   end
36
37
38 end