From 8b352c80d8806956336b7cc01532b9e2f8c48039 Mon Sep 17 00:00:00 2001 From: Dreamersoul Date: Sat, 7 Jul 2018 17:03:56 +0300 Subject: [PATCH] This closes #4 --- administrate-field-active_storage.gemspec | 2 +- app/views/fields/active_storage/_index.html.erb | 1 + app/views/fields/active_storage/_item.html.erb | 10 +++++----- app/views/fields/active_storage/_show.html.erb | 1 + lib/administrate/field/active_storage.rb | 14 ++++++++++---- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/administrate-field-active_storage.gemspec b/administrate-field-active_storage.gemspec index cc443cd..4de95f1 100644 --- a/administrate-field-active_storage.gemspec +++ b/administrate-field-active_storage.gemspec @@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__) Gem::Specification.new do |gem| gem.name = "administrate-field-active_storage" - gem.version = "0.1.0" + gem.version = "0.1.1" gem.authors = ["Hamad AlGhanim"] gem.email = ["hamadyalghanim@gmail.com"] gem.homepage = "https://github.com/Dreamersoul/administrate-field-active_storage" diff --git a/app/views/fields/active_storage/_index.html.erb b/app/views/fields/active_storage/_index.html.erb index cac12ec..2c3887d 100644 --- a/app/views/fields/active_storage/_index.html.erb +++ b/app/views/fields/active_storage/_index.html.erb @@ -18,6 +18,7 @@ By default, the attribute is rendered as an image tag. <% if field.many? %> <% field.attachments.each do |attachment| %> <%= render :partial => 'item', :field => field, :attachment => attachment %> +
<% end %> <% else %> <%= render :partial => 'item', :field => field, :attachment => field.data %> diff --git a/app/views/fields/active_storage/_item.html.erb b/app/views/fields/active_storage/_item.html.erb index 7c014f9..b08dbbb 100644 --- a/app/views/fields/active_storage/_item.html.erb +++ b/app/views/fields/active_storage/_item.html.erb @@ -1,12 +1,12 @@ -<% if attachment.image? %> +<% if attachment.image? and !field.url_only? %> <%= image_tag(field.url(attachment)) %> -<% elsif attachment.video? and attachment.previewable? %> <%# if ffmpeg is installed %> +<% elsif attachment.video? and attachment.previewable? and !field.url_only? %> <%# if ffmpeg is installed %> <%= video_tag(field.url(attachment), poster: field.preview(attachment, resize: "1920x1080>"), controls: true, autobuffer: true, style: "width: 100%; height: auto;") %> -<% elsif attachment.video? %> +<% elsif attachment.video? and !field.url_only? %> <%= video_tag(field.url(attachment), controls: true, autobuffer: true, style: "width: 100%; height: auto;") %> -<% elsif attachment.audio? %> +<% elsif attachment.audio? and !field.url_only? %> <%= audio_tag(field.url(attachment), autoplay: true, controls: true) %> -<% elsif attachment.previewable? %> +<% elsif attachment.previewable? and !field.url_only? %> <%= image_tag(field.preview(attachment, resize: "595x842>")) %>
Download: <%= link_to(attachment.filename, field.blob_url(attachment)) %> diff --git a/app/views/fields/active_storage/_show.html.erb b/app/views/fields/active_storage/_show.html.erb index 306f2ba..0bdf123 100644 --- a/app/views/fields/active_storage/_show.html.erb +++ b/app/views/fields/active_storage/_show.html.erb @@ -18,6 +18,7 @@ By default, the attribute is rendered as an image tag. <% if field.many? %> <% field.attachments.each do |attachment| %> <%= render :partial => 'fields/active_storage/item', locals: { field: field, attachment: attachment } %> +
<% end %> <% else %> <%= render :partial => 'fields/active_storage/item', locals: { field: field, attachment: field.data } %> diff --git a/lib/administrate/field/active_storage.rb b/lib/administrate/field/active_storage.rb index e4bb2aa..a1c42d3 100644 --- a/lib/administrate/field/active_storage.rb +++ b/lib/administrate/field/active_storage.rb @@ -6,16 +6,22 @@ module Field class ActiveStorage < Administrate::Field::Base class Engine < ::Rails::Engine end - # currently we are using Rails.application.routes.url_helpers - # without including the namespace because it runs into an - # exception + + def url_only? + options.fetch(:url_only, false) + end + def many? # find a way to use instance_of data.class.name == "ActiveStorage::Attached::Many" end + + # currently we are using Rails.application.routes.url_helpers + # without including the namespace because it runs into an + # exception + # work around since calling data.preview(options) # returns "/images/" which isnt the url - def preview(attachment, options) Rails.application.routes.url_helpers.rails_representation_path(attachment.preview(options), only_path: true) end