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 @ 1394:0f918e37e1d6
History | View | Annotate | Download (3.05 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 | 1394:0f918e37e1d6 | luis | attr_writer :search_author_id, :search_author_class |
| 13 | attr_writer :search_author_tie
|
||
| 14 | 1364:4d5d25039a5f | luis | |
| 15 | 1394:0f918e37e1d6 | luis | ### attr_accessor :search_results, :identify_author
|
| 16 | ## attr_writer :search_author_class
|
||
| 17 | |||
| 18 | before_create :set_author
|
||
| 19 | 1367:a2e51c0a7860 | luis | before_update :delete_publication_cache
|
| 20 | 591:9e866f13c984 | luis | |
| 21 | 1363:855b4ae5ecdd | luis | # tod: review scope of ordering
|
| 22 | acts_as_list :column => 'auth_order' |
||
| 23 | 1317:2805873c0147 | luis | |
| 24 | 1124:807426fa6017 | luis | # todo: review usage of scope --lf.20130108
|
| 25 | 1123:48c5fdd6cf10 | luis | scope :like_unique, lambda {|q|
|
| 26 | 601:1608b3cb50cd | luis | s = "%#{q.to_s.strip.downcase}%"
|
| 27 | {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
|
||
| 28 | :order => 'name_on_paper', |
||
| 29 | :group => "name_on_paper, institution, email" |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | 1124:807426fa6017 | luis | # todo: review usage of scope --lf.20130108
|
| 34 | 1123:48c5fdd6cf10 | luis | scope :like, lambda {|q|
|
| 35 | 591:9e866f13c984 | luis | s = "%#{q.to_s.strip.downcase}%"
|
| 36 | 601:1608b3cb50cd | luis | {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
|
| 37 | 591:9e866f13c984 | luis | :order => 'name_on_paper' |
| 38 | } |
||
| 39 | } |
||
| 40 | 1123:48c5fdd6cf10 | luis | |
| 41 | 1394:0f918e37e1d6 | luis | def search_author_class |
| 42 | # Authorship must always have an Author
|
||
| 43 | # unless it hasn't been saved yet
|
||
| 44 | # using default setter (attr_writer)
|
||
| 45 | |||
| 46 | if self.author.nil? |
||
| 47 | return "" |
||
| 48 | else
|
||
| 49 | return "Author" |
||
| 50 | end
|
||
| 51 | end
|
||
| 52 | |||
| 53 | def search_author_id |
||
| 54 | if self.author.nil? |
||
| 55 | return "" |
||
| 56 | else
|
||
| 57 | return self.author_id |
||
| 58 | end
|
||
| 59 | end
|
||
| 60 | |||
| 61 | def search_author_tie |
||
| 62 | if self.author.nil? |
||
| 63 | return false |
||
| 64 | else
|
||
| 65 | return true |
||
| 66 | end
|
||
| 67 | |||
| 68 | end
|
||
| 69 | |||
| 70 | 592:68c6b060385c | luis | def name |
| 71 | return self.name_on_paper |
||
| 72 | end
|
||
| 73 | 1123:48c5fdd6cf10 | luis | |
| 74 | 591:9e866f13c984 | luis | def <=>(authorship) |
| 75 | 592:68c6b060385c | luis | name.downcase <=> authorship.name.downcase |
| 76 | end
|
||
| 77 | 1123:48c5fdd6cf10 | luis | |
| 78 | 592:68c6b060385c | luis | def mail |
| 79 | return self.email |
||
| 80 | 591:9e866f13c984 | luis | end
|
| 81 | 1123:48c5fdd6cf10 | luis | |
| 82 | protected |
||
| 83 | 1367:a2e51c0a7860 | luis | |
| 84 | def delete_publication_cache |
||
| 85 | publication = Publication.find(self.publication_id) |
||
| 86 | Rails.cache.delete "publication-#{publication.id}-ieee" |
||
| 87 | Rails.cache.delete "publication-#{publication.id}-bibtex" |
||
| 88 | end
|
||
| 89 | |||
| 90 | 1394:0f918e37e1d6 | luis | def set_author |
| 91 | # if an author, simply associates with it
|
||
| 92 | # if an user, checks if it has already an author associated with it
|
||
| 93 | # if so, assicoates with that author
|
||
| 94 | # otherwise, creates a new author
|
||
| 95 | |||
| 96 | logger.error { "%%%%%%%%%%%%%%% Associate Author User %%%%%%%%%%%%%%" }
|
||
| 97 | |||
| 98 | logger.error { "EU #{self.to_yaml}" }
|
||
| 99 | logger.error { "Class: #{search_author_class}" }
|
||
| 100 | logger.error { "ID #{search_author_id}" }
|
||
| 101 | |||
| 102 | 1286:d0d6bbe9f2e0 | luis | case self.search_author_class |
| 103 | 1366:7e85f7988ab8 | luis | when "" |
| 104 | 1394:0f918e37e1d6 | luis | logger.debug { "Adding new author to the database." }
|
| 105 | 1366:7e85f7988ab8 | luis | author = Author.new
|
| 106 | author.save |
||
| 107 | 1394:0f918e37e1d6 | luis | |
| 108 | when "User" |
||
| 109 | # get user id
|
||
| 110 | user = User.find(self.search_author_id) |
||
| 111 | logger.error { "Found user with this ID: #{user.id}" }
|
||
| 112 | |||
| 113 | if user.author.nil?
|
||
| 114 | logger.error { "The user has no author... creating one!" }
|
||
| 115 | |||
| 116 | # User w/o author:
|
||
| 117 | # create new author and update user
|
||
| 118 | author = Author.new
|
||
| 119 | author.save |
||
| 120 | user << author |
||
| 121 | else
|
||
| 122 | logger.error { "found an author!" }
|
||
| 123 | author = user.author |
||
| 124 | end
|
||
| 125 | 1366:7e85f7988ab8 | luis | |
| 126 | when "Author" |
||
| 127 | 1394:0f918e37e1d6 | luis | author = Author.find(self.search_author_id) |
| 128 | end
|
||
| 129 | 1366:7e85f7988ab8 | luis | |
| 130 | 1394:0f918e37e1d6 | luis | self.author = author
|
| 131 | 518:b24091590b63 | luis | end
|
| 132 | 328:aed18b463206 | luis | end |