ActiveStorage::Imgur
An ActiveStorage driver for storing images
on Imgur hosting.
Installation
Add this line to your application's Gemfile:
gem 'activestorage-imgur'
And then execute:
$ bundle
Quick Start
-
generate a migration that creates the dependent table:
# if you never use activestorage, run rails active_storage:install first. rails g active_storage_imgur:install rails db:migrate
-
Set
config.active_storage.service
to:imgur
in any ofconfig/environments/*.rb
files. -
Set the imgur config in your
config/storage.yml
:imgur: service: Imgur client_id: <%= ENV['IMGUR_CLIENT_ID'] %> client_secret: <%= ENV['IMGUR_CLIENT_SECRET'] %> access_token: <%= ENV['IMGUR_ACCESS_TOKEN'] %> refresh_token: <%= ENV['IMGUR_REFRESH_TOKEN'] %>
for environment variables on the above, follow steps:
- sign an imgur account: https://imgur.com/
- to create an imgur application visit: https://api.imgur.com/oauth2/addclient
- you will get client_id and client_secret after creating an application.
- run
rails imgur:authorize CLIENT_ID='CLIENT_ID' CLIENT_SECRET='CLIENT_SECRET'
to getaccess_token
andrefresh_token
.
-
Add attachment to your model just like explanation of activestorage:
class User has_one_attached :avatar has_many_attached :photos ... end
<%= form_for @user do |form| %> <% if @user.errors.any? %> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> <% end %> Avatar: <%= form.file_field :avatar %> Photo: <%= form.file_field :photos, multiple: true %> <%= form.submit %> <% end %>
<%= image_tag user.avatar %> <%= image_tag user.photos.first.variant(resize: "100x100") %>
for more detail usage like uploading, image displaying. see official documentationsactivestorage.
Be careful
- this gem has built-in validation to validate image file. attachment can be nil, if it presents, it only accept image type. if your app only accept files with image types, it should be fine.
- though imgur is free, it still has rate limits, if your application hit the daily limit, uploading function will probably be terminated.
Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.