Skip to content

Commit

Permalink
most basic features are working now
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamersoul committed Jul 6, 2018
1 parent db044ab commit 5f53ad5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 14 deletions.
51 changes: 42 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
# Administrate::Field::ActiveStorage
![rails](https://img.shields.io/badge/rails-%3E%3D5.2.0-red.svg)
## Things to do:

- [ ] upload single file
- [ ] adding image support through url_for to support 3rd party cloud storage
- [ ] use html 5 video element for video files
- [ ] use html audio element for audio files
- [ ] download link to other files
- [ ] preview pdfs and office files as pictures
## Things To Know:
- 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
gem 'administrate-field-active_storage'
```

Install:

```
$ bundle install
```

Use:
assuming your modelname is `Model` and field name is `attachment`
```ruby
class ModelDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
attachment: Field::ActiveStorage,
}
# ...
```
Then add `:attachment` to `FORM_ATTRIBUTES` and `SHOW_PAGE_ATTRIBUTES`.
currently adding `:attachment` `COLLECTION_ATTRIBUTES` will work but will probably look too big.

## Things To Do:

- [x] upload single file
- [x] adding image support through url_for to support 3rd party cloud storage
- [x] use html 5 video element for video files
- [x] use html audio element for audio files
- [x] download link to other files
- [x] preview videos
- [x] preview pdfs
- [ ] preview office files as pictures
- [ ] upload multiple files

## Contribution guide:
1. contributers are welcome (code, suggestions, and bugs).
2. please document your code.
3. add your name to the `contribute.md`.

please note that this is my first gem :) i might have gotten some stuff wrong PR's are always welcome
---
Based on the [Administrate::Field::Image](https://github.com/thoughtbot/administrate-field-image) template.
Based on the [Administrate::Field::Image](https://github.com/thoughtbot/administrate-field-image) template, and inspired by [Administrate::Field::Paperclip](https://github.com/picandocodigo/administrate-field-paperclip).

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.1"
gem.version = "0.0.2"
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.text_field field.attribute %>
<%= f.file_field field.attribute %>
</div>
17 changes: 15 additions & 2 deletions app/views/fields/active_storage/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,18 @@ By default, the attribute is rendered as an image tag.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Image
%>

<%= image_tag field.data %>
<% 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) %>
<% end %>
14 changes: 13 additions & 1 deletion app/views/fields/active_storage/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ By default, the attribute is rendered as an image tag.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Image
%>

<%= image_tag field.data if field.data.present? %>
<% 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 %>
26 changes: 26 additions & 0 deletions lib/administrate/field/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ 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
Rails.application.routes.url_helpers.rails_blob_path(data, only_path: true)
end

def blob_url
Rails.application.routes.url_helpers.rails_blob_path(data, disposition: :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)
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

end
end
end

0 comments on commit 5f53ad5

Please sign in to comment.