luis@385
|
1 # vendor/plugins/redmine_bibliography/app/models/publication.rb
|
luis@385
|
2
|
luis@328
|
3 class Publication < ActiveRecord::Base
|
luis@428
|
4 unloadable
|
luis@1068
|
5
|
luis@571
|
6 has_many :authorships, :dependent => :destroy, :order => "auth_order ASC"
|
luis@447
|
7 has_many :authors, :through => :authorships, :uniq => true
|
luis@1068
|
8
|
luis@560
|
9 has_one :bibtex_entry, :dependent => :destroy
|
luis@376
|
10
|
luis@376
|
11 validates_presence_of :title
|
luis@686
|
12 validates_length_of :authorships, :minimum => 1, :message => l("error_no_authors")
|
luis@1287
|
13 validates_associated :bibtex_entry, :authorships
|
luis@445
|
14
|
luis@445
|
15 accepts_nested_attributes_for :authorships
|
luis@446
|
16 accepts_nested_attributes_for :authors, :allow_destroy => true
|
luis@454
|
17 accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
|
luis@1068
|
18
|
luis@464
|
19 has_and_belongs_to_many :projects, :uniq => true
|
luis@1068
|
20
|
chris@567
|
21 before_save :set_initial_author_order
|
chris@653
|
22
|
luis@1212
|
23 scope :visible, lambda {|*args| { :include => :projects,
|
luis@1212
|
24 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } }
|
luis@1087
|
25
|
luis@1080
|
26 acts_as_activity_provider :type => 'publication',
|
luis@1080
|
27 :timestamp => "#{Publication.table_name}.created_at",
|
luis@1087
|
28 :find_options => {
|
luis@1087
|
29 :include => :projects,
|
luis@1087
|
30 :conditions => "#{Project.table_name}.id = projects_publications.project_id"
|
luis@1087
|
31 }
|
luis@1080
|
32
|
luis@1080
|
33 acts_as_event :title => Proc.new {|o| o.title },
|
luis@1080
|
34 :datetime => :created_at,
|
luis@1080
|
35 :type => 'publications',
|
luis@1087
|
36 :author => nil,
|
luis@1080
|
37 #todo - need too move the cache from the helper to the model
|
luis@1080
|
38 :description => Proc.new {|o| o.print_entry(:ieee)},
|
luis@1087
|
39 :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }}
|
luis@1080
|
40
|
luis@1080
|
41
|
chris@653
|
42 # Ensure error message uses proper text instead of
|
chris@653
|
43 # bibtex_entry.entry_type (#268). There has to be a better way to
|
chris@653
|
44 # do this!
|
luis@1287
|
45 def self.human_attribute_name(k, *args)
|
chris@653
|
46 if k == 'bibtex_entry.entry_type'
|
chris@653
|
47 l(:field_entry_type)
|
chris@653
|
48 else
|
chris@653
|
49 super
|
chris@653
|
50 end
|
chris@653
|
51 end
|
chris@653
|
52
|
luis@1068
|
53 def notify_authors_publication_added(project)
|
luis@643
|
54 self.authors.each do |author|
|
luis@651
|
55 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
|
luis@1401
|
56 Mailer.publication_added(author.user, self, project).deliver unless author.user.nil?
|
luis@643
|
57 end
|
luis@666
|
58 end
|
luis@1068
|
59
|
luis@1068
|
60 def notify_authors_publication_updated(project)
|
luis@666
|
61 self.authors.each do |author|
|
luis@666
|
62 Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
|
luis@1401
|
63 Mailer.publication_updated(author.user, self, project).deliver unless author.user.nil?
|
luis@666
|
64 end
|
luis@643
|
65 end
|
luis@1068
|
66
|
luis@1068
|
67
|
luis@556
|
68 def set_initial_author_order
|
luis@556
|
69 authorships = self.authorships
|
luis@1068
|
70
|
luis@556
|
71 logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
|
luis@1068
|
72
|
luis@556
|
73 authorships.each_with_index do |authorship, index|
|
luis@556
|
74 if authorship.auth_order.nil?
|
luis@556
|
75 authorship.auth_order = index
|
luis@556
|
76 end
|
luis@1068
|
77 end
|
luis@556
|
78 end
|
luis@1068
|
79
|
luis@946
|
80 def print_bibtex_author_names
|
luis@1068
|
81 # this authors are correctly sorted because the authorships model
|
luis@946
|
82 # already outputs the author names ASC by auth_order
|
luis@946
|
83 self.authorships.map{|a| a.name_on_paper}.join(' and ')
|
luis@1068
|
84 end
|
luis@1068
|
85
|
luis@946
|
86 def print_entry(style)
|
luis@946
|
87 bib = BibTeX::Entry.new
|
luis@946
|
88
|
luis@946
|
89 bib.author = self.print_bibtex_author_names
|
luis@946
|
90 bib.title = self.title
|
luis@946
|
91
|
luis@1068
|
92 self.bibtex_entry.attributes.keys.sort.each do |key|
|
luis@946
|
93 value = self.bibtex_entry.attributes[key].to_s
|
luis@946
|
94 next if key == 'id' or key == 'publication_id' or value == ""
|
luis@946
|
95
|
luis@1068
|
96 if key == "entry_type"
|
luis@1028
|
97 bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
|
luis@946
|
98 else
|
luis@946
|
99 bib[key.to_sym] = value
|
luis@1068
|
100 end
|
luis@946
|
101 end
|
luis@1068
|
102
|
luis@946
|
103 if style == :ieee
|
luis@1312
|
104 CiteProc.process(bib.to_citeproc, :style => :ieee, :format => :html)
|
luis@1068
|
105 else
|
luis@1023
|
106 bibtex = bib.to_s :include => :meta_content
|
luis@1023
|
107 bibtex.strip!
|
luis@1068
|
108 end
|
luis@946
|
109 end
|
luis@328
|
110 end
|