changeset 468:0bb9c7baed07 feature_36

dynamically adding/removing users.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Thu, 23 Jun 2011 18:52:30 +0100
parents c1ecc16cf38e
children ae87ae455cfb
files vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb vendor/plugins/redmine_bibliography/app/models/author.rb vendor/plugins/redmine_bibliography/app/models/publication.rb vendor/plugins/redmine_bibliography/app/views/publications/_author_fields.rhtml vendor/plugins/redmine_bibliography/app/views/publications/_authors_fields.html.erb vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb vendor/plugins/redmine_bibliography/assets/javascripts/authors.js
diffstat 8 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Thu Jun 23 18:52:30 2011 +0100
@@ -13,7 +13,7 @@
     @publication.build_bibtex_entry
     
     # and at least one author
-    @publication.authors.build
+    3.times {@publication.authors.build}
     
     @project_id = params[:project_id]
     @current_user = User.current
--- a/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb	Thu Jun 23 18:52:30 2011 +0100
@@ -9,4 +9,12 @@
     end
     s 
   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
+    link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
+  end  
 end
--- a/vendor/plugins/redmine_bibliography/app/models/author.rb	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/author.rb	Thu Jun 23 18:52:30 2011 +0100
@@ -1,5 +1,5 @@
 class Author < ActiveRecord::Base
-  has_many :authorships
+  has_many :authorships, :dependent => :destroy
   has_many :publications, :through => :authorships
 
   belongs_to :user
--- a/vendor/plugins/redmine_bibliography/app/models/publication.rb	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/models/publication.rb	Thu Jun 23 18:52:30 2011 +0100
@@ -3,7 +3,7 @@
 class Publication < ActiveRecord::Base
   unloadable
   
-  has_many :authorships
+  has_many :authorships, :dependent => :destroy
   has_many :authors, :through => :authorships, :uniq => true
   
   has_one :bibtex_entry, :dependent => :destroy
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_author_fields.rhtml	Thu Jun 23 18:52:30 2011 +0100
@@ -0,0 +1,8 @@
+<div class="author_fields">
+  <% fields_for :authors do |author_form| -%>
+    <%= author_form.label :name, l("name") %> <%= author_form.text_field :name %>            
+    <%= author_form.hidden_field :_destroy %>
+    
+    <%= link_to_function "Remove", "remove_author(this)" %>  
+  <% end -%>
+</div>  
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authors_fields.html.erb	Wed Jun 22 19:42:31 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-<p class="author_fields">
-  <%= f.label :name, l("name") %> <%= f.text_field :name %>            
-  <%= f.hidden_field :_destroy %>
-  <%= link_to_function "Remove", "remove_author(this)" %>  
-</p>
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb	Thu Jun 23 18:52:30 2011 +0100
@@ -4,24 +4,19 @@
 
 
 <% form_for @publication, :url => { :project_id  => @project_id, :action => :create } do |f| -%>
-
   <%= f.error_messages %>
   
   <p><%= f.label :title, l(:title) %>  <%= f.text_field :title %></p>
 
   <h3><%= l(:authors) %></h3>    
-  <%= link_to_remote l(:add_me_as_author), {:url => { :controller => 'publications', :action => 'add_author', :user_id => @current_user}, :update => 'users_div' }, { :id => 'add_me_as_an_author', :class => 'icon icon-add' } %>  
+    <%= link_to_remote l(:add_me_as_author), {:url => { :controller => 'publications', :action => 'add_author', :user_id => @current_user}, :update => 'users_div' }, { :id => 'add_me_as_an_author', :class => 'icon icon-add' } %>  
 
-  <div id='users_div'>aaa</div>
-  
-  
-  
-  
-  
-  
-  <%- f.fields_for :authors do |builder| -%>
-    <%= render :partial => 'authors_fields', :locals => { :f => builder} %>
-  <%- end -%>
+
+  <div id='authors'>    
+    <%= link_to_add_fields l(:add_another_author), f, :authors %>
+
+    <%= render :partial => 'author_fields', :collection => @publication.authors %>    
+  </div>  
     
   <h3>Other Details</h3>
 
--- a/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Wed Jun 22 19:42:31 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Thu Jun 23 18:52:30 2011 +0100
@@ -1,4 +1,10 @@
 function remove_author(link){
-	$(link).previous("input[type=hidden]").value = 1;
-	$(link).up(".author_fields").hide();
+	$(link).previous('input[type=hidden]').value = 1;
+	$(link).up('.author_fields').remove();
+}
+
+function add_fields(link, association, content) {
+  var new_id = new Date().getTime();
+  var regexp = new RegExp("new_" + association, "g")
+  $(link).parent().before(content.replace(regexp, new_id));
 }
\ No newline at end of file