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