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 @ 1286:d0d6bbe9f2e0

History | View | Annotate | Download (1.85 KB)

1 328:aed18b463206 luis
class Authorship < ActiveRecord::Base
2 1123:48c5fdd6cf10 luis
  unloadable
3
4 328:aed18b463206 luis
  belongs_to :author
5
  belongs_to :publication
6 1123:48c5fdd6cf10 luis
7 483:cc267eb99115 luis
  accepts_nested_attributes_for :author
8
  accepts_nested_attributes_for :publication
9 686:b1debf464389 luis
10
  validates_presence_of :name_on_paper
11 1123:48c5fdd6cf10 luis
12 1286:d0d6bbe9f2e0 luis
  attr_accessor :search_author_class, :search_author_id, :search_name, :search_results, :identify_author
13 563:a76abc63257a luis
  before_save :associate_author_user
14 591:9e866f13c984 luis
15 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
16 1123:48c5fdd6cf10 luis
  scope :like_unique, lambda {|q|
17 601:1608b3cb50cd luis
    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 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
25 1123:48c5fdd6cf10 luis
  scope :like, lambda {|q|
26 591:9e866f13c984 luis
    s = "%#{q.to_s.strip.downcase}%"
27 601:1608b3cb50cd luis
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
28 591:9e866f13c984 luis
     :order => 'name_on_paper'
29
    }
30
  }
31 1123:48c5fdd6cf10 luis
32 592:68c6b060385c luis
  def name
33
    return self.name_on_paper
34
  end
35 1123:48c5fdd6cf10 luis
36 591:9e866f13c984 luis
  def <=>(authorship)
37 592:68c6b060385c luis
    name.downcase <=> authorship.name.downcase
38
  end
39 1123:48c5fdd6cf10 luis
40 592:68c6b060385c luis
  def mail
41
    return self.email
42 591:9e866f13c984 luis
  end
43 1123:48c5fdd6cf10 luis
44
  protected
45
  def associate_author_user
46 1286:d0d6bbe9f2e0 luis
    case self.search_author_class
47
      when "User"
48 601:1608b3cb50cd luis
        author = Author.new
49
        author.save
50
        self.author_id = author.id
51
      else
52
        selected = self.search_results
53 1286:d0d6bbe9f2e0 luis
        selected_classname = Kernel.const_get(self.search_author_class)
54
        selected_id = self.search_author_id
55 601:1608b3cb50cd luis
        object = selected_classname.find(selected_id)
56 563:a76abc63257a luis
57 601:1608b3cb50cd luis
        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 1123:48c5fdd6cf10 luis
    end
72 518:b24091590b63 luis
  end
73 328:aed18b463206 luis
end