changeset 484:3ab94ed89c2f cannam_integration

Merge from branch "feature_36"
author Chris Cannam
date Mon, 11 Jul 2011 16:35:45 +0100
parents 9867ed78c4c1 (current diff) cc267eb99115 (diff)
children 1afe06d9ba94
files vendor/plugins/redmine_bibliography/app/views/publications/add_me_as_author.rjs
diffstat 13 files changed, 195 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -3,17 +3,17 @@
 class PublicationsController < ApplicationController
   unloadable
   
-  before_filter :find_project_by_project_id, :except => [:autocomplete_for_project, :add_author, :sort_authors]
+  before_filter :find_project_by_project_id, :except => [:autocomplete_for_project, :add_author, :sort_authors, :autocomplete_for_author]
   
   
   def new
     @publication = Publication.new      
     
     # we'll always want a new publication to have its bibtex entry
-    @publication.build_bibtex_entry
+    # @publication.build_bibtex_entry
     
     # and at least one author
-    @publication.authorships.build.build_author
+    # @publication.authorships.build.build_author
     
     @project_id = params[:project_id]
     @current_user = User.current
@@ -61,24 +61,6 @@
     end
   end
 
-  def add_me_as_author
-     if (request.xhr?)       
-       if User.current.author.nil?
-         logger.error { "current user has an author" }
-         @author = Author.new(:user_id => User.current)         
-       else
-         logger.error { "current user does not have an author" }
-         @author = User.current.author
-       end                     
-      @authorship = Authorship.create(:author => @author, :publication => @publication)                    
-     else
-       # No?  Then render an action.
-       #render :action => 'view_attribute', :attr => @name
-       logger.error { "ERROR ADD ME AS AUTHOR" }
-     end
-   end
-
-
   def edit    
     @publication = Publication.find(params[:id])
   end
@@ -204,15 +186,29 @@
 
   def autocomplete_for_project
     @publication = Publication.find(params[:id])
-    
-    logger.error "aaaaaaaa"
-    logger.error { @publication.id }
-    
+        
     @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects            
     logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
     render :layout => false
   end
 
+  def autocomplete_for_author
+    @authors = []
+    
+    authors_list = Author.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 \"#{authors_list.size}\" authors and \"#{users_list.size}\""
+    
+    # need to subtract both lists
+    # give priority to the users
+    
+    authors_list.each do |author|
+      @authors << author unless author.user_id.nil?
+    end
+                
+    render :layout => false
+  end
 
   def sort_authors
     params[:authors].each_with_index do |id, index|
@@ -221,6 +217,11 @@
     render :nothing => true
   end
 
+  def identify_author
+    
+  end
+
+
   
   private
    
--- a/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -9,15 +9,58 @@
     s 
   end
   
+  def identify_author(author)
+    if author.class == User
+      author_info = {
+        :name_on_paper => author.name,
+        :user_id => author.id
+      }
+    
+    else 
+      if author.class == Author    
+      author_info = { 
+        :name_on_paper => author.name, 
+        :user_id => author.user_id,
+        :id => author.id
+      }
+      end
+    end
+                
+    link_to_function(author.name, "update_author_info(this," + author_info.to_json + ")")
+  end
+  
+  def choose_author_link(name, authors)
+    s = ''
+    authors.sort.each do |author|
+      s << "#{identify_author author}\n"
+    end
+    s 
+  end
+
   def link_to_remove_fields(name, f)
     f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
   end
-  
+    
   def link_to_add_fields(name, f, association)
     new_object = f.object.class.reflect_on_association(association).klass.new
     fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
       render(association.to_s.singularize + "_fields", :f => builder)
-    end
+    end    
     link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"), { :class => 'icon icon-add', :id => "add_another_author" })
   end  
+
+  def sanitized_object_name(object_name)
+    object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
+  end
+
+  def sanitized_method_name(method_name)
+    method_name.sub(/\?$/, "")
+  end
+
+  def form_tag_id(object_name, method_name)    
+    str = "#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}"
+    str.to_sym
+  end
+
 end
+
--- a/vendor/plugins/redmine_bibliography/app/models/author.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/author.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -3,5 +3,16 @@
   has_many :publications, :through => :authorships
 
   belongs_to :user
