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 / authorship.rb @ 1401:95bdaaab97ca

History | View | Annotate | Download (2.2 KB)

1
class Authorship < ActiveRecord::Base
2
  unloadable
3

    
4
  belongs_to :author
5
  belongs_to :publication
6

    
7
  accepts_nested_attributes_for :author
8
  accepts_nested_attributes_for :publication
9

    
10
  validates_presence_of :name_on_paper
11

    
12
  attr_accessor :search_author_class, :search_author_id, :search_name, :search_results, :identify_author
13

    
14
  before_create :associate_author_user
15
  before_update :delete_publication_cache
16

    
17
  # tod: review scope of ordering
18
  acts_as_list :column => 'auth_order'
19

    
20
  # todo: review usage of scope --lf.20130108
21
  scope :like_unique, lambda {|q|
22
    s = "%#{q.to_s.strip.downcase}%"
23
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
24
     :order => 'name_on_paper',
25
     :group => "name_on_paper, institution, email"
26
    }
27
  }
28

    
29
  # todo: review usage of scope --lf.20130108
30
  scope :like, lambda {|q|
31
    s = "%#{q.to_s.strip.downcase}%"
32
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
33
     :order => 'name_on_paper'
34
    }
35
  }
36

    
37
  def name
38
    return self.name_on_paper
39
  end
40

    
41
  def <=>(authorship)
42
    name.downcase <=> authorship.name.downcase
43
  end
44

    
45
  def mail
46
    return self.email
47
  end
48

    
49
  protected
50

    
51
  def delete_publication_cache
52
    publication = Publication.find(self.publication_id)
53
    Rails.cache.delete "publication-#{publication.id}-ieee"
54
    Rails.cache.delete "publication-#{publication.id}-bibtex"
55
  end
56

    
57
  def associate_author_user
58
    case self.search_author_class
59
    when ""
60
      logger.debug { "Unknown Author to be added..." }
61
    when "User"
62
      author = Author.new
63
      author.save
64
      self.author_id = author.id
65

    
66
    when "Author"
67
      selected = self.search_results
68
      selected_classname = Kernel.const_get(self.search_author_class)
69
      selected_id = self.search_author_id
70
      object = selected_classname.find(selected_id)
71

    
72
      if object.respond_to? :name_on_paper
73
        # Authorship
74
        self.author_id = object.author.id
75
      else
76
        # User
77
        unless object.author.nil?
78
          self.author_id = object.author.id
79
        else
80
          author = Author.new
81
          object.author = author
82
          object.save
83
          self.author_id = object.author.id
84
        end
85
      end
86
    end
87
  end
88
end