From 5940486c7715aee85cccfd65fc130074424e91aa Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Tue, 16 May 2017 13:05:24 +0200 Subject: [PATCH] #48 Caching documents in the issue form --- app/views/dmsf_upload/_form.html.erb | 4 ++-- .../hooks/controllers/issues_controller_hooks.rb | 16 ++++++++++++++++ lib/redmine_dmsf/hooks/views/issue_view_hooks.rb | 2 +- lib/redmine_dmsf/patches/issue_patch.rb | 13 +++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/views/dmsf_upload/_form.html.erb b/app/views/dmsf_upload/_form.html.erb index 5066ab01..207881f1 100644 --- a/app/views/dmsf_upload/_form.html.erb +++ b/app/views/dmsf_upload/_form.html.erb @@ -21,8 +21,8 @@ %> -<% if defined?(container) && container && container.saved_attachments %> - <% container.saved_attachments.each_with_index do |attachment, i| %> +<% if defined?(container) && container && container.saved_dmsf_attachments %> + <% container.saved_dmsf_attachments.each_with_index do |attachment, i| %> <%= text_field_tag("dmsf_attachments[p#{i}][filename]", attachment.filename, :class => 'filename') + text_field_tag("dmsf_attachments[p#{i}][description]", attachment.description, :maxlength => 255, :placeholder => l(:label_optional_description), :class => 'description') + diff --git a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb index 8e10700c..55196d28 100644 --- a/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb +++ b/lib/redmine_dmsf/hooks/controllers/issues_controller_hooks.rb @@ -24,6 +24,10 @@ module RedmineDmsf class ControllerIssuesHook < RedmineDmsf::Hooks::Listener + def controller_issues_new_before_save(context={}) + controller_issues_before_save(context) + end + def controller_issues_new_after_save(context={}) controller_issues_after_save(context) # Copy documents from the source issue @@ -40,12 +44,24 @@ module RedmineDmsf end end + def controller_issues_edit_before_save(context={}) + controller_issues_before_save(context) + end + def controller_issues_edit_after_save(context={}) controller_issues_after_save(context) end private + def controller_issues_before_save(context) + if context.is_a?(Hash) + issue = context[:issue] + params = context[:params] + issue.save_dmsf_attachments(params[:dmsf_attachments]) + end + end + def controller_issues_after_save(context) # Create attached documents if context.is_a?(Hash) diff --git a/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb b/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb index f464e28e..3d27624a 100644 --- a/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb +++ b/lib/redmine_dmsf/hooks/views/issue_view_hooks.rb @@ -34,7 +34,7 @@ module RedmineDmsf html << '

' html << "" html << context[:controller].send(:render_to_string, - {:partial => 'dmsf_upload/form', :locals => { :multiple => true }}) + {:partial => 'dmsf_upload/form', :locals => { :container => issue, :multiple => true }}) html << '

' html << '' html.html_safe diff --git a/lib/redmine_dmsf/patches/issue_patch.rb b/lib/redmine_dmsf/patches/issue_patch.rb index 3284bad4..f3857de2 100644 --- a/lib/redmine_dmsf/patches/issue_patch.rb +++ b/lib/redmine_dmsf/patches/issue_patch.rb @@ -32,6 +32,19 @@ module RedmineDmsf module InstanceMethods + def save_dmsf_attachments(dmsf_attachments) + @saved_dmsf_attachments = [] + dmsf_attachments = dmsf_attachments.map(&:last) + dmsf_attachments.each do |dmsf_attachment| + a = Attachment.find_by_token(dmsf_attachment[:token]) + @saved_dmsf_attachments << a if a + end + end + + def saved_dmsf_attachments + @saved_dmsf_attachments || [] + end + def system_folder(create = false) parent = DmsfFolder.system.where(:project_id => self.project_id, :title => '.Issues').first if create && !parent