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 / vendor / plugins / redmine_bibliography / app / models / authorship.rb @ 1024:503ed68a4e0f

History | View | Annotate | Download (1.77 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 :is_user, :author_user_id, :search_name, :identify_author, :search_results
13
  before_save :associate_author_user
14

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

    
23
  named_scope :like, lambda {|q| 
24
    s = "%#{q.to_s.strip.downcase}%"
25
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
26
     :order => 'name_on_paper'
27
    }
28
  }
29
  
30
  def name
31
    return self.name_on_paper
32
  end
33
  
34
  def <=>(authorship)
35
    name.downcase <=> authorship.name.downcase
36
  end
37
    
38
  def mail
39
    return self.email
40
  end
41
  
42
  protected 
43
  def associate_author_user 
44
    case self.identify_author
45
      when "no"
46
        author = Author.new
47
        author.save
48
        self.author_id = author.id
49
      else
50
        selected = self.search_results
51
        selected_classname = Kernel.const_get(selected.split('_')[0])
52
        selected_id = selected.split('_')[1]
53
        object = selected_classname.find(selected_id)
54

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