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 @ 1287:1c3e2fb6793a

History | View | Annotate | Download (1.85 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
  before_save :associate_author_user
14

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

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

    
32
  def name
33
    return self.name_on_paper
34
  end
35

    
36
  def <=>(authorship)
37
    name.downcase <=> authorship.name.downcase
38
  end
39

    
40
  def mail
41
    return self.email
42
  end
43

    
44
  protected
45
  def associate_author_user
46
    case self.search_author_class
47
      when "User"
48
        author = Author.new
49
        author.save
50
        self.author_id = author.id
51
      else
52
        selected = self.search_results
53
        selected_classname = Kernel.const_get(self.search_author_class)
54
        selected_id = self.search_author_id
55
        object = selected_classname.find(selected_id)
56

    
57
        if object.respond_to? :name_on_paper
58
          # Authorship
59
          self.author_id = object.author.id
60
        else
61
          # User
62
          unless object.author.nil?
63
            self.author_id = object.author.id
64
          else
65
            author = Author.new
66
            object.author = author
67
            object.save
68
            self.author_id = object.author.id
69
          end
70
        end
71
    end
72
  end
73
end