changeset 601:1608b3cb50cd feature_36

Fixed User and Authorships Search Query. New Publication: reordered some fields in the form; now correctly saving all authors.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 17 Aug 2011 15:50:25 +0100
parents c3c1091639ad
children e48cb6081076
files vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb vendor/plugins/redmine_bibliography/app/models/authorship.rb vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml vendor/plugins/redmine_bibliography/assets/javascripts/authors.js vendor/plugins/redmine_bibliography/config/locales/en.yml
diffstat 5 files changed, 95 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Aug 16 17:38:30 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Wed Aug 17 15:50:25 2011 +0100
@@ -197,45 +197,34 @@
     render :layout => false
   end
 
-  def autocomplete_for_author
-    #TODO: luisf. optimize this Query: possible?
-    
+  def autocomplete_for_author    
     @results = []
     
     object_id = params[:object_id]
     @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
-    
-    logger.error { "OBJECT NAME #{@object_name}" }
-    
-    authorships_list = Authorship.like(params[:q]).find(:all, :limit => 100)
+        
+    authorships_list = Authorship.like_unique(params[:q]).find(:all, :limit => 100)
     users_list = User.active.like(params[:q]).find(:all, :limit => 100)
 
     logger.debug "Query for \"#{params[:q]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
     
-    authorships_list.each do |authorship|
-      # NEED TO COMPARE AUTHORSHIPS FOR SIMILAR NAME/INST/EMAIL              
-      @results << authorship
-      logger.error { "Added AUTHORSHIP #{authorship.id} to the results list" }
+    @results = users_list
+
+    # TODO: can be optimized…    
+    authorships_list.each do |authorship|      
+      flag = true
+      
+      users_list.each do |user|
+        if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
+          Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
+          flag = false
+          break
+        end
+      end
+
+      @results << authorship if flag
     end
 
-    # need to subtract both lists; give priority to the users    
-    flag = true
-    users_list.each do |user|      
-      if user.author.nil?
-        @results << user
-      else
-        user.author.authorships.each do |auth|
-          if authorships_list.include?(auth)
-            flag = false
-            break
-          end
-        end  
-        @results << user unless flag
-      end
-    end
-
-#    @results.uniq!
-    
     render :layout => false    
   end
   
@@ -261,9 +250,9 @@
           page[institution_field].value = item.institution
 
           page[yes_radio].checked = true
-          page[name_field].disabled = true
-          page[email_field].disabled = true
-          page[institution_field].disabled = true
+          page[name_field].readOnly = true
+          page[email_field].readOnly = true
+          page[institution_field].readOnly = true
         }
       }
     end
--- a/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Tue Aug 16 17:38:30 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Wed Aug 17 15:50:25 2011 +0100
@@ -10,9 +10,17 @@
   attr_accessor :is_user, :author_user_id, :search_name, :identify_author, :search_results
   before_save :associate_author_user
 
