annotate vendor/plugins/redmine_bibliography/app/models/publication.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

Close obsolete branch bibplugin_db_migrations
author Chris Cannam
date Fri, 30 Nov 2012 14:40:50 +0000
parents 74407a04925c
children 6d85e9ea961c
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
luis@1087 22 named_scope :visible, lambda {|*args| { :include => :projects,
luis@1087 23 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } }
luis@1087 24
luis@1080 25 acts_as_activity_provider :type => 'publication',
luis@1080 26 :timestamp => "#{Publication.table_name}.created_at",
luis@1087 27 :find_options => {
luis@1087 28 :include => :projects,
luis@1087 29 :conditions => "#{Project.table_name}.id = projects_publications.project_id"
luis@1087 30 }
luis@1080 31
luis@1080 32 acts_as_event :title => Proc.new {|o| o.title },
luis@1080 33 :datetime => :created_at,
luis@1080 34 :type => 'publications',
luis@1087 35 :author => nil,
luis@1080 36 #todo - need too move the cache from the helper to the model
luis@1080 37 :description => Proc.new {|o| o.print_entry(:ieee)},
luis@1087 38 :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }}
luis@1080 39
luis@1080 40
chris@653 41 # Ensure error message uses proper text instead of
chris@653 42 # bibtex_entry.entry_type (#268). There has to be a better way to
chris@653 43 # do this!
chris@653 44 def self.human_attribute_name(k)
chris@653 45 if k == 'bibtex_entry.entry_type'
chris@653 46 l(:field_entry_type)
chris@653 47 else
chris@653 48 super
chris@653 49 end
chris@653 50 end
chris@653 51
luis@1068 52 def notify_authors_publication_added(project)
luis@643 53 self.authors.each do |author|
luis@651 54 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
luis@666 55 Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil?
luis@643 56 end
luis@666 57 end
luis@1068 58
luis@1068 59 def notify_authors_publication_updated(project)
luis@666 60 self.authors.each do |author|
luis@666 61 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
luis@666 62 Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil?
luis@666 63 end
luis@643 64 end
luis@1068 65
luis@1068 66
luis@556 67 def set_initial_author_order
luis@556 68 authorships = self.authorships
luis@1068 69
luis@556 70 logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
luis@1068 71
luis@556 72 authorships.each_with_index do |authorship, index|
luis@556 73 if authorship.auth_order.nil?
luis@556 74 authorship.auth_order = index
luis@556 75 end
luis@1068 76 end
luis@556 77 end
luis@1068 78
luis@946 79 def print_bibtex_author_names
luis@1068 80 # this authors are correctly sorted because the authorships model
luis@946 81 # already outputs the author names ASC by auth_order
luis@946 82 self.authorships.map{|a| a.name_on_paper}.join(' and ')
luis@1068 83 end
luis@1068 84
luis@946 85 def print_entry(style)
luis@946 86 bib = BibTeX::Entry.new
luis@946 87
luis@946 88 bib.author = self.print_bibtex_author_names
luis@946 89 bib.title = self.title
luis@946 90
luis@1068 91 self.bibtex_entry.attributes.keys.sort.each do |key|
luis@946 92 value = self.bibtex_entry.attributes[key].to_s
luis@946 93 next if key == 'id' or key == 'publication_id' or value == ""
luis@946 94
luis@1068 95 if key == "entry_type"
luis@1028 96 bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
luis@946 97 else
luis@946 98 bib[key.to_sym] = value
luis@1068 99 end
luis@946 100 end
luis@1068 101
luis@946 102 if style == :ieee
luis@1068 103 CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html
luis@1068 104 else
luis@1023 105 bibtex = bib.to_s :include => :meta_content
luis@1023 106 bibtex.strip!
luis@1023 107 logger.error { bibtex }
luis@1068 108 end
luis@946 109 end
luis@328 110 end