luis@385: # vendor/plugins/redmine_bibliography/app/models/publication.rb luis@385: luis@328: class Publication < ActiveRecord::Base luis@428: unloadable luis@1068: luis@571: has_many :authorships, :dependent => :destroy, :order => "auth_order ASC" luis@447: has_many :authors, :through => :authorships, :uniq => true luis@1068: luis@560: has_one :bibtex_entry, :dependent => :destroy luis@376: luis@376: validates_presence_of :title luis@686: validates_length_of :authorships, :minimum => 1, :message => l("error_no_authors") luis@1287: validates_associated :bibtex_entry, :authorships luis@445: luis@445: accepts_nested_attributes_for :authorships luis@446: accepts_nested_attributes_for :authors, :allow_destroy => true luis@454: accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true luis@1068: luis@464: has_and_belongs_to_many :projects, :uniq => true luis@1068: chris@567: before_save :set_initial_author_order chris@653: luis@1212: scope :visible, lambda {|*args| { :include => :projects, luis@1212: :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } } luis@1087: luis@1080: acts_as_activity_provider :type => 'publication', luis@1080: :timestamp => "#{Publication.table_name}.created_at", luis@1087: :find_options => { luis@1087: :include => :projects, luis@1087: :conditions => "#{Project.table_name}.id = projects_publications.project_id" luis@1087: } luis@1080: luis@1080: acts_as_event :title => Proc.new {|o| o.title }, luis@1080: :datetime => :created_at, luis@1080: :type => 'publications', luis@1087: :author => nil, luis@1080: #todo - need too move the cache from the helper to the model luis@1080: :description => Proc.new {|o| o.print_entry(:ieee)}, luis@1087: :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }} luis@1080: luis@1080: chris@653: # Ensure error message uses proper text instead of chris@653: # bibtex_entry.entry_type (#268). There has to be a better way to chris@653: # do this! luis@1287: def self.human_attribute_name(k, *args) chris@653: if k == 'bibtex_entry.entry_type' chris@653: l(:field_entry_type) chris@653: else chris@653: super chris@653: end chris@653: end chris@653: luis@1068: def notify_authors_publication_added(project) luis@643: self.authors.each do |author| luis@651: Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." } luis@1401: Mailer.publication_added(author.user, self, project).deliver unless author.user.nil? luis@643: end luis@666: end luis@1068: luis@1068: def notify_authors_publication_updated(project) luis@666: self.authors.each do |author| luis@666: Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." } luis@1401: Mailer.publication_updated(author.user, self, project).deliver unless author.user.nil? luis@666: end luis@643: end luis@1068: luis@1068: luis@556: def set_initial_author_order luis@556: authorships = self.authorships luis@1068: luis@556: logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." } luis@1068: luis@556: authorships.each_with_index do |authorship, index| luis@556: if authorship.auth_order.nil? luis@556: authorship.auth_order = index luis@556: end luis@1068: end luis@556: end luis@1068: luis@946: def print_bibtex_author_names luis@1068: # this authors are correctly sorted because the authorships model luis@946: # already outputs the author names ASC by auth_order luis@946: self.authorships.map{|a| a.name_on_paper}.join(' and ') luis@1068: end luis@1068: luis@946: def print_entry(style) luis@946: bib = BibTeX::Entry.new luis@946: luis@946: bib.author = self.print_bibtex_author_names luis@946: bib.title = self.title luis@946: luis@1068: self.bibtex_entry.attributes.keys.sort.each do |key| luis@946: value = self.bibtex_entry.attributes[key].to_s luis@946: next if key == 'id' or key == 'publication_id' or value == "" luis@946: luis@1068: if key == "entry_type" luis@1028: bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name luis@946: else luis@946: bib[key.to_sym] = value luis@1068: end luis@946: end luis@1068: luis@946: if style == :ieee luis@1312: CiteProc.process(bib.to_citeproc, :style => :ieee, :format => :html) luis@1068: else luis@1023: bibtex = bib.to_s :include => :meta_content luis@1023: bibtex.strip! luis@1068: end luis@946: end luis@328: end