changeset 1094:9b7f99b7cb14 bibplugin_bibtex

cleaned some code, fixes a couple of issues.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 28 Nov 2012 19:32:31 +0000
parents 294545c0282d
children 116346b9cab4
files vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.html.erb vendor/plugins/redmine_bibliography/app/views/publications/_suggest_author.html.erb vendor/plugins/redmine_bibliography/app/views/publications/new.html.erb
diffstat 5 files changed, 69 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Nov 27 17:39:01 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Wed Nov 28 19:32:31 2012 +0000
@@ -62,39 +62,6 @@
   end
 
 
-#      respond_to do |format|
-#        format.js {
-#          render(:update) {|page|
-#            flash.now[:notice] = "Correctly parsed BibTeX entry"
-#
-#            bibtex_entry_no = BibtexEntryType.find_by_name(bib[0].type.to_s).id
-#            page["publication_title"].value = bib[0][:title]
-#            page["publication_bibtex_entry_attributes_entry_type"].value = #bibtex_entry_no
-#
-#            BibtexEntryType.fields(bibtex_entry_no).each do |field|
-#              page["publication_bibtex_entry_attributes_#{field}"].value = bib[0][field#]
-#            end
-#
-#            # for each author simulates a click and fills the author info
-##            bib[0].authors.each do |author|
-##              page["add_another_author"].click
-##              page.alert(bib[0].authors.length)
-##              page.alert(page["authors"].first.id)
-##            end
-#
-#
-#
-#          }
-#        }
-#      end
-
-#    rescue BibtexParsingError => e
-#      logger.error { "Bibtex Parsing Error #{bib.errors}" }
-
-#    end
-
-
-
   def create
     @project = Project.find(params[:project_id])
 
@@ -123,20 +90,8 @@
     end
   end
 
-  def new_from_bibfile
-    @publication.current_step = session[:publication_step]
-
-    # contents of the paste text area
-    bibtex_entry = params[:bibtex_entry]
-
-    # method for creating "pasted" bibtex entries
-    if bibtex_entry
-      parse_bibtex_list bibtex_entry
-    end
-  end
 
   def get_bibtex_required_fields
-
     fields = BibtexEntryType.fields(params[:q])
 
     respond_to do |format|
@@ -153,6 +108,7 @@
     end
   end
 
+
   def add_author
     if (request.xhr?)
       render :text => User.find(params[:user_id]).name
@@ -163,6 +119,7 @@
     end
   end
 
+
   def edit
     find_project_by_project_id unless params[:project_id].nil?
 
@@ -215,21 +172,6 @@
     return authors_entry.split(" and ")
   end
 
-  # parses a list of bibtex
-  def parse_bibtex_list(bibtex_list)
-    bibliography = BibTeX.parse bibtex_list
-
-    no_entries = bibliography.data.length
-
-    # parses the bibtex entries
-    bibliography.data.map do |d|
-
-      if d.class == BibTeX::Entry
-        create_bibtex_entry d
-      end
-    end
-  end
-
   def create_bibtex_entry(d)
     @publication = Publication.new
     @bentry = BibtexEntry.new
@@ -258,23 +200,22 @@
     # what is this for???
     # @created_publications << @publication.id
 
-    # need to save all authors
-    #   and establish the author-publication association
-    #   via the authorships table
+    # Saves all authors, creating the author-publication association via the authorships
     authors.each_with_index.map do |authorname, idx|
       author = Author.new(:name => authorname)
       if author.save!
+        author.authorships.create!(
+          :publication => @publication,
+          :institution => institution,
+          :email => email,
+          :order => idx)
+
         # todo: catch the errors...
-        puts "SAVED"
+        logger.info { "Author #{author.name} correctly created." }
       else
-        puts "NOT SAVED"
+        logger.error { "Error: author #{authorname} not correctly saved when creating publication with ID=#{@publication.id}." }
+
       end
-
-      author.authorships.create!(
-      :publication => @publication,
-      :institution => institution,
-      :email => email,
-      :order => idx)
     end
   end
 
@@ -286,16 +227,24 @@
     render :layout => false
   end
 
-  # returns a list of authors and users
-  def suggest_authors(author)
-    firstname = author.first
-    lastname = author.last
+  # returns a list of authors
+  def suggest_authors(authorname)
+    firstname = authorname.first
+    lastname = authorname.last
 
     # todo: improve name searching algorithm -- lf.20121127
-    authorships = Authorship.like(lastname).find(:all, :limit => 100).count
-    users = User.like(lastname).find(:all, :limit => 100).count
+    authorships = Authorship.like(lastname).find(:all, :limit => 100)
 
-    # todo: finish implementing. Use same code as in autocomplete_for_author
+    logger.error { "Authorships #{authorships}<-" }
+
+    unless authorships.empty?
+      authors = authorships.collect {|a| a.author}
+      authors.uniq!
+
+      # @users is a list of suggested users
+      # authors = authors.reject { |a| @users.include?(a.user) }
+    end
+
   end
 
   def autocomplete_for_author
@@ -304,29 +253,17 @@
     object_id = params[:object_id]
     @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
 