+  named_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"
+    }
+  }
+
   named_scope :like, lambda {|q| 
     s = "%#{q.to_s.strip.downcase}%"
-    {:conditions => ["LOWER(name_on_paper) LIKE :s", {:s => s}],
+    {:conditions => ["LOWER(name_on_paper) LIKE :s OR LOWER(email) LIKE :s", {:s => s}],
      :order => 'name_on_paper'
     }
   }
@@ -31,6 +39,31 @@
   
   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
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Tue Aug 16 17:38:30 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Wed Aug 17 15:50:25 2011 +0100
@@ -1,46 +1,39 @@
 <div id="authors" class="fields">
+  <h4><%= l("label_author_1") %></h4>
+  <small>(<%= link_to l(:label_search_existing_author), {}, :onclick => 'Effect.toggle("' + form_tag_id( f.object_name, :search_author ).to_s + '", "appear", {duration:0.3}); return false;' %>)</small>
 
-  <h4><%= l("label_author_1") %></h4>
+  <div id="<%= form_tag_id( f.object_name, :search_author ) %>">
+    <div class="splitcontentleft">
+      <p>
+        <%= f.text_field :search_name, :size => 25 %>
+        <%= observe_field( form_tag_id(f.object_name, :search_name), :frequency => 0.5, :update => form_tag_id( f.object_name, :search_results), :url => { :controller => 'publications', :action => 'autocomplete_for_author', :object_name => form_object_id(f.object_name)  },  :with => 'q' ) %>
+      </p>
+      <%# link_to_function l(:label_author_is_me), "update_author_info(this," + User.current.get_author_info.to_json + ")", :id => "add_me_as_author" %>
 
-  <p>
-	<%= f.text_field :search_name, :size => 25 %>
-	
-	<%= observe_field( form_tag_id(f.object_name, :search_name), :frequency => 0.5, :update => form_tag_id( f.object_name, :search_results), :url => { :controller => 'publications', :action => 'autocomplete_for_author', :object_name => form_object_id(f.object_name)  },  :with => 'q' ) %>
-	 
-	<%= link_to_function l(:label_author_is_me), "update_author_info(this," + User.current.get_author_info.to_json + ")", :id => "add_me_as_author" %>
-	<br />
-	<em><%= h l("text_author_search") %></em>
-  </p>
-
-  <p>
-    <div id="<%= form_tag_id( f.object_name, :identify_author ) %>">
+      <h5>Is it this author?</h5>
       <p>
-        <%= f.select :search_results, options_for_select(@author_options), {}, {:size => 5, 
-          :onChange => remote_function( :url => { :controller => :publications, :action => :get_user_info, :object_id => form_object_id(f.object_name) }, :with => "'value=' + 
-          value" )} %>
-      </p>
-        <h5>Is it this author?</h5>
-      <p>
-	      <%= f.radio_button :identify_author, "yes", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_yes %><br />
-	      <%= f.radio_button :identify_author, "correct", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_correct %><br />
-	      <%= f.radio_button :identify_author, "no", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_no %><br />
+        <%= f.radio_button :identify_author, "yes", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_yes %><br />
+        <%= f.radio_button :identify_author, "correct", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_correct %><br />
+        <%= f.radio_button :identify_author, "no", :onClick => "identify_author_status($(this).value, #{form_object_id(f.object_name) });" %><%= f.label :identify_author_no %>
       </p>
     </div>	
-  </p>
 
-  <p>        
-    <%= f.text_field :name_on_paper, :size => 25 %> 
-    <em><%= h l("text_author_name_on_paper") %></em><br />
-      
+    <div class="splitcontentright">       
+      <%= f.select :search_results, options_for_select(@author_options), {}, {:size => 5, 
+        :onChange => remote_function( :url => { :controller => :publications, :action => :get_user_info, :object_id => form_object_id(f.object_name) }, :with => "'value=' + 
+        value" )} %>
+    </div>	
+  </div>
+  
+  <div class="box">
     <p>
-    <%= f.text_field :institution, :size => 35 %><br />
-    <em><%= h l("text_author_institution") %></em><br />
-
-    <%= f.text_field :email, :size => 35  %><br />
-    <em><%= h l("text_author_email") %></em><br />
-    
-    <%= link_to_remove_fields l("remove_author"), f %>    
+      <%= f.text_field :name_on_paper, :size => 25 %><br />
+      <em><%= h l("text_author_name_on_paper") %></em><br />
+      <%= f.text_field :institution, :size => 35 %><br />
+      <em><%= h l("text_author_institution") %></em><br />
+      <%= f.text_field :email, :size => 35  %><br />
+      <em><%= h l("text_author_email") %></em><br />
     </p>
-    
-  </p>
+  </div>
+  <%= link_to_remove_fields l("remove_author"), f %>
 </div>
--- a/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Tue Aug 16 17:38:30 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Wed Aug 17 15:50:25 2011 +0100
@@ -19,24 +19,24 @@
 	switch(status)
 	{
 		case "yes":
-			name_field.disabled = true;
-			email_field.disabled = true;
-			institution_field.disabled = true;
+			name_field.readOnly = true;
+			email_field.readOnly = true;
+			institution_field.readOnly = true;
 		break;
 		case "no":
 			name_field.value = "";
 			email_field.value = "";
 			institution_field.value = "";
 		
-			name_field.disabled = false;
-			email_field.disabled = false;
-			institution_field.disabled = false;
+			name_field.readOnly = false;
+			email_field.readOnly = false;
+			institution_field.readOnly = false;
 
 		break;
 		case "correct":
-			name_field.disabled = false;
-			email_field.disabled = false;
-			institution_field.disabled = false;
+			name_field.readOnly = false;
+			email_field.readOnly = false;
+			institution_field.readOnly = false;
 		break;
 	}
 }
--- a/vendor/plugins/redmine_bibliography/config/locales/en.yml	Tue Aug 16 17:38:30 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/config/locales/en.yml	Wed Aug 17 15:50:25 2011 +0100
@@ -29,6 +29,8 @@
   label_add_another_author: "Add another author"
   field_search_name: "Search author"
   field_search_results: "Authors"
+  label_search_existing_author: "Search Author"
+  label_author_information: "Author Information"
 
   remove_author: "Remove this author"