+
+  def <=>(author)
+    name.downcase <=> author.name.downcase
+  end
   
+  named_scope :like, lambda {|q| 
+    s = "%#{q.to_s.strip.downcase}%"
+    {:conditions => ["LOWER(name) LIKE :s", {:s => s}],
+     :order => 'name'
+    }
+  }
+
 end
--- a/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/authorship.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -2,4 +2,15 @@
   belongs_to :author
   belongs_to :publication
   
+  accepts_nested_attributes_for :author
+  accepts_nested_attributes_for :publication
+  
+ 
+  # setter and getter for virtual attribute :author search
+  def author_search
+  end 
+  
+  def author_search=(string)
+  end
+  
 end
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.rhtml	Mon Jul 11 16:35:45 2011 +0100
@@ -1,12 +1,38 @@
-<div class="fields">
-  <p>
+<div id="authors" class="fields">
+  <p>        
     <%= f.label :name_on_paper, l("name") %>
     <%= f.text_field :name_on_paper %><br />
-    <em><%= h l("text_name_on_paper") %></em><br />
+    <em><%= h l("text_author_name_on_paper") %></em><br />
+                                                
+    <legend><%= "Identify Authors in the system…" %></legend>
+
+    <%= link_to_function "Add Me as Author", "update_author_info(this," + User.current.to_json + ")", :class => 'icon icon-add', :id => "add_me_as_author" %>
+
+    <p>
+      <%= f.label :author_search, l(:label_project_search) %>
+      <%= f.text_field :author_search %>
+    </p>
+
+    <%= observe_field( form_tag_id(f.object_name, :author_search), :frequency => 0.5, :update => form_tag_id(f.object_name, :identify_author), :url => { :controller => 'publications', :action => 'autocomplete_for_author' }, :with => 'q')
+    %>
+    
+    <div id="<%= form_tag_id(f.object_name, :identify_author) %>">
+      <% if params[:q] && params[:q].length > 1 %>
+        <%= select_author_links 'author[author_ids][]', @authors %>
+      <% end %>
+    </div>
+      
     <%= f.label :institution, l("institution") %>
     <%= f.text_field :institution %><br />
-    <em><%= h l("text_institution") %></em><br />
-    <%= f.hidden_field :_destroy %>
-    <%= link_to_remove_fields l("remove_author"), f %>
+    <em><%= h l("text_author_institution") %></em><br />
+
+    <%= f.label :email, l("email") %>
+    <%= f.text_field :email %><br />
+    <em><%= h l("text_author_email") %></em><br />
+    
+    <%= f.hidden_field :author_id %>
+    
+    <%= link_to_remove_fields l("remove_author"), f %>    
+    
   </p>
 </div>
\ No newline at end of file
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb	Mon Jul 11 16:35:45 2011 +0100
@@ -2,7 +2,6 @@
     <%= javascript_include_tag 'authors', :plugin => 'redmine_bibliography' %>
 <% end %>
 
-
 <% form_for @publication, :url => { :project_id  => @project_id, :action => :create } do |f| -%>
   <%= f.error_messages %>  
   <p>
@@ -14,23 +13,13 @@
   <% f.fields_for :authorships do |builder| %>
     <%= render "authorship_fields", :f => builder %>
   <% end %>
+
   <p><%= link_to_add_fields l(:label_add_another_author), f, :authorships %></p>
       
-  <%= link_to_remote l(:label_add_me_as_author), 
-                     { :url => { :controller => 'publications', :action => 'add_me_as_author', :project_id => @project },
-                       :method => 'post',
-                       :update => 'me',
-                       :complete => ""
-                     }, { :class => 'icon icon-add', :id => "add_me_as_author" } %>
-
-  <div id="me"></div>
-    
   <h3>Other Details</h3>
   <% f.fields_for :bibtex_entry do |builder| -%>
     <%= render :partial => 'bibtex_fields', :locals => { :f => builder}  %>
   <%- end -%>
 
-
   <%= f.submit %>
-
 <% end -%>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_identify_author_form.html.erb	Mon Jul 11 16:35:45 2011 +0100