-    # cc 20110909 -- revert to like instead of like_unique -- see #289
-    authorships_list = Authorship.like(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 = Authorship.like(params[:q]).find(:all, :limit => 100)
 
-    @results = users_list
+    # list with authorships that are associated with users
+    authorships_with_users = authorships_list.reject { |a| a.author.user.nil? }
 
-    # todo: can be optimized…
-    authorships_list.each do |authorship|
-      flag = true
-
-      # todo: refactor this code using select -- lf.20121127
-      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
+    # authorships not associated with a user
+    orphan_authorships = authorships_list - authorships_with_users
+    authorships_with_users.map! { |a| a.author.user }
+    @results = (users_list + authorships_with_users).uniq! + orphan_authorships
 
     render :layout => false
   end
--- a/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Tue Nov 27 17:39:01 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Wed Nov 28 19:32:31 2012 +0000
@@ -33,17 +33,25 @@
     # creates the select list based on the results array
     # results is an array with both Users and Authorships objects
 
-    @author_options = []
-    @results.each do |result|
+   if @results.size > 0
+    author_options = @results.map do |result|
+      logger.error { "DEBUG --> #{result}" }
+
       email_bit = result.mail.partition('@')[2]
-      if email_bit != "":
-          email_bit = "(@#{email_bit})"
-      end
-      @author_options << ["#{result.name} #{email_bit}", "#{result.class.to_s}_#{result.id.to_s}"]
+      email_bit = "(@#{email_bit})" unless email_bit = ""
+
+      ["#{result.name} #{email_bit}", "#{result.class.to_s}_#{result.id.to_s}"]
     end
 
-   if @results.size > 0
-     s = select_tag( form_tag_name(object_name, :author_search_results), options_for_select(@author_options), { :id => form_tag_id(object_name, :author_search_results), :size => 3} )
+#    @results.each do |result|
+#      email_bit = result.mail.partition('@')[2]
+#      if email_bit != "":
+#          email_bit = "(@#{email_bit})"
+#      end
+#      @author_options << ["#{result.name} #{email_bit}", "#{result.class.to_s}_#{#result.id.to_s}"]
+#    end
+
+     s = select_tag( form_tag_name(object_name, :author_search_results), options_for_select(author_options), { :id => form_tag_id(object_name, :author_search_results), :size => 3} )
      s << observe_field( form_tag_id(object_name, :author_search_results), :on => 'click', :function => "alert('Element changed')", :with => 'q')
    else
      s = "<em>No Authors found that match your search… sorry!</em>"
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.html.erb	Tue Nov 27 17:39:01 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.html.erb	Wed Nov 28 19:32:31 2012 +0000
@@ -1,27 +1,32 @@
-<fieldset> 
+<fieldset>
   <div id="<%= form_tag_id( f.object_name, :search_author ) %>" style=<%= "display:none;" unless params[:action] == "new" %> >
       <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' ) %>
+        <%= 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=' + encodeURIComponent(value)" %>
       </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.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=' + 
+      <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>  
+	    </p>
 
       <p style="margin-bottom: -2.5em; padding-bottom; 0"><label><%= l(:identify_author_question) %></label></p>
       <p class="author_identify">
         <label class='inline'><%= radio_button_tag(:identify_author, "yes", false, :name => form_tag_name(f.object_name,:identify_author ), :id => form_tag_id( f.object_name, :identify_author_yes ), :onchange => "identify_author_status($(this).value, #{form_object_id(f.object_name) });") %> <%= l(:identify_author_yes) %> </label><br />
-       
+
         <label class='inline'><%= radio_button_tag(:identify_author, "correct", false, :name => form_tag_name(f.object_name,:identify_author ), :id => form_tag_id( f.object_name, :identify_author_corrections ), :onchange => "identify_author_status($(this).value, #{form_object_id(f.object_name) });") %> <%= l(:identify_author_correct) %> </label><br />
-        
+
         <label class='inline'><%= radio_button_tag(:identify_author, "no", true, :name => form_tag_name(f.object_name,:identify_author ), :id => form_tag_id( f.object_name, :identify_author_no ), :onchange => "identify_author_status($(this).value, #{form_object_id(f.object_name) });") %> <%= l(:identify_author_no) %> </label><br />
       </p>
-  </div>	
-  
+  </div>
+
   <div class='author_edit' id="<%= form_tag_id( f.object_name, :edit_author_info ) %>">
     <p>
       <%= f.text_field :name_on_paper, {:class => ("readonly" unless params[:action] == "new") } %></p>
@@ -32,10 +37,10 @@
       <p class='description' style=<%= "display:none;" unless params[:action] == "new" %>><%= h l("text_author_email") %></p>
     </p>
   </div>
-  
+
   <div class="box" id="<%= form_tag_id( f.object_name, :show_author_info ) %>" style="display: none">
   </div>
-  
+
   <p>
     <%- if params[:action] == 'new' -%>
       <%= button_to_function l(:label_save_author), {}, { :onclick => "toggle_save_author(#{form_object_id(f.object_name)}); return false;", :id => form_tag_id( f.object_name, :edit_save_button )} %>
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_suggest_author.html.erb	Tue Nov 27 17:39:01 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_suggest_author.html.erb	Wed Nov 28 19:32:31 2012 +0000
@@ -1,6 +1,5 @@
 
 <p>
-  Author <%= h author[0] %>
-
+  Author: <%= h(author[0]) -%>
 </p>
 
--- a/vendor/plugins/redmine_bibliography/app/views/publications/new.html.erb	Tue Nov 27 17:39:01 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/new.html.erb	Wed Nov 28 19:32:31 2012 +0000
@@ -4,6 +4,7 @@
 <% end %>
 
 <%= error_messages_for 'publication' %>
+<div id="ajax-indicator" style="display:none;"><span><%= l(:label_loading) %></span></div>
 
 <h2><%=l(:label_publication_new)%></h2>