To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / redmine_bibliography / app / models / authorship.rb @ 723:d41bf754c0f2
History | View | Annotate | Download (1.77 KB)
| 1 | 328:aed18b463206 | luis | class Authorship < ActiveRecord::Base |
|---|---|---|---|
| 2 | 562:82a6e3756383 | luis | unloadable |
| 3 | |||
| 4 | 328:aed18b463206 | luis | belongs_to :author
|
| 5 | belongs_to :publication
|
||
| 6 | 393:9595ab4cac6b | 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 | 483:cc267eb99115 | luis | |
| 12 | 597:70f0276fde9a | luis | attr_accessor :is_user, :author_user_id, :search_name, :identify_author, :search_results |
| 13 | 563:a76abc63257a | luis | before_save :associate_author_user
|
| 14 | 591:9e866f13c984 | luis | |
| 15 | 601:1608b3cb50cd | luis | 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 | 591:9e866f13c984 | luis | named_scope :like, lambda {|q|
|
| 24 | s = "%#{q.to_s.strip.downcase}%"
|
||
| 25 | 601:1608b3cb50cd | luis | {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
|
| 26 | 591:9e866f13c984 | luis | :order => 'name_on_paper' |
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | 592:68c6b060385c | luis | def name |
| 31 | return self.name_on_paper |
||
| 32 | end
|
||
| 33 | 591:9e866f13c984 | luis | |
| 34 | def <=>(authorship) |
||
| 35 | 592:68c6b060385c | luis | name.downcase <=> authorship.name.downcase |
| 36 | end
|
||
| 37 | |||
| 38 | def mail |
||
| 39 | return self.email |
||
| 40 | 591:9e866f13c984 | luis | end
|
| 41 | 530:ce1614b19759 | luis | |
| 42 | 563:a76abc63257a | luis | protected |
| 43 | def associate_author_user |
||
| 44 | 601:1608b3cb50cd | luis | 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 | 563:a76abc63257a | luis | |
| 55 | 601:1608b3cb50cd | luis | 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 | 518:b24091590b63 | luis | end
|
| 71 | 328:aed18b463206 | luis | end |