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 @ 1317:2805873c0147

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