-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Always display persisted image in the edit view (#3015)
* Check if file is persisted before rendering it in view * Remove dash * Always display persisted image in the edit view * Write tests * Refactor: mutate value in file_field instead of base_field * Always display persisted attachment in edit view * Change file name * Remove conditional --------- Co-authored-by: Adrian Marin <[email protected]> Co-authored-by: Paul Bob <[email protected]>
- Loading branch information
1 parent
b8ad0e2
commit 4ce587f
Showing
5 changed files
with
79 additions
and
24 deletions.
There are no files selected for viewing
44 changes: 20 additions & 24 deletions
44
app/components/avo/fields/common/files/view_type/grid_item_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
<div id="<%= dom_id file %>" class="relative min-h-full max-w-full flex-1 flex flex-col justify-between space-y-2"> | ||
<% if file.present? %> | ||
<div class="flex flex-col h-full"> | ||
<% if file.representable? && is_image? %> | ||
<%= image_tag helpers.main_app.url_for(file), class: "rounded-lg max-w-full self-start #{@extra_classes}", loading: :lazy, width: file.metadata["width"], height: file.metadata["height"] %> | ||
<% elsif is_audio? %> | ||
<%= audio_tag(helpers.main_app.url_for(file), controls: true, preload: false, class: 'w-full') %> | ||
<% elsif is_video? %> | ||
<%= video_tag(helpers.main_app.url_for(file), controls: true, preload: false, class: 'w-full') %> | ||
<% else %> | ||
<div class="relative flex flex-col justify-evenly items-center px-2 rounded-lg border bg-white border-gray-500 min-h-24"> | ||
<div class="flex flex-col justify-center items-center w-full"> | ||
<%= helpers.svg "heroicons/outline/document-text", class: 'h-10 text-gray-600 mb-2' %> | ||
</div> | ||
<div class="flex flex-col h-full"> | ||
<% if file.representable? && is_image? %> | ||
<%= image_tag helpers.main_app.url_for(file), class: "rounded-lg max-w-full self-start #{@extra_classes}", loading: :lazy, width: file.metadata["width"], height: file.metadata["height"] %> | ||
<% elsif is_audio? %> | ||
<%= audio_tag(helpers.main_app.url_for(file), controls: true, preload: false, class: 'w-full') %> | ||
<% elsif is_video? %> | ||
<%= video_tag(helpers.main_app.url_for(file), controls: true, preload: false, class: 'w-full') %> | ||
<% else %> | ||
<div class="relative flex flex-col justify-evenly items-center px-2 rounded-lg border bg-white border-gray-500 min-h-24"> | ||
<div class="flex flex-col justify-center items-center w-full"> | ||
<%= helpers.svg "heroicons/outline/document-text", class: 'h-10 text-gray-600 mb-2' %> | ||
</div> | ||
<% end %> | ||
<% if field.display_filename %> | ||
<span class="text-gray-500 mt-1 text-sm truncate" title="<%= file.filename %>"><%= file.filename %></span> | ||
<% end %> | ||
</div> | ||
<div class="flex space-x-2"> | ||
<%= render Avo::Fields::Common::Files::ControlsComponent.new(field: field, file: file, resource: resource) %> | ||
</div> | ||
<% else %> | ||
– | ||
<% end %> | ||
</div> | ||
<% end %> | ||
<% if field.display_filename %> | ||
<span class="text-gray-500 mt-1 text-sm truncate" title="<%= file.filename %>"><%= file.filename %></span> | ||
<% end %> | ||
</div> | ||
<div class="flex space-x-2"> | ||
<%= render Avo::Fields::Common::Files::ControlsComponent.new(field: field, file: file, resource: resource) %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "DisplayPersistedAttachment", type: :system do | ||
describe "display persisted image on edit view" do | ||
let(:post) do | ||
create(:post) do |p| | ||
p.cover_photo.attach( | ||
io: File.open(Rails.root.join("db", "seed_files", "iphone.jpg")), | ||
filename: "iphone.jpg", | ||
content_type: "image/jpeg" | ||
) | ||
end | ||
end | ||
|
||
it "displays persisted image on edit view" do | ||
visit "/admin/resources/posts/#{post.to_param}/edit" | ||
fill_in "Name", with: "" | ||
attach_file Rails.root.join("db", "seed_files", "dummy-image.jpg"), id: "post_cover_photo" | ||
|
||
click_button "Save" | ||
wait_for_loaded | ||
|
||
expect(page).to have_selector 'div[data-field-id="cover_photo"] img' | ||
end | ||
end | ||
|
||
describe "display persisted audio on edit view" do | ||
let(:post) do | ||
create(:post) do |p| | ||
p.audio.attach( | ||
io: File.open(Rails.root.join("db", "seed_files", "dummy-music.mp3")), | ||
filename: "dummy-music.mp3", | ||
content_type: "audio/mp3" | ||
) | ||
end | ||
end | ||
|
||
it "displays persisted audio on edit view" do | ||
visit "/admin/resources/posts/#{post.to_param}/edit" | ||
fill_in "Name", with: "" | ||
attach_file Rails.root.join("db", "seed_files", "dummy-music-two.mp3"), id: "post_audio" | ||
|
||
click_button "Save" | ||
wait_for_loaded | ||
|
||
expect(page).to have_selector "audio" | ||
end | ||
end | ||
end |