Skip to content

Commit

Permalink
This closes #1, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamersoul committed Jul 7, 2018
1 parent 7130dc8 commit b4f3943
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 49 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Administrate::Field::ActiveStorage
![rails](https://img.shields.io/badge/rails-%3E%3D5.2.0-red.svg)
## Things To Know:
- currently the gem only works for `has_one_attached` support for `has_many_attached` will be added in a future release.
- to preview pdf files you need to install `mupdf` or `Poppler`.
- to preview video files you need to install `ffmpeg`.

## How To Use:

Add `administrate-field-active_storage` to your Gemfile:

```ruby
Expand All @@ -18,7 +18,7 @@ Install:
$ bundle install
```

Use:
### `has_one_attached`:
assuming your modelname is `Model` and field name is `attachment`
```ruby
class ModelDashboard < Administrate::BaseDashboard
Expand All @@ -30,6 +30,29 @@ class ModelDashboard < Administrate::BaseDashboard
Then add `:attachment` to `FORM_ATTRIBUTES` and `SHOW_PAGE_ATTRIBUTES`.
currently adding `:attachment` `COLLECTION_ATTRIBUTES` will work but will probably look too big.

### `has_many_attached`:
assuming your modelname is `Model` and field name is `attachments`
the processs is identical the only issue is that the form field isn't being permitted, in order to permit it we apply the following method to the dashboard:

```ruby
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachments: Field::ActiveStorage,
}

# ...
FORM_ATTRIBUTES = {
#...
:attachments
}

# permitted for has_many_attached
def permitted_attributes
super + [:attachments => []]
end
```
i know it is not ideal, if you have a workaround please submit a PR

## Things To Do:

- [x] upload single file
Expand All @@ -39,8 +62,9 @@ currently adding `:attachment` `COLLECTION_ATTRIBUTES` will work but will probab
- [x] download link to other files
- [x] preview videos
- [x] preview pdfs
- [x] upload multiple files
- [ ] find a way to delete attachments
- [ ] preview office files as pictures
- [ ] upload multiple files

## Contribution guide:
1. contributers are welcome (code, suggestions, and bugs).
Expand Down
Binary file removed administrate-field-active_storage-0.0.2.gem
Binary file not shown.
2 changes: 1 addition & 1 deletion administrate-field-active_storage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |gem|
gem.name = "administrate-field-active_storage"
gem.version = "0.0.2"
gem.version = "0.1.0"
gem.authors = ["Hamad AlGhanim"]
gem.email = ["[email protected]"]
gem.homepage = "https://github.com/Dreamersoul/administrate-field-active_storage"
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/active_storage/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ By default, the input is a text field for the image's URL.
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.file_field field.attribute %>
<%= f.file_field field.attribute, multiple: field.many? %>
</div>
22 changes: 8 additions & 14 deletions app/views/fields/active_storage/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ By default, the attribute is rendered as an image tag.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Image
%>
<% if field.image? %>
<%= image_tag(field.url) %>
<% elsif field.video? and field.previewable?%> <%# if ffmpeg is installed %>
<%= video_tag(field.url, poster: field.preview(resize: "1920x1080>"), controls: true, autobuffer: true, style: "width: 100%; height: auto;") %>
<% elsif field.video? %>
<%= video_tag(field.url, controls: true, autobuffer: true, style: "width: 100%; height: auto;") %>
<% elsif field.audio? %>
<%= audio_tag(field.url, autoplay: true, controls: true) %>
<% elsif field.previewable? %>
<%= image_tag(field.preview(resize: "595x842>")) %>
<br/>
Download: <%= link_to(field.filename, field.blob_url) %>
<% else %>
<%= link_to(field.filename, field.blob_url) %>
<% if field.attached? %>
<% 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 %>
<% end %>
<% end %>
15 changes: 15 additions & 0 deletions app/views/fields/active_storage/_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% if attachment.image? %>
<%= image_tag(field.url(attachment)) %>
<% elsif attachment.video? and attachment.previewable? %> <%# 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? %>
<%= video_tag(field.url(attachment), controls: true, autobuffer: true, style: "width: 100%; height: auto;") %>
<% elsif attachment.audio? %>
<%= audio_tag(field.url(attachment), autoplay: true, controls: true) %>
<% elsif attachment.previewable? %>
<%= image_tag(field.preview(attachment, resize: "595x842>")) %>
<br/>
Download: <%= link_to(attachment.filename, field.blob_url(attachment)) %>
<% else %>
<%= link_to(attachment.filename, field.blob_url(attachment)) %>
<% end %>
23 changes: 9 additions & 14 deletions app/views/fields/active_storage/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ By default, the attribute is rendered as an image tag.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Image
%>

<% if field.image? %>
<%= image_tag(field.url) %>
<% elsif field.video? %>
<%= video_tag(field.url, poster: field.preview(resize: "1920x1080>"), controls: true, autobuffer: true, style: "width: 100%; height: auto;") %>
<% elsif field.audio? %>
<%= audio_tag(field.url, autoplay: true, controls: true) %>
<% elsif field.previewable? %>
<%= image_tag(field.preview(resize: "595x842>")) %>
<br/>
Download: <%= link_to(field.filename, field.blob_url) %>
<% else %>
<%= link_to(field.filename, field.blob_url) %>
<% end %>
<% if field.attached? %>
<% 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 } %>
<% end %>
<% end %>
30 changes: 14 additions & 16 deletions lib/administrate/field/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@ class Engine < ::Rails::Engine
# currently we are using Rails.application.routes.url_helpers
# without including the namespace because it runs into an
# exception
def many?
# find a way to use instance_of
data.class.name == "ActiveStorage::Attached::Many"
end
# work around since calling data.preview(options)
# returns "/images/<ActiveStorage::Preview>" which isnt the url

def url
Rails.application.routes.url_helpers.rails_blob_path(data, only_path: true)
def preview(attachment, options)
Rails.application.routes.url_helpers.rails_representation_path(attachment.preview(options), only_path: true)
end

def blob_url
Rails.application.routes.url_helpers.rails_blob_path(data, disposition: :attachment, only_path: true)
def url(attachment)
Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true)
end

# work around since calling data.preview(options)
# returns "/images/<ActiveStorage::Preview>" which isnt the url

def preview(options)
Rails.application.routes.url_helpers.rails_representation_path(data.preview(options), only_path: true)
def blob_url(attachment)
Rails.application.routes.url_helpers.rails_blob_path(attachment, disposition: :attachment, only_path: true)
end

delegate :filename, to: :data
delegate :previewable?, to: :data
delegate :image?, to: :data
delegate :video?, to: :data
delegate :audio?, to: :data
delegate :audio?, to: :data

delegate :attached?, to: :data
delegate :attachments, to: :data
end
end
end

0 comments on commit b4f3943

Please sign in to comment.