To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / redmine_bibliography / app / models / publication.rb @ 1080:5bd8c86cfa6a
History | View | Annotate | Download (3.6 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 | 1080:5bd8c86cfa6a | luis | acts_as_activity_provider :type => 'publication', |
| 23 | :timestamp => "#{Publication.table_name}.created_at", |
||
| 24 | :author_key => "#{Publication.table_name}.builder_id", |
||
| 25 | :find_options => {:joins => "INNER JOIN projects_publications ON #{Publication.table_name}.id = projects_publications.publication_id JOIN #{Project.table_name} ON #{Project.table_name}.id = projects_publications.project_id"} |
||
| 26 | |||
| 27 | acts_as_event :title => Proc.new {|o| o.title }, |
||
| 28 | :datetime => :created_at, |
||
| 29 | :author => nil, |
||
| 30 | :type => 'publications', |
||
| 31 | #todo - need too move the cache from the helper to the model
|
||
| 32 | :description => Proc.new {|o| o.print_entry(:ieee)}, |
||
| 33 | :project => Project.first |
||
| 34 | # :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }}
|
||
| 35 | |||
| 36 | |||
| 37 | 653:0c5674b65db0 | chris | # Ensure error message uses proper text instead of
|
| 38 | # bibtex_entry.entry_type (#268). There has to be a better way to
|
||
| 39 | # do this!
|
||
| 40 | def self.human_attribute_name(k) |
||
| 41 | if k == 'bibtex_entry.entry_type' |
||
| 42 | l(:field_entry_type)
|
||
| 43 | else
|
||
| 44 | super
|
||
| 45 | end
|
||
| 46 | end
|
||
| 47 | |||
| 48 | 1068:e11d8d13ebc5 | luis | def notify_authors_publication_added(project) |
| 49 | 643:505fdac73166 | luis | self.authors.each do |author| |
| 50 | 651:f029431de4dd | luis | Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." } |
| 51 | 666:865d079e5fa0 | luis | Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil? |
| 52 | 643:505fdac73166 | luis | end
|
| 53 | 666:865d079e5fa0 | luis | end
|
| 54 | 1068:e11d8d13ebc5 | luis | |
| 55 | def notify_authors_publication_updated(project) |
||
| 56 | 666:865d079e5fa0 | luis | self.authors.each do |author| |
| 57 | Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." } |
||
| 58 | Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil? |
||
| 59 | end
|
||
| 60 | 643:505fdac73166 | luis | end
|
| 61 | 1068:e11d8d13ebc5 | luis | |
| 62 | |||
| 63 | 556:ca9e8e562ea7 | luis | def set_initial_author_order |
| 64 | authorships = self.authorships
|
||
| 65 | 1068:e11d8d13ebc5 | luis | |
| 66 | 556:ca9e8e562ea7 | luis | logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
|
| 67 | 1068:e11d8d13ebc5 | luis | |
| 68 | 556:ca9e8e562ea7 | luis | authorships.each_with_index do |authorship, index|
|
| 69 | if authorship.auth_order.nil?
|
||
| 70 | authorship.auth_order = index |
||
| 71 | end
|
||
| 72 | 1068:e11d8d13ebc5 | luis | end
|
| 73 | 556:ca9e8e562ea7 | luis | end
|
| 74 | 1068:e11d8d13ebc5 | luis | |
| 75 | 946:a0c9cc95bcf3 | luis | def print_bibtex_author_names |
| 76 | 1068:e11d8d13ebc5 | luis | # this authors are correctly sorted because the authorships model
|
| 77 | 946:a0c9cc95bcf3 | luis | # already outputs the author names ASC by auth_order
|
| 78 | self.authorships.map{|a| a.name_on_paper}.join(' and ') |
||
| 79 | 1068:e11d8d13ebc5 | luis | end
|
| 80 | |||
| 81 | 946:a0c9cc95bcf3 | luis | def print_entry(style) |
| 82 | bib = BibTeX::Entry.new |
||
| 83 | |||
| 84 | bib.author = self.print_bibtex_author_names
|
||
| 85 | bib.title = self.title
|
||
| 86 | |||
| 87 | 1068:e11d8d13ebc5 | luis | self.bibtex_entry.attributes.keys.sort.each do |key| |
| 88 | 946:a0c9cc95bcf3 | luis | value = self.bibtex_entry.attributes[key].to_s
|
| 89 | next if key == 'id' or key == 'publication_id' or value == "" |
||
| 90 | |||
| 91 | 1068:e11d8d13ebc5 | luis | if key == "entry_type" |
| 92 | 1028:b8ae7b3af25a | luis | bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name |
| 93 | 946:a0c9cc95bcf3 | luis | else
|
| 94 | bib[key.to_sym] = value |
||
| 95 | 1068:e11d8d13ebc5 | luis | end
|
| 96 | 946:a0c9cc95bcf3 | luis | end
|
| 97 | 1068:e11d8d13ebc5 | luis | |
| 98 | 946:a0c9cc95bcf3 | luis | if style == :ieee |
| 99 | 1068:e11d8d13ebc5 | luis | CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html |
| 100 | else
|
||
| 101 | 1023:3d924264419a | luis | bibtex = bib.to_s :include => :meta_content |
| 102 | bibtex.strip! |
||
| 103 | logger.error { bibtex }
|
||
| 104 | 1068:e11d8d13ebc5 | luis | end
|
| 105 | 946:a0c9cc95bcf3 | luis | end
|
| 106 | 328:aed18b463206 | luis | end |