diff vendor/plugins/redmine_bibliography/app/models/publication.rb @ 1081:b56a4c5afa35 bug_531

Merge from branch cannam
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 22 Nov 2012 17:26:15 +0000
parents e11d8d13ebc5
children 5bd8c86cfa6a
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/models/publication.rb	Tue Nov 13 10:12:31 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/models/publication.rb	Thu Nov 22 17:26:15 2012 +0000
@@ -2,10 +2,10 @@
 
 class Publication < ActiveRecord::Base
   unloadable
-  
+
   has_many :authorships, :dependent => :destroy, :order => "auth_order ASC"
   has_many :authors, :through => :authorships, :uniq => true
-  
+
   has_one :bibtex_entry, :dependent => :destroy
 
   validates_presence_of :title
@@ -14,9 +14,9 @@
   accepts_nested_attributes_for :authorships
   accepts_nested_attributes_for :authors, :allow_destroy => true
   accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
-  
+
   has_and_belongs_to_many :projects, :uniq => true
-  
+
   before_save :set_initial_author_order
 
   # Ensure error message uses proper text instead of
@@ -30,34 +30,62 @@
     end
   end
 
-  def notify_authors_publication_added(project)  
+  def notify_authors_publication_added(project)
     self.authors.each do |author|
       Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
       Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil?
     end
   end
-  
-  def notify_authors_publication_updated(project)  
+
+  def notify_authors_publication_updated(project)
     self.authors.each do |author|
       Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
       Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil?
     end
   end
-  
-  
+
+
   def set_initial_author_order
     authorships = self.authorships
-    
+
     logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
-    
+
     authorships.each_with_index do |authorship, index|
       if authorship.auth_order.nil?
          authorship.auth_order = index
       end
-    end    
+    end
   end
-  
-  
-  
-  
+
+  def print_bibtex_author_names
+    # this authors are correctly sorted because the authorships model
+    # already outputs the author names ASC by auth_order
+    self.authorships.map{|a| a.name_on_paper}.join(' and ')
+  end
+
+  def print_entry(style)
+    bib = BibTeX::Entry.new
+
+    bib.author = self.print_bibtex_author_names
+    bib.title = self.title
+
+    self.bibtex_entry.attributes.keys.sort.each do |key|
+      value = self.bibtex_entry.attributes[key].to_s
+      next if key == 'id' or key == 'publication_id' or value == ""
+
+      if key == "entry_type"
+        bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
+      else
+        bib[key.to_sym] = value
+      end
+    end
+
+    if style == :ieee
+      CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html
+    else
+      bibtex = bib.to_s :include => :meta_content
+      bibtex.strip!
+      logger.error { bibtex }
+    end
+  end
 end