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