To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / vendor / plugins / redmine_bibliography / app / models / publication.rb @ 1029:beea10a56132

History | View | Annotate | Download (2.77 KB)

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