To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / plugins / redmine_bibliography / app / models / authorship.rb @ 1402:5cb7eeccede1

History | View | Annotate | Download (3.29 KB)

1
class Authorship < ActiveRecord::Base
2
  unloadable
3

    
4
  belongs_to :author
5
  belongs_to :publication
6

    
7
  accepts_nested_attributes_for :author
8
  accepts_nested_attributes_for :publication
9

    
10
  validates_presence_of :name_on_paper
11

    
12
  attr_writer :search_author_id , :search_author_class
13
  attr_writer :search_author_tie
14

    
15
  ### attr_accessor :search_results, :identify_author
16
  ## attr_writer :search_author_class
17

    
18
  before_save :set_author
19
  before_update :delete_publication_cache
20

    
21
  # tod: review scope of ordering
22
  acts_as_list :column => 'auth_order'
23

    
24
  # todo: review usage of scope --lf.20130108
25
  scope :like_unique, lambda {|q|
26
    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
  # todo: review usage of scope --lf.20130108
34
  scope :like, lambda {|q|
35
    s = "%#{q.to_s.strip.downcase}%"
36
    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
37
     :order => 'name_on_paper'
38
    }
39
  }
40

    
41
  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
      aclass = ""
48
    else
49
      aclass = "Author"
50
    end
51

    
52
    @search_author_class || aclass
53
  end
54

    
55
  # def search_author_class=(search_author_class)
56
  #  @search_author_class = search_author_class
57
  # end
58

    
59
  def search_author_id
60
    if self.author.nil?
61
      authid = ""
62
    else
63
      authid = author_id
64
    end
65

    
66
    @search_author_id || authid
67
  end
68

    
69
  def search_author_tie
70
    if self.author.nil?
71
      auth_tie = false
72
    else
73
      auth_tie = true
74
    end
75

    
76
    @search_author_tie || auth_tie
77
  end
78

    
79
  def name
80
    return self.name_on_paper
81
  end
82

    
83
  def <=>(authorship)
84
    name.downcase <=> authorship.name.downcase
85
  end
86

    
87
  def mail
88
    return self.email
89
  end
90

    
91
  protected
92

    
93
  def delete_publication_cache
94
    publication = Publication.find(self.publication_id)
95
    Rails.cache.delete "publication-#{publication.id}-ieee"
96
    Rails.cache.delete "publication-#{publication.id}-bibtex"
97
  end
98

    
99
  private
100

    
101
  def set_author
102
    # if an author, simply associates with it
103
    # if an user, checks if it has already an author associated with it
104
    # if so, assicoates with that author
105
    # otherwise, creates a new author
106

    
107
    logger.error { "%%%%%%%%%%%%%%% Associate Author User %%%%%%%%%%%%%%" }
108

    
109
    logger.error { "Me #{self.to_yaml}" }
110
    logger.error { "Class: #{@search_author_class}" }
111
    logger.error { "ID #{@search_author_id}" }
112

    
113
    case @search_author_class
114
    when ""
115
      logger.debug { "Adding new author to the database." }
116
      author = Author.new
117
      author.save
118

    
119
    when "User"
120
      # get user id
121
      user = User.find(@search_author_id)
122
      logger.error { "Found user with this ID: #{user.id}" }
123

    
124
      if user.author.nil?
125
        logger.error { "The user has no author... creating one!" }
126

    
127
        # User w/o author:
128
        # create new author and update user
129
        author = Author.new
130
        author.save
131
        user.author = author
132
        user.save
133
      else
134
        logger.error { "found an author!" }
135
        author = user.author
136
      end
137

    
138
    when "Author"
139
      author = Author.find(@search_author_id)
140
    end
141

    
142
    self.author = author
143
  end
144
end