changeset 1068:e11d8d13ebc5 bibplugin_cache

Added local caching to the publications helper; patch to the projects controller to load the publications helper; added patch to the plugin init file
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 20 Nov 2012 16:55:41 +0000
parents 2ad2f9ab46a6
children 734fe0c6b3e4
files vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb vendor/plugins/redmine_bibliography/app/models/publication.rb vendor/plugins/redmine_bibliography/app/views/projects/_bibliography_box.html.erb vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb vendor/plugins/redmine_bibliography/init.rb
diffstat 5 files changed, 81 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Tue Nov 20 16:55:13 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Tue Nov 20 16:55:41 2012 +0000
@@ -12,20 +12,20 @@
   def projects_check_box_tags(name, projects)
     s = ''
     projects.sort.each do |project|
-      if User.current.allowed_to?(:edit_publication, project) 
+      if User.current.allowed_to?(:edit_publication, project)
         s << "<label>#{ check_box_tag name, project.id, false } #{link_to_project project}</label>\n"
         s << '<br />'
       end
     end
 
-    s 
+    s
   end
-  
+
   def choose_author_link(object_name, items)
     # called by autocomplete_for_author (publications' action/view)
     # 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|
       email_bit = result.mail.partition('@')[2]
@@ -34,26 +34,26 @@
       end
       @author_options << ["#{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} )
      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>"
-   end      
+   end
   end
 
   def link_to_remove_fields(name, f)
     f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", :class => 'icon icon-del')
   end
-    
+
   def link_to_add_author_fields(name, f, association, action)
     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_author_fields(this, '#{association}', '#{escape_javascript(fields)}', '#{action}')"), { :class => 'icon icon-add', :id => "add_another_author" })
-  end  
+  end
 
   def sanitized_object_name(object_name)
     object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
@@ -62,75 +62,77 @@
   def sanitized_method_name(method_name)
     method_name.sub(/\?$/, "")
   end
-  
+
   def form_tag_name(object_name, method_name)
       str = "#{object_name.to_s}[#{sanitized_method_name(method_name.to_s)}]"
-      str.to_sym 
+      str.to_sym
   end
-  
-  def form_tag_id(object_name, method_name)    
+
+  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
-  
+
   def form_object_id(object_name)
     str = object_name.split("\[").last().gsub("\]","")
     str.to_sym
   end
 
-  def render_authorships_list(publication)   
+  def render_authorships_list(publication)
     s = '<p>'
-    
+
     publication.authorships.each do |authorship|
       s << link_to_authorship(authorship)
       s << "<br /><em>#{authorship.institution}</em></p>"
-    end    
+    end
 
-    s   
+    s
   end
-  
-  def render_projects_list(publication, show_delete_icon)    
+
+  def render_projects_list(publication, show_delete_icon)
     s= ""
-    
+
     publication.projects.visible.each do |proj|
       s << link_to_project(proj, {}, :class => 'publication_project')
-    
-      if show_delete_icon  
+
+      if show_delete_icon
         if User.current.allowed_to?(:edit_publication, @project)
           if @project == proj
             # todo: move this message to yml file
             confirm_msg = 'Are you sure you want to remove the current project from this publication\'s projects list?'
           else
             confirm_msg = false
-          end 
-          
-          s << link_to_remote(l(:button_delete), { :url => { :controller => 'publications', :action => 'remove_project', :id => publication, :remove_project_id => proj,  :project_id => @project }, :method => :post, :confirm => confirm_msg }, :class => 'icon icon-del') 
+          end
+
+          s << link_to_remote(l(:button_delete), { :url => { :controller => 'publications', :action => 'remove_project', :id => publication, :remove_project_id => proj,  :project_id => @project }, :method => :post, :confirm => confirm_msg }, :class => 'icon icon-del')
         end
       end
-      
-      s << "<br />"      
-    end    
 
-    s  
+      s << "<br />"
+    end
+
+    s
   end
-  
-  def show_cite_proc_entry(publication)
-    # code that should be moved either to the model or to the controller?
-    
-    publication.print_entry(:ieee)
+
+  def print_ieee_format(publication)
+    Rails.cache.fetch("publication-#{publication.id}-ieee") do
+      publication.print_entry(:ieee)
+    end
   end
-  
-  def print_bibtex_entry(publication)
-    publication.print_entry(:bibtex)
+
+  def print_bibtex_format(publication)
+    Rails.cache.fetch("publication-#{publication.id}-bibtex") do
+      publication.print_entry(:bibtex)
+    end
   end
-    
-  
+
+
   def show_bibtex_fields(bibtex_entry)
     s = ""
     bibtex_entry.attributes.keys.sort.each do |key|
       value = bibtex_entry.attributes[key].to_s
       next if key == 'id' or key == 'publication_id' or value == ""
-      s << "<h4>" + l("field_#{key}") + "</h4>" 
+      s << "<h4>" + l("field_#{key}") + "</h4>"
       s << "<p>"
       if key == "entry_type"
         s << bibtex_entry.entry_type_label
@@ -140,6 +142,6 @@
       s << "</p>"
     end
     s
-  end 
+  end
 end
 
--- a/vendor/plugins/redmine_bibliography/app/models/publication.rb	Tue Nov 20 16:55:13 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/models/publication.rb	Tue Nov 20 16:55:41 2012 +0000
@@ -2,10 +2,10 @@
 
 class Publication < ActiveRecord::Base
   unloadable
-  
+
   has_many :authorships, :dependent => :destroy, :order => "auth_order ASC"
   has_many :authors, :through => :authorships, :uniq => true
-  
+
   has_one :bibtex_entry, :dependent => :destroy
 
   validates_presence_of :title
@@ -14,9 +14,9 @@
   accepts_nested_attributes_for :authorships
   accepts_nested_attributes_for :authors, :allow_destroy => true
   accepts_nested_attributes_for :bibtex_entry, :allow_destroy => true
-  
+
   has_and_belongs_to_many :projects, :uniq => true
-  
+
   before_save :set_initial_author_order
 
   # Ensure error message uses proper text instead of
@@ -30,62 +30,62 @@
     end
   end
 
-  def notify_authors_publication_added(project)  
+  def notify_authors_publication_added(project)
     self.authors.each do |author|
       Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
       Mailer.deliver_publication_added(author.user, self, project) unless author.user.nil?
     end
   end
-  
-  def notify_authors_publication_updated(project)  
+
+  def notify_authors_publication_updated(project)
     self.authors.each do |author|
       Rails.logger.debug { "Sending mail to \"#{self.title}\" publication authors." }
       Mailer.deliver_publication_updated(author.user, self, project) unless author.user.nil?
     end
   end
-  
-  
+
+
   def set_initial_author_order
     authorships = self.authorships
-    
+
     logger.debug { "Publication \"#{self.title}\" has #{authorships.size} authors." }
-    
+
     authorships.each_with_index do |authorship, index|
       if authorship.auth_order.nil?
          authorship.auth_order = index
       end
-    end    
+    end
   end
-  
+
   def print_bibtex_author_names
-    # this authors are correctly sorted because the authorships model 
+    # this authors are correctly sorted because the authorships model
     # already outputs the author names ASC by auth_order
     self.authorships.map{|a| a.name_on_paper}.join(' and ')
-  end  
-  
+  end
+
   def print_entry(style)
     bib = BibTeX::Entry.new
 
     bib.author = self.print_bibtex_author_names
     bib.title = self.title
 
-    self.bibtex_entry.attributes.keys.sort.each do |key|      
+    self.bibtex_entry.attributes.keys.sort.each do |key|
       value = self.bibtex_entry.attributes[key].to_s
       next if key == 'id' or key == 'publication_id' or value == ""
 
-      if key == "entry_type"        
+      if key == "entry_type"
         bib.type = BibtexEntryType.find(self.bibtex_entry.entry_type).name
       else
         bib[key.to_sym] = value
-      end               
+      end
     end
-    
+
     if style == :ieee
-      CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html      
-    else 
+      CiteProc.process bib.to_citeproc, :style => :ieee, :format => :html
+    else
       bibtex = bib.to_s :include => :meta_content
       bibtex.strip!
       logger.error { bibtex }
-    end  
+    end
   end
 end
--- a/vendor/plugins/redmine_bibliography/app/views/projects/_bibliography_box.html.erb	Tue Nov 20 16:55:13 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/projects/_bibliography_box.html.erb	Tue Nov 20 16:55:41 2012 +0000
@@ -9,18 +9,18 @@
     <div class="box">
     <h3><%=l(:label_related_publication_plural)%></h3>
 
-     <dl>       
-     <% @project.publications.each do |publication| %>       
+     <dl>
+     <% @project.publications.each do |publication| %>
        <dt>
-          <%= publication.print_entry(:ieee) -%>
+          <%= print_ieee_format(publication) %>
        </dt>
        <dd>
-         <%= link_to("[More Details]", {:controller => :publications, :action => :show, :id => publication.id, :project_id => @project.id}) -%> 
-         
-         <%= link_to_function "[B<small>IB</small>T<sub>E</sub>X]", onclick="toggleBibtex(this)" -%> 
+         <%= link_to("[More Details]", {:controller => :publications, :action => :show, :id => publication.id, :project_id => @project.id}) -%>
+
+         <%= link_to_function "[B<small>IB</small>T<sub>E</sub>X]", onclick="toggleBibtex(this)" -%>
        </dd>
        <dd class="bibtex-textarea collapsed" style="display: none;">
-         <textarea readonly><%= publication.print_entry(:bibtex) -%></textarea>         
+         <textarea readonly> <%= print_bibtex_format(publication) %> </textarea>
        </dd>
    <% end -%>
      </dl>
--- a/vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb	Tue Nov 20 16:55:13 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb	Tue Nov 20 16:55:41 2012 +0000
@@ -2,10 +2,10 @@
 
 <div class="box">
   <h3>Publication Info</h3>
-  <p><%= show_cite_proc_entry(@publication)%></p>
+  <p><%= print_ieee_format(@publication)%></p>
 
   <h3>B<small>IB</small>T<sub>E</sub>X Format</h3>
-  <pre><%=h print_bibtex_entry(@publication) %></pre>
+  <pre><%=h print_bibtex_format(@publication) %></pre>
 </div>
 
 <div class="box">
--- a/vendor/plugins/redmine_bibliography/init.rb	Tue Nov 20 16:55:13 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/init.rb	Tue Nov 20 16:55:41 2012 +0000
@@ -5,7 +5,7 @@
 require 'citeproc'
 
 
-RAILS_DEFAULT_LOGGER.info 'Starting Bibliography Plugin for RedMine'
+RAILS_DEFAULT_LOGGER.info 'Starting Bibliography Plugin for Redmine'
 
 # Patches to the Redmine core.
 Dispatcher.to_prepare :redmine_model_dependencies do
@@ -25,6 +25,9 @@
     Mailer.send(:include, Bibliography::MailerPatch)
   end
 
+  unless ProjectsController.included_modules.include?(Bibliography::ProjectsControllerPatch)
+   ProjectsController.send(:include, Bibliography::ProjectsControllerPatch)
+  end
 
 end
 
@@ -51,5 +54,5 @@
   # extending the Project Menu
   menu :project_menu, :publications, { :controller => 'publications', :action => 'index', :path => nil }, :after => :activity, :param => :project_id, :caption => Proc.new { Setting.plugin_redmine_bibliography['menu'] },
    :if => Proc.new { !Setting.plugin_redmine_bibliography['menu'].blank? }
-    
+
 end