Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add open attachment without download #3681

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
<% 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">
<a
<% if @field.openable %>
href="<%= helpers.main_app.url_for(file) %>" target="_blank" rel="noopener noreferrer"
<% end %>
class="relative flex flex-col justify-evenly items-center px-2 rounded-lg border bg-white border-gray-500 min-h-24 <%= 'hover:bg-gray-100 transition' if @field.openable %>"
>
<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>
</a>
<% end %>
<% if @field.display_filename %>
<span class="text-gray-500 mt-1 text-sm truncate" title="<%= file.filename %>"><%= file.filename %></span>
Expand Down
2 changes: 2 additions & 0 deletions lib/avo/fields/file_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class FileField < BaseField
attr_accessor :is_audio
attr_accessor :direct_upload
attr_accessor :accept
attr_accessor :openable
attr_reader :display_filename

def initialize(id, **args, &block)
Expand All @@ -19,6 +20,7 @@ def initialize(id, **args, &block)
@direct_upload = args[:direct_upload].present? ? args[:direct_upload] : false
@accept = args[:accept].present? ? args[:accept] : nil
@display_filename = args[:display_filename].nil? ? true : args[:display_filename]
@openable = args[:openable].present? ? args[:openable] : false
end

def path
Expand Down
2 changes: 2 additions & 0 deletions lib/avo/fields/files_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FilesField < BaseField
attr_accessor :is_image
attr_accessor :direct_upload
attr_accessor :accept
attr_accessor :openable
attr_reader :display_filename
attr_reader :view_type
attr_reader :hide_view_type_switcher
Expand All @@ -19,6 +20,7 @@ def initialize(id, **args, &block)
@display_filename = args[:display_filename].nil? ? true : args[:display_filename]
@view_type = args[:view_type] || :grid
@hide_view_type_switcher = args[:hide_view_type_switcher]
@openable = args[:openable].present? ? args[:openable] : false
end

def view_component_name
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/field_discovery_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Avo::Resources::FieldDiscoveryUser < Avo::BaseResource
def fields
main_panel do
discover_columns except: %i[email active is_admin? birthday is_writer outside_link custom_css]
discover_associations only: %i[cv_attachment]
discover_associations only: %i[cv_attachment], openable: true

sidebar do
with_options only_on: :show do
Expand Down
26 changes: 26 additions & 0 deletions spec/system/avo/open_field_attachment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe "OpenFieldAttachment", type: :system do
let!(:user) { User.first }
let!(:cv_file) { Rails.root.join("app", "assets", "pdfs", "cv_sample.pdf") }
let(:path) { "/admin/resources/field_discovery_users/#{user.slug}" }

before do
user.cv.attach(io: File.open(cv_file), filename: "cv_sample.pdf", content_type: "application/pdf")
end

def test_open_field_attachment(path)
visit path

link = find('a[rel="noopener noreferrer"][target="_blank"]', visible: :all)
expect(link).to be_present
link.click

expect(page.driver.browser.current_url).not_to include("download")
expect(page.driver.browser.window_handles.length).to eq 2
end

it "opens attachment in new window without download" do
test_open_field_attachment(path)
end
end
Loading