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 @ 1363:855b4ae5ecdd

History | View | Annotate | Download (1.92 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 1363:855b4ae5ecdd luis
  # tod: review scope of ordering
16
  acts_as_list :column => 'auth_order'
17 1317:2805873c0147 luis
18 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
19 1123:48c5fdd6cf10 luis
  scope :like_unique, lambda {|q|
20 601:1608b3cb50cd luis
    s = "%#{q.to_s.strip.downcase}%"
21
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
22
     :order => 'name_on_paper',
23
     :group => "name_on_paper, institution, email"
24
    }
25
  }
26
27 1124:807426fa6017 luis
  # todo: review usage of scope --lf.20130108
28 1123:48c5fdd6cf10 luis
  scope :like, lambda {|q|
29 591:9e866f13c984 luis
    s = "%#{q.to_s.strip.downcase}%"
30 601:1608b3cb50cd luis
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
31 591:9e866f13c984 luis
     :order => 'name_on_paper'
32
    }
33
  }
34 1123:48c5fdd6cf10 luis
35 592:68c6b060385c luis
  def name
36
    return self.name_on_paper
37
  end
38 1123:48c5fdd6cf10 luis
39 591:9e866f13c984 luis
  def <=>(authorship)
40 592:68c6b060385c luis
    name.downcase <=> authorship.name.downcase
41
  end
42 1123:48c5fdd6cf10 luis
43 592:68c6b060385c luis
  def mail
44
    return self.email
45 591:9e866f13c984 luis
  end
46 1123:48c5fdd6cf10 luis
47
  protected
48
  def associate_author_user
49 1286:d0d6bbe9f2e0 luis
    case self.search_author_class
50
      when "User"
51 601:1608b3cb50cd luis
        author = Author.new
52
        author.save
53
        self.author_id = author.id
54
      else
55
        selected = self.search_results
56 1286:d0d6bbe9f2e0 luis
        selected_classname = Kernel.const_get(self.search_author_class)
57
        selected_id = self.search_author_id
58 601:1608b3cb50cd luis
        object = selected_classname.find(selected_id)
59 563:a76abc63257a luis
60 601:1608b3cb50cd luis
        if object.respond_to? :name_on_paper
61
          # Authorship
62
          self.author_id = object.author.id
63
        else
64
          # User
65
          unless object.author.nil?
66
            self.author_id = object.author.id
67
          else
68
            author = Author.new
69
            object.author = author
70
            object.save
71
            self.author_id = object.author.id
72
          end
73
        end
74 1123:48c5fdd6cf10 luis
    end
75 518:b24091590b63 luis
  end
76 328:aed18b463206 luis
end