diff plugins/redmine_bibliography/app/models/authorship.rb @ 1426:7367cd232b1e luisf

Merge.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 07 Oct 2013 15:42:39 +0100
parents 35732ac4324a
children
line wrap: on
line diff
--- a/plugins/redmine_bibliography/app/models/authorship.rb	Mon Sep 30 13:52:14 2013 +0100
+++ b/plugins/redmine_bibliography/app/models/authorship.rb	Mon Oct 07 15:42:39 2013 +0100
@@ -9,9 +9,13 @@
 
   validates_presence_of :name_on_paper
 
-  attr_accessor :search_author_class, :search_author_id, :search_name, :search_results, :identify_author
+  attr_writer :search_author_id , :search_author_class
+  attr_writer :search_author_tie
 
-  before_create :associate_author_user
+  ### attr_accessor :search_results, :identify_author
+  ## attr_writer :search_author_class
+
+  before_save :set_author
   before_update :delete_publication_cache
 
   # tod: review scope of ordering
@@ -34,6 +38,40 @@
     }
   }
 
+  def search_author_class
+    # Authorship must always have an Author
+    # unless it hasn't been saved yet
+    # using default setter (attr_writer)
+
+    if self.author.nil?
+      aclass = ""
+    else
+      aclass = "Author"
+    end
+
+    @search_author_class || aclass
+  end
+
+  def search_author_id
+    if self.author.nil?
+      authid = ""
+    else
+      authid = author_id
+    end
+
+    @search_author_id || authid
+  end
+
+  def search_author_tie
+    if self.author.nil?
+      auth_tie = false
+    else
+      auth_tie = true
+    end
+
+    @search_author_tie || auth_tie
+  end
+
   def name
     return self.name_on_paper
   end
@@ -54,35 +92,47 @@
     Rails.cache.delete "publication-#{publication.id}-bibtex"
   end
 
-  def associate_author_user
-    case self.search_author_class
-    when ""
-      logger.debug { "Unknown Author to be added..." }
-    when "User"
+  private
+
+  def set_author
+    # do we want to associate the authorship
+    #  with an existing author/user?
+    if @search_author_tie
+      # if an author, simply associates with it
+      # if an user, checks if it has already an author associated with it
+      #   if so, associates with that author
+      #   otherwise, creates a new author
+
+      case @search_author_class
+      when ""
+        author = Author.new
+        author.save
+
+      when "User"
+        user = User.find(@search_author_id)
+
+        if user.author.nil?
+          # User w/o author:
+          # create new author and update user
+          author = Author.new
+          author.save
+          user.author = author
+          user.save
+        else
+          author = user.author
+        end
+
+      when "Author"
+        author = Author.find(@search_author_id)
+      end
+
+    # if we don't want to associate with an existing author/user
+    else
+      # todo: should we delete any previously existing relationship?
       author = Author.new
       author.save
-      self.author_id = author.id
+    end
 
-    when "Author"
-      selected = self.search_results
-      selected_classname = Kernel.const_get(self.search_author_class)
-      selected_id = self.search_author_id
-      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
+    self.author = author
   end
 end