changeset 563:a76abc63257a feature_36

working on the author<->user logic.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Mon, 08 Aug 2011 18:13:30 +0100
parents 82a6e3756383
children a47abc953e45 b18f19710242
files vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb vendor/plugins/redmine_bibliography/app/models/authorship.rb vendor/plugins/redmine_bibliography/app/models/publication.rb vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml vendor/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb
diffstat 5 files changed, 52 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Fri Aug 05 17:44:17 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Mon Aug 08 18:13:30 2011 +0100
@@ -11,11 +11,15 @@
   
   def identify_author(author)
     if author.class == User
+
+      Rails.logger.debug { "Identify Author: USER" }
+
       author_info = {
         :name_on_paper => author.name,
         :email => author.mail,
         :user_id => author.id, 
-        :institution  => ""
+        :institution  => "",
+        :is_user  => "1"
       }
       
       unless author.ssamr_user_detail.nil?
@@ -24,11 +28,14 @@
     
     else 
       if author.class == Author    
-      author_info = { 
-        :name_on_paper => author.name, 
-        :user_id => author.user_id,
-        :id => author.id
-      }
+        Rails.logger.debug { "Identify Author: AUTHOR" }
+
+        author_info = { 
+          :name_on_paper => author.name, 
+          :user_id => author.user_id,
+          :id => author.id, 
+          :is_user  => "0"
+        }
       end
     end
                 
--- a/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Fri Aug 05 17:44:17 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Mon Aug 08 18:13:30 2011 +0100
@@ -7,28 +7,36 @@
   accepts_nested_attributes_for :author
   accepts_nested_attributes_for :publication
   
+  attr_accessor :is_user, :author_user_id 
+  before_save :associate_author_user
   
-  # setter and getter for virtual attribute :user_id
-  def user_id    
-  end 
-  
-  def user_id=(uid)  
-    if uid.blank?
-      author = Author.new :name => self.name_on_paper
-      author.save!
-      self.author_id = author.id
-    else
-      user = User.find(uid)                         
-      
-      if user.author.nil?      
-        # TODO: should reflect the name_on_paper parameter
+  protected 
+  def associate_author_user 
+    logger.error { "Before Save: associate_author_user" }   
+
+    case self.is_user
+      when "0"
+        author = Author.find(self.author_user_id)
+        self.author_id = author.id        
+      when "1"
+        user = User.find(self.author_user_id)
+        
+        if user.author.nil?
+          author = Author.new :name => self.name_on_paper
+          author.save
+          self.author_id = author.id
+          user.author = author
+          user.save
+        else
+          author = user.author
+          self.author_id = author.id
+        end        
+      else
         author = Author.new :name => self.name_on_paper
-        author.save!
-        user.author = author
-        user.save!
-      else
-        self.author_id = user.author.id        
+        logger.error { "SAVED AUTHOR" }
+        author.save
+        self.author_id = author.id
+        
       end
-    end    
   end
 end
--- a/vendor/plugins/redmine_bibliography/app/models/publication.rb	Fri Aug 05 17:44:17 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/publication.rb	Mon Aug 08 18:13:30 2011 +0100
@@ -26,7 +26,7 @@
     authorships.each_with_index do |authorship, index|
       if authorship.auth_order.nil?
          authorship.auth_order = index
-         authorship.save!
+         authorship.update
       end
     end    
   end
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Fri Aug 05 17:44:17 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Mon Aug 08 18:13:30 2011 +0100
@@ -20,7 +20,8 @@
     <%= f.text_field :email, :size => 60  %><br />
     <em><%= h l("text_author_email") %></em><br />
     
-    <%= f.hidden_field :user_id %>
+    <%= f.hidden_field :is_user %>
+    <%= f.text_field :author_user_id %>
     
     <%= link_to_remove_fields l("remove_author"), f %>    
     
--- a/vendor/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Fri Aug 05 17:44:17 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Mon Aug 08 18:13:30 2011 +0100
@@ -22,29 +22,29 @@
           :name_on_paper => "",
           :email => "",
           :institution => "",
-          :user_id => self.id                    
+          :author_user_id => self.id,
+          :is_user => "1"                    
         }
         
         if self.author.nil?
-          logger.error { "NO AUTHOR HERE" }
+          logger.debug { "Bibliography: The current user has no author associated." }
           info[:name_on_paper] = self.name
           info[:email] = self.mail
           if not self.ssamr_user_detail.nil?
             info[:institution]  = self.ssamr_user_detail.institution_name
           end
         else
-          logger.error { "-----> We've got an author associated with the user." }          
+          logger.error { "Bibliography: We've got an author associated with the current user." }          
           info[:name_on_paper] = self.author.name            
 
           if self.author.authorships.length > 0
             info[:email] = self.author.authorships.first.email
             info[:institution] = self.author.authorships.first.institution
           end
-        end
-        
-        return info
-        
-      end            
+        end        
+        return info        
+      end
+                
     end #InstanceMethods
     
   end #UserPublicationsPatch