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 @ 1286:d0d6bbe9f2e0

History | View | Annotate | Download (3.65 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

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

    
18
  has_and_belongs_to_many :projects, :uniq => true
19

    
20
  before_save :set_initial_author_order
21

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

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

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

    
40

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

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

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

    
66

    
67
  def set_initial_author_order
68
    authorships = self.authorships
69

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

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

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

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

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

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

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

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