# HG changeset patch # User luisf # Date 1308851550 -3600 # Node ID 0bb9c7baed073332b6b18e0363b03beff799d275 # Parent c1ecc16cf38e028b3731b79326e46fa68cd8fda0 dynamically adding/removing users. diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb --- 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 diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/helpers/publications_helper.rb --- 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 diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/models/author.rb --- 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 diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/models/publication.rb --- 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 diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/views/publications/_author_fields.rhtml --- /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 @@ +
+ <% 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 -%> +
diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/views/publications/_authors_fields.html.erb --- 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 @@ -

- <%= f.label :name, l("name") %> <%= f.text_field :name %> - <%= f.hidden_field :_destroy %> - <%= link_to_function "Remove", "remove_author(this)" %> -

diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/app/views/publications/_edit.html.erb --- 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 %>

<%= f.label :title, l(:title) %> <%= f.text_field :title %>

<%= l(:authors) %>

- <%= 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' } %> -
aaa
- - - - - - - <%- f.fields_for :authors do |builder| -%> - <%= render :partial => 'authors_fields', :locals => { :f => builder} %> - <%- end -%> + +
+ <%= link_to_add_fields l(:add_another_author), f, :authors %> + + <%= render :partial => 'author_fields', :collection => @publication.authors %> +

Other Details

diff -r c1ecc16cf38e -r 0bb9c7baed07 vendor/plugins/redmine_bibliography/assets/javascripts/authors.js --- 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