annotate plugins/redmine_bibliography/app/models/publication.rb @ 1142:7c3de6c7b7f5 redmine-2.2-integration

Syntax fixes
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 10 Jan 2013 15:25:28 +0000
parents b4b72f1eb644
children b619a667a8e8
rev   line source
luis@385 1 # vendor/plugins/redmine_bibliography/app/models/publication.rb
luis@385 2
luis@328 3 class Publication < ActiveRecord::Base
luis@428 4 unloadable
luis@1068 5
luis@571 6 has_many :authorships, :dependent => :destroy, :order => "auth_order ASC"
luis@447 7 has_many :authors, :through => :authorships, :uniq => true
luis@1068 8
luis@560 9 has_one :bibtex_entry, :dependent => :destroy
luis@376 10
luis@376 11 validates_presence_of :title
luis@686 12 validates_length_of :authorships, :minimum => 1, :message => l("error_no_authors")
luis@445 13
luis@445 14 accepts_nested_attributes_for :authorships
luis@446 15 accepts_nested_attributes_for :authors, :allow_destroy => true
luis@454 16 accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
luis@1068 17
luis@464 18 has_and_belongs_to_many :projects, :uniq => true
luis@1068 19
chris@567 20 before_save :set_initial_author_order
chris@653 21
chris@653 22 # Ensure error message uses proper text instead of
chris@653 23 # bibtex_entry.entry_type (#268). There has to be a better way to
chris@653 24 # do this!
chris@653 25 def self.human_attribute_name(k)
chris@653 26 if k == 'bibtex_entry.entry_type'
chris@653 27 l(:field_entry_type)
chris@653 28 else
chris@653 29 super
chris@653 30 end
chris@653 31 end
chris@653 32
luis@1068 33 def notify_authors_publication_added(project)
luis@643 34 self.authors.each do |author|
luis@651 35 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
luis@666 36 Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil?
luis@643 37 end
luis@666 38 end
luis@1068 39
luis@1068 40 def notify_authors_publication_updated(project)
luis@666 41 self.authors.each do |author|
luis@666 42 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
luis@666 43 Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil?
luis@666 44 end
luis@643 45 end
luis@1068 46
luis@1068 47
luis@556 48 def set_initial_author_order
luis@556 49 authorships = self.authorships
luis@1068 50
luis@556 51 logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
luis@1068 52
luis@556 53 authorships.each_with_index do |authorship, index|
luis@556 54 if authorship.auth_order.nil?
luis@556 55 authorship.auth_order = index
luis@556 56 end
luis@1068 57 end
luis@556 58 end
luis@1068 59
luis@946 60 def print_bibtex_author_names
luis@1068 61 # this authors are correctly sorted because the authorships model
luis@946 62 # already outputs the author names ASC by auth_order
luis@946 63 self.authorships.map{|a| a.name_on_paper}.join(' and ')
luis@1068 64 end
luis@1068 65
luis@946 66 def print_entry(style)
luis@946 67 bib = BibTeX::Entry.new
luis@946 68
luis@946 69 bib.author = self.print_bibtex_author_names
luis@946 70 bib.title = self.title
luis@946 71
luis@1068 72 self.bibtex_entry.attributes.keys.sort.each do |key|
luis@946 73 value = self.bibtex_entry.attributes[key].to_s
luis@946 74 next if key == 'id' or key == 'publication_id' or value == ""
luis@946 75
luis@1068 76 if key == "entry_type"
luis@1028 77 bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
luis@946 78 else
luis@946 79 bib[key.to_sym] = value
luis@1068 80 end
luis@946 81 end
luis@1068 82
luis@946 83 if style == :ieee
luis@1068 84 CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html
luis@1068 85 else
luis@1023 86 bibtex = bib.to_s :include => :meta_content
luis@1023 87 bibtex.strip!
luis@1023 88 logger.error { bibtex }
luis@1068 89 end
luis@946 90 end
luis@328 91 end