view plugins/redmine_bibliography/app/models/authorship.rb @ 1123:48c5fdd6cf10 redmine-2.2-integration

Rails 3 compatibility: renamed all named_scope calls to scope.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 08 Jan 2013 16:19:01 +0000
parents b4b72f1eb644
children 807426fa6017
line wrap: on
line source
class Authorship < ActiveRecord::Base
  unloadable

  belongs_to :author
  belongs_to :publication

  accepts_nested_attributes_for :author
  accepts_nested_attributes_for :publication

  validates_presence_of :name_on_paper

  attr_accessor :is_user, :author_user_id, :search_name, :identify_author, :search_results
  before_save :associate_author_user

  scope :like_unique, lambda {|q|
    s = "%#{q.to_s.strip.downcase}%"
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
     :order => 'name_on_paper',
     :group => "name_on_paper, institution, email"
    }
  }

  scope :like, lambda {|q|
    s = "%#{q.to_s.strip.downcase}%"
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
     :order => 'name_on_paper'
    }
  }

  def name
    return self.name_on_paper
  end

  def <=>(authorship)
    name.downcase <=> authorship.name.downcase
  end

  def mail
    return self.email
  end

  protected
  def associate_author_user
    case self.identify_author
      when "no"
        author = Author.new
        author.save
        self.author_id = author.id
      else
        selected = self.search_results
        selected_classname = Kernel.const_get(selected.split('_')[0])
        selected_id = selected.split('_')[1]
        object = selected_classname.find(selected_id)

        if object.respond_to? :name_on_paper
          # Authorship
          self.author_id = object.author.id
        else
          # User
          unless object.author.nil?
            self.author_id = object.author.id
          else
            author = Author.new
            object.author = author
            object.save
            self.author_id = object.author.id
          end
        end
    end
  end
end