@@ -0,0 +1,25 @@
+<legend><%= "Identify Authors in the system…" %></legend>
+
+<%= link_to_remote "It's me!", 
+   { :url => { :controller => 'publications', 
+     :action => 'add_me_as_author',
+     :project_id => @project }, :method => 'post'}, 
+   { :class => 'icon icon-add', :id => "add_me_as_author" } %>
+
+<p>
+  <%= label_tag "author_search", l(:label_project_search) %><%= text_field_tag 'author_search', nil %>
+</p>
+	     
+<%= observe_field( form_tag_id(f.object_name, :name),
+     :frequency => 0.5,
+     :update => :identify_author,
+     :url => { :controller => 'publications', :action => 'autocomplete_for_author' },
+     :with => 'q')
+%>
+
+<div id="identify_author">
+  <% if params[:q] && params[:q].length > 1 %>
+    <%= select_author_links 'author[author_ids][]', @authors %>
+  <% end %>
+</div>
+
--- a/vendor/plugins/redmine_bibliography/app/views/publications/add_me_as_author.rjs	Mon Jun 27 12:38:09 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-page.insert_html :bottom, :authors, :partial => 'authorships_fields', :locals => { :authorships => @authorship }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/autocomplete_for_author.rhtml	Mon Jul 11 16:35:45 2011 +0100
@@ -0,0 +1,3 @@
+<% if params[:q] && params[:q].length > 1 %>
+	<%= choose_author_link 'publication[author_ids][]', @authors %>
+<% end %>
--- a/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Mon Jul 11 16:35:45 2011 +0100
@@ -9,4 +9,13 @@
   $(link).up().insert({
     before: content.replace(regexp, new_id)
   });
+}
+
+function update_author_info(link, author_info){
+	$(link).up('div').up('div').select('input[id^=publication_authorships_attributes]').each(
+		function(e){
+			key = e.name.split("[").last().trim().sub(']','');			
+			e.value = author_info[key];
+		}		
+	)
 }
\ No newline at end of file
--- a/vendor/plugins/redmine_bibliography/config/locales/en.yml	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/config/locales/en.yml	Mon Jul 11 16:35:45 2011 +0100
@@ -15,13 +15,14 @@
   label_add_project_to_publication: "Add Project to Publication"
   label_project_search: "Search by name: "
   
-  text_institution: "Please insert the institution as it appears on the paper."
-  text_name_on_paper: "Please insert the name of the author as it appears on the paper."
+  text_author_email: "Author's email address as on the paper."
+  text_author_institution: "Author's institution name as on paper."
+  text_author_name_on_paper: "Name of the author as it appears on paper."
   
   # authorships model
   institution: "Institution"
-  naem_on_paper: "Name" 
-  
+  name_on_paper: "Name" 
+  email: "Email Address"
       
   # bibtex_entries model
   id: "id" 
--- a/vendor/plugins/redmine_bibliography/init.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/init.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -12,8 +12,8 @@
     Project.send(:include, Bibliography::ProjectPublicationsPatch)
   end
 
-  unless Project.included_modules.include? Bibliography::UserAuthorPatch
-    Project.send(:include, Bibliography::UserAuthorPatch)
+  unless User.included_modules.include? Bibliography::UserAuthorPatch
+    User.send(:include, Bibliography::UserAuthorPatch)
   end
 
 end
--- a/vendor/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Mon Jun 27 12:38:09 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/lib/bibliography/user_author_patch.rb	Mon Jul 11 16:35:45 2011 +0100
@@ -1,11 +1,32 @@
 require_dependency 'user'
 
 module Bibliography
-  module UserAuthorPatch
+  module UserAuthorPatch    
     def self.included(base)
-          base.class_eval do
-            has_one :publication
-          end
+      base.send(:include, InstanceMethods) 
+      extend ClassMethods     
+          
+      base.class_eval do
+        has_one :publication            
+      end
     end #self.included
+    
+    module ClassMethods
+    end  
+    
+    module InstanceMethods
+      def get_author_name
+        if self.author 
+          self.author.name
+        else
+          "No Name"
+        end
+      end
+      
+      def get_author_info
+        
+      end            
+    end #InstanceMethods
+    
   end #UserPublicationsPatch
 end #RedmineBibliography
\ No newline at end of file