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 @ 1432:ebda59ca84db

History | View | Annotate | Download (3.68 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 1287:1c3e2fb6793a luis
  validates_associated :bibtex_entry, :authorships
14 445:77f88379115a luis
15
  accepts_nested_attributes_for :authorships
16 446:995d4c99843d luis
  accepts_nested_attributes_for :authors, :allow_destroy => true
17 454:2f1a308c4c11 luis
  accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
18 1068:e11d8d13ebc5 luis
19 464:fbdfec975bfa luis
  has_and_belongs_to_many :projects, :uniq => true
20 1068:e11d8d13ebc5 luis
21 567:5404f7dfb4b3 chris
  before_save :set_initial_author_order
22 653:0c5674b65db0 chris
23 1212:1186340b4ad4 luis
  scope :visible, lambda {|*args| { :include => :projects,
24
                                    :conditions => Project.allowed_to_condition(args.shift || User.current, :view_publication, *args) } }
25 1087:74407a04925c luis
26 1080:5bd8c86cfa6a luis
  acts_as_activity_provider :type => 'publication',
27
                            :timestamp => "#{Publication.table_name}.created_at",
28 1087:74407a04925c luis
                            :find_options => {
29
                              :include => :projects,
30
                              :conditions => "#{Project.table_name}.id = projects_publications.project_id"
31
                            }
32 1080:5bd8c86cfa6a luis
33
  acts_as_event :title => Proc.new {|o| o.title },
34
                :datetime => :created_at,
35
                :type => 'publications',
36 1087:74407a04925c luis
                :author => nil,
37 1080:5bd8c86cfa6a luis
                #todo - need too move the cache from the helper to the model
38
                :description => Proc.new {|o| o.print_entry(:ieee)},
39 1087:74407a04925c luis
                :url => Proc.new {|o| {:controller => 'publications', :action => 'show', :id => o.id }}
40 1080:5bd8c86cfa6a luis
41
42 653:0c5674b65db0 chris
  # 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 1287:1c3e2fb6793a luis
  def self.human_attribute_name(k, *args)
46 653:0c5674b65db0 chris
    if k == 'bibtex_entry.entry_type'
47
      l(:field_entry_type)
48
    else
49
      super
50
    end
51
  end
52
53 1068:e11d8d13ebc5 luis
  def notify_authors_publication_added(project)
54 643:505fdac73166 luis
    self.authors.each do |author|
55 651:f029431de4dd luis
      Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
56 1401:95bdaaab97ca luis
      Mailer.publication_added(author.user, self, project).deliver unless author.user.nil?
57 643:505fdac73166 luis
    end
58 666:865d079e5fa0 luis
  end
59 1068:e11d8d13ebc5 luis
60
  def notify_authors_publication_updated(project)
61 666:865d079e5fa0 luis
    self.authors.each do |author|
62
      Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
63 1401:95bdaaab97ca luis
      Mailer.publication_updated(author.user, self, project).deliver unless author.user.nil?
64 666:865d079e5fa0 luis
    end
65 643:505fdac73166 luis
  end
66 1068:e11d8d13ebc5 luis
67
68 556:ca9e8e562ea7 luis
  def set_initial_author_order
69
    authorships = self.authorships
70 1068:e11d8d13ebc5 luis
71 556:ca9e8e562ea7 luis
    logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
72 1068:e11d8d13ebc5 luis
73 556:ca9e8e562ea7 luis
    authorships.each_with_index do |authorship, index|
74
      if authorship.auth_order.nil?
75
         authorship.auth_order = index
76
      end
77 1068:e11d8d13ebc5 luis
    end
78 556:ca9e8e562ea7 luis
  end
79 1068:e11d8d13ebc5 luis
80 946:a0c9cc95bcf3 luis
  def print_bibtex_author_names
81 1068:e11d8d13ebc5 luis
    # this authors are correctly sorted because the authorships model
82 946:a0c9cc95bcf3 luis
    # already outputs the author names ASC by auth_order
83
    self.authorships.map{|a| a.name_on_paper}.join(' and ')
84 1068:e11d8d13ebc5 luis
  end
85
86 946:a0c9cc95bcf3 luis
  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 1068:e11d8d13ebc5 luis
    self.bibtex_entry.attributes.keys.sort.each do |key|
93 946:a0c9cc95bcf3 luis
      value = self.bibtex_entry.attributes[key].to_s
94
      next if key == 'id' or key == 'publication_id' or value == ""
95
96 1068:e11d8d13ebc5 luis
      if key == "entry_type"
97 1028:b8ae7b3af25a luis
        bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
98 946:a0c9cc95bcf3 luis
      else
99
        bib[key.to_sym] = value
100 1068:e11d8d13ebc5 luis
      end
101 946:a0c9cc95bcf3 luis
    end
102 1068:e11d8d13ebc5 luis
103 946:a0c9cc95bcf3 luis
    if style == :ieee
104 1312:2abc48cc545e luis
      CiteProc.process(bib.to_citeproc, :style => :ieee, :format => :html)
105 1068:e11d8d13ebc5 luis
    else
106 1023:3d924264419a luis
      bibtex = bib.to_s :include => :meta_content
107
      bibtex.strip!
108 1068:e11d8d13ebc5 luis
    end
109 946:a0c9cc95bcf3 luis
  end
110 328:aed18b463206 luis
end