| 9 |
9 |
|
| 10 |
10 |
validates_presence_of :name_on_paper
|
| 11 |
11 |
|
| 12 |
|
attr_accessor :search_author_class, :search_author_id, :search_name, :search_results, :identify_author
|
|
12 |
attr_writer :search_author_id, :search_author_class
|
|
13 |
attr_writer :search_author_tie
|
| 13 |
14 |
|
| 14 |
|
before_create :associate_author_user
|
|
15 |
### attr_accessor :search_results, :identify_author
|
|
16 |
## attr_writer :search_author_class
|
|
17 |
|
|
18 |
before_create :set_author
|
| 15 |
19 |
before_update :delete_publication_cache
|
| 16 |
20 |
|
| 17 |
21 |
# tod: review scope of ordering
|
| ... | ... | |
| 34 |
38 |
}
|
| 35 |
39 |
}
|
| 36 |
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 |
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 |
|
| 37 |
70 |
def name
|
| 38 |
71 |
return self.name_on_paper
|
| 39 |
72 |
end
|
| ... | ... | |
| 54 |
87 |
Rails.cache.delete "publication-#{publication.id}-bibtex"
|
| 55 |
88 |
end
|
| 56 |
89 |
|
| 57 |
|
def associate_author_user
|
|
90 |
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 |
|
| 58 |
102 |
case self.search_author_class
|
| 59 |
103 |
when ""
|
| 60 |
|
logger.debug { "Unknown Author to be added..." }
|
| 61 |
|
when "User"
|
|
104 |
logger.debug { "Adding new author to the database." }
|
| 62 |
105 |
author = Author.new
|
| 63 |
106 |
author.save
|
| 64 |
|
self.author_id = author.id
|
|
107 |
|
|
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
|
| 65 |
125 |
|
| 66 |
126 |
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)
|
|
127 |
author = Author.find(self.search_author_id)
|
|
128 |
end
|
| 71 |
129 |
|
| 72 |
|
if object.respond_to? :name_on_paper
|
| 73 |
|
# Authorship
|
| 74 |
|
self.author_id = object.author.id
|
| 75 |
|
else
|
| 76 |
|
# User
|
| 77 |
|
unless object.author.nil?
|
| 78 |
|
self.author_id = object.author.id
|
| 79 |
|
else
|
| 80 |
|
author = Author.new
|
| 81 |
|
object.author = author
|
| 82 |
|
object.save
|
| 83 |
|
self.author_id = object.author.id
|
| 84 |
|
end
|
| 85 |
|
end
|
| 86 |
|
end
|
|
130 |
self.author = author
|
| 87 |
131 |
end
|
| 88 |
132 |
end
|