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 @ 1068:e11d8d13ebc5

History | View | Annotate | Download (2.69 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 1068:e11d8d13ebc5 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 1068:e11d8d13ebc5 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 1068:e11d8d13ebc5 luis
18 464:fbdfec975bfa luis
  has_and_belongs_to_many :projects, :uniq => true
19 1068:e11d8d13ebc5 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 1068:e11d8d13ebc5 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 1068:e11d8d13ebc5 luis
40
  def notify_authors_publication_updated(project)
41 666:865d079e5fa0 luis
    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 1068:e11d8d13ebc5 luis
47
48 556:ca9e8e562ea7 luis
  def set_initial_author_order
49
    authorships = self.authorships
50 1068:e11d8d13ebc5 luis
51 556:ca9e8e562ea7 luis
    logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
52 1068:e11d8d13ebc5 luis
53 556:ca9e8e562ea7 luis
    authorships.each_with_index do |authorship, index|
54
      if authorship.auth_order.nil?
55
         authorship.auth_order = index
56
      end
57 1068:e11d8d13ebc5 luis
    end
58 556:ca9e8e562ea7 luis
  end
59 1068:e11d8d13ebc5 luis
60 946:a0c9cc95bcf3 luis
  def print_bibtex_author_names
61 1068:e11d8d13ebc5 luis
    # this authors are correctly sorted because the authorships model
62 946:a0c9cc95bcf3 luis
    # already outputs the author names ASC by auth_order
63
    self.authorships.map{|a| a.name_on_paper}.join(' and ')
64 1068:e11d8d13ebc5 luis
  end
65
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 1068:e11d8d13ebc5 luis
    self.bibtex_entry.attributes.keys.sort.each do |key|
73 946:a0c9cc95bcf3 luis
      value = self.bibtex_entry.attributes[key].to_s
74
      next if key == 'id' or key == 'publication_id' or value == ""
75
76 1068:e11d8d13ebc5 luis
      if key == "entry_type"
77 1028:b8ae7b3af25a luis
        bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
78 946:a0c9cc95bcf3 luis
      else
79
        bib[key.to_sym] = value
80 1068:e11d8d13ebc5 luis
      end
81 946:a0c9cc95bcf3 luis
    end
82 1068:e11d8d13ebc5 luis
83 946:a0c9cc95bcf3 luis
    if style == :ieee
84 1068:e11d8d13ebc5 luis
      CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html
85
    else
86 1023:3d924264419a luis
      bibtex = bib.to_s :include => :meta_content
87
      bibtex.strip!
88
      logger.error { bibtex }
89 1068:e11d8d13ebc5 luis
    end
90 946:a0c9cc95bcf3 luis
  end
91 328:aed18b463206 luis
end