Don't erase old publcations, so that the relations don't disappear.
[redakcja_redmine.git] / app / controllers / publications_controller.rb
1 class PublicationsController < ApplicationController
2   unloadable
3
4   #  before_filter :authorize, :only => [:issues]
5
6   def index
7         @publications = Publication.all
8     respond_to do |format|
9       format.html
10       format.xml { render :xml => @publications }
11       format.json { render :json => @publications }
12     end
13   end
14
15   def refresh
16     regexp = Regexp.new(Setting.plugin_redmine_publications[:pattern])
17     repo = Repository.find(:first, :conditions => ['project_id = ?', Setting.plugin_redmine_publications[:project]] )
18
19     Rails.logger.info('[INFO] Importing changes from ' << repo.url)
20     Rails.logger.info('[INFO] Change list: ' << repo.changes.find(:all).inspect )
21
22     @repo_status = []
23     repo.changes.find(:all).each do |change|
24         Rails.logger.info('[INFO] Importing change ' << change.path)
25         match = change.path.match(regexp)
26         if match
27             Publication.find_or_create_by_name(:name => match[1],
28                 :source_file => change.path, :repository_id => repo.id)
29               @repo_status += [{:path => change.path, :match => match[1], :matched => true}]
30         else
31               @repo_status += [{:path => change.path, :match => nil, :matched => false}]
32         end
33     end
34         
35     respond_to do |format|
36         format.html
37         format.xml { render :xml => @repo_status}
38         format.json { render :json => @repo_status }
39     end
40   end
41
42   def issues
43     @publication = Publication.find_by_name(params[:pub])
44
45     joins = "JOIN issue_publications ON (issues.id = issue_publications.issue_id)"
46     conditions = ['issue_publications.publication_id = ? ', @publication.id ]
47     @issues = Issue.all(:joins => joins, :conditions =>  conditions)
48
49     respond_to do |fmt| 
50       fmt.json { render :json => @issues, :callback => params[:callback] }
51     end
52   end
53
54   private
55
56   def find_project
57     @project = Project.find(params[:project_id])
58   end
59
60 end