From da5b24ef57f94adad44b3298969e51779cf1e717 Mon Sep 17 00:00:00 2001 From: aignatov-bio <47317017+aignatov-bio@users.noreply.github.com> Date: Mon, 13 May 2019 10:54:16 +0200 Subject: [PATCH] Fix attachment appearence after update (#1745) --- app/controllers/assets_controller.rb | 28 +++++++++++++++++++++------- app/models/step.rb | 6 ++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index b50b00f14..a587fc206 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -168,15 +168,29 @@ class AssetsController < ApplicationController # Post process file here @asset.post_process_file(@asset.team) + render_html = if @asset.step + asset_position = @asset.step.asset_position(@asset) + render_to_string( + partial: 'steps/attachments/item.html.erb', + locals: { + asset: @asset, + i: asset_position[:pos], + assets_count: asset_position[:count], + step: @asset.step + }, + formats: :html + ) + else + render_to_string( + partial: 'shared/asset_link', + locals: { asset: @asset, display_image_tag: true }, + formats: :html + ) + end + respond_to do |format| format.json do - render json: { - html: render_to_string( - partial: 'shared/asset_link', - locals: { asset: @asset, display_image_tag: true }, - formats: :html - ) - } + render json: { html: render_html } end end end diff --git a/app/models/step.rb b/app/models/step.rb index cd63d221e..f2c220511 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -124,6 +124,12 @@ class Step < ApplicationRecord st end + def asset_position(asset) + assets.order(:file_updated_at).each_with_index do |step_asset, i| + return { count: assets.count, pos: i } if asset.id == step_asset.id + end + end + protected def cascade_after_destroy