REFILE

rails generate migration add_avatar_to_users 
  avatar_id:string 
  avatar_filename:string
  avatar_size:integer 
  avatar_content_type:string

rake db:migrate
class User < ActiveRecord::Base
  attachment :profile_image
end
= simple_form_for @user do |form|
  = form.attachment_field :avatar
def user_params
  params.require(:user).permit(:avatar)
end
= image_tag attachment_url(@user, :avatar)
  1. Backends: cache and persist files
  2. Model attachments: map files to model columns
  3. A Rack application: streams files and accepts uploads
  4. Rails helpers: conveniently generate markup in your views
  5. A JavaScript library: facilitates direct uploads

1. Backend

require "refile/backend/s3"

aws = {
  access_key_id: 'key_id',
  secret_access_key: 'key',
  bucket: 'narconnecting.production',
  region: 'eu-west-1'
}
Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws)
Refile.store = Refile::Backend::S3.new(prefix: "store", **aws)

2. Attachments

class User < ActiveRecord::Base
  attachment :avatar
end

assign uploads to cache

save transfers to store

3. Rack application

  • an endpoint, not a middleware
  • written in Sinatra
  • streams files from backends
  • upload them to backends

3. Rack application

Refile.host = "//your-dist-url.cloudfront.net"

3. Rack application

GET /attachments/:token/:backend_name/:processor_name/*args/:id/:filename
  • fill 
  • fit
  • limit 
  • pad
  • convert

4. Rails helpers

= simple_form_for @user do |form|
  = form.attachment_field :avatar
<input value="{}" type="hidden" name="user[avatar]">
<input type="file" name="user[avatar]" id="user_avatar">
= link_to "Image", attachment_url(@user, :avatar) 
= attachment_image_tag(@user, :avatar, :fill, 300, 300)

5. JavaScript library

//= require refile
= form.attachment_field :avatar, direct: true
$(document).on("upload:start", "form", function(e) {
  // ...
});

"upload:progress"
"upload:complete"
"upload:success"
"upload:failure"

Additional stuff

= simple_form_for @user do |form| %>
  = form.label :avatar
  = form.attachment_field :avatar

  = form.check_box :remove_avatar
  = form.label :remove_avatar

  = form.label :remote_avatar_url, "Or specify URL"
  = form.text_field :remote_avatar_url
def user_params
  params.require(:user).permit(
    :avatar, 
    :remove_avatar,
    :remote_avatar_url
  )
end

Refile vs Paperclip vs Carrierwave

REFILE

By Stjepan Hadjić

REFILE

  • 824