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 / plugins / redmine_bibliography / app / models / publication.rb @ 1398:92d854be33d5

History | View | Annotate | Download (3.68 KB)

1
# vendor/plugins/redmine_bibliography/app/models/publication.rb
2

    
3
class Publication < ActiveRecord::Base
4
  unloadable
5

    
6
  has_many :authorships, :dependent => :destroy, :order => "auth_order ASC"
7
  has_many :authors, :through => :authorships, :uniq => true
8

    
9
  has_one :bibtex_entry, :dependent => :destroy
10

    
11
  validates_presence_of :title
12
  validates_length_of :authorships, :minimum => 1, :message => l("error_no_authors")
13
  validates_associated :bibtex_entry, :authorships
14

    
15
  accepts_nested_attributes_for :authorships
16
  accepts_nested_attributes_for :authors, :allow_destroy => true
17
  accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
18

    
19
  has_and_belongs_to_many :projects, :uniq => true
20

    
21
  before_save :set_initial_author_order
22

    
23
  scope :visible, lambda {|*args| { :include => :projects,
24
                                    :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } }
25

    
26
  acts_as_activity_provider :type => 'publication',
27
                            :timestamp => "#{Publication.table_name}.created_at",
28
                            :find_options => {
29
                              :include => :projects,
30
                              :conditions => "#{Project.table_name}.id = projects_publications.project_id"
31
                            }
32

    
33
  acts_as_event :title => Proc.new {|o| o.title },
34
                :datetime => :created_at,
35
                :type => 'publications',
36
                :author => nil,
37
                #todo - need too move the cache from the helper to the model
38
                :description => Proc.new {|o| o.print_entry(:ieee)},
39
                :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }}
40

    
41

    
42
  # Ensure error message uses proper text instead of
43
  # bibtex_entry.entry_type (#268).  There has to be a better way to
44
  # do this!
45
  def self.human_attribute_name(k, *args)
46
    if k == 'bibtex_entry.entry_type'
47
      l(:field_entry_type)
48
    else
49
      super
50
    end
51
  end
52

    
53
  def notify_authors_publication_added(project)
54
    self.authors.each do |author|
55
      Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
56
      Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil?
57
    end
58
  end
59

    
60
  def notify_authors_publication_updated(project)
61
    self.authors.each do |author|
62
      Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
63
      Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil?
64
    end
65
  end
66

    
67

    
68
  def set_initial_author_order
69
    authorships = self.authorships
70

    
71
    logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
72

    
73
    authorships.each_with_index do |authorship, index|
74
      if authorship.auth_order.nil?
75
         authorship.auth_order = index
76
      end
77
    end
78
  end
79

    
80
  def print_bibtex_author_names
81
    # this authors are correctly sorted because the authorships model
82
    # already outputs the author names ASC by auth_order
83
    self.authorships.map{|a| a.name_on_paper}.join(' and ')
84
  end
85

    
86
  def print_entry(style)
87
    bib = BibTeX::Entry.new
88

    
89
    bib.author = self.print_bibtex_author_names
90
    bib.title = self.title
91

    
92
    self.bibtex_entry.attributes.keys.sort.each do |key|
93
      value = self.bibtex_entry.attributes[key].to_s
94
      next if key == 'id' or key == 'publication_id' or value == ""
95

    
96
      if key == "entry_type"
97
        bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
98
      else
99
        bib[key.to_sym] = value
100
      end
101
    end
102

    
103
    if style == :ieee
104
      CiteProc.process(bib.to_citeproc, :style => :ieee, :format => :html)
105
    else
106
      bibtex = bib.to_s :include => :meta_content
107
      bibtex.strip!
108
    end
109
  end
110
end