luis@328: class Authorship < ActiveRecord::Base luis@1123: unloadable luis@1123: luis@328: belongs_to :author luis@328: belongs_to :publication luis@1123: luis@483: accepts_nested_attributes_for :author luis@483: accepts_nested_attributes_for :publication luis@686: luis@686: validates_presence_of :name_on_paper luis@1123: luis@1395: attr_writer :search_author_id , :search_author_class luis@1394: attr_writer :search_author_tie luis@1364: luis@1394: ### attr_accessor :search_results, :identify_author luis@1394: ## attr_writer :search_author_class luis@1394: luis@1395: before_save :set_author luis@1367: before_update :delete_publication_cache luis@591: luis@1363: # tod: review scope of ordering luis@1363: acts_as_list :column => 'auth_order' luis@1317: luis@1124: # todo: review usage of scope --lf.20130108 luis@1123: scope :like_unique, lambda {|q| luis@601: s = "%#{q.to_s.strip.downcase}%" luis@601: {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}], luis@601: :order => 'name_on_paper', luis@601: :group => "name_on_paper, institution, email" luis@601: } luis@601: } luis@601: luis@1124: # todo: review usage of scope --lf.20130108 luis@1123: scope :like, lambda {|q| luis@591: s = "%#{q.to_s.strip.downcase}%" luis@601: {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}], luis@591: :order => 'name_on_paper' luis@591: } luis@591: } luis@1123: luis@1394: def search_author_class luis@1394: # Authorship must always have an Author luis@1394: # unless it hasn't been saved yet luis@1394: # using default setter (attr_writer) luis@1394: luis@1394: if self.author.nil? luis@1397: aclass = "" luis@1394: else luis@1397: aclass = "Author" luis@1394: end luis@1395: luis@1397: @search_author_class || aclass luis@1394: end luis@1394: luis@1394: def search_author_id luis@1394: if self.author.nil? luis@1398: authid = "" luis@1394: else luis@1397: authid = author_id luis@1394: end luis@1397: luis@1397: @search_author_id || authid luis@1394: end luis@1394: luis@1394: def search_author_tie luis@1394: if self.author.nil? luis@1397: auth_tie = false luis@1394: else luis@1397: auth_tie = true luis@1394: end luis@1394: luis@1397: @search_author_tie || auth_tie luis@1394: end luis@1394: luis@592: def name luis@592: return self.name_on_paper luis@592: end luis@1123: luis@591: def <=>(authorship) luis@592: name.downcase <=> authorship.name.downcase luis@592: end luis@1123: luis@592: def mail luis@592: return self.email luis@591: end luis@1123: luis@1123: protected luis@1367: luis@1367: def delete_publication_cache luis@1367: publication = Publication.find(self.publication_id) luis@1367: Rails.cache.delete "publication-#{publication.id}-ieee" luis@1367: Rails.cache.delete "publication-#{publication.id}-bibtex" luis@1367: end luis@1367: luis@1395: private luis@1395: luis@1394: def set_author luis@1403: # do we want to associate the authorship luis@1403: # with an existing author/user? luis@1403: if @search_author_tie luis@1403: # if an author, simply associates with it luis@1403: # if an user, checks if it has already an author associated with it luis@1403: # if so, associates with that author luis@1403: # otherwise, creates a new author luis@1394: luis@1403: case @search_author_class luis@1403: when "" luis@1403: author = Author.new luis@1403: author.save luis@1394: luis@1403: when "User" luis@1403: user = User.find(@search_author_id) luis@1394: luis@1403: if user.author.nil? luis@1403: # User w/o author: luis@1403: # create new author and update user luis@1403: author = Author.new luis@1403: author.save luis@1403: user.author = author luis@1403: user.save luis@1403: else luis@1403: author = user.author luis@1403: end luis@1403: luis@1403: when "Author" luis@1403: author = Author.find(@search_author_id) luis@1403: end luis@1403: luis@1403: # if we don't want to associate with an existing author/user luis@1403: else luis@1403: # todo: should we delete any previously existing relationship? luis@1366: author = Author.new luis@1366: author.save luis@1394: end luis@1366: luis@1394: self.author = author luis@518: end luis@328: end