Skip to content

Instantly share code, notes, and snippets.

@kmarsh
Created August 19, 2011 19:52
Show Gist options
  • Save kmarsh/1157829 to your computer and use it in GitHub Desktop.
Save kmarsh/1157829 to your computer and use it in GitHub Desktop.

Revisions

  1. kmarsh created this gist Aug 19, 2011.
    92 changes: 92 additions & 0 deletions carrierwave-issue-436.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    require 'rubygems'
    require 'active_support'
    require 'carrierwave'
    require 'sinatra'
    require 'sqlite3'
    require 'sequel'
    require 'carrierwave/sequel'
    require 'mini_magick'

    # Example app to demonstrate issue as seen in https://github.com/jnicklas/carrierwave/issues/436
    # Used http://www.engineyard.com/blog/2011/a-gentle-introduction-to-carrierwave/
    # as a starting point.
    #
    # gem install sinatra sqlite3 sequel carrierwave-sequel

    CarrierWave.configure do |config|
    config.root = "#{Dir.pwd}/public/"
    end

    # database setup

    DB = Sequel.sqlite

    DB.create_table :uploads do
    String :file
    end

    # uploader

    class MyUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

    storage :file

    version :blurred do
    process :blur
    end

    version :thumbnail do
    process :resize_to_fit => [250, 250]
    end

    def blur
    manipulate! do |img|
    img = img.radial_blur 10
    end
    end
    end

    # model

    class Upload < Sequel::Model
    mount_uploader :file, MyUploader

    def filename
    "#{model.id}.#{file.extension}" if file
    end

    def store_dir
    "public/uploads"
    end
    end

    # sinatra app

    get '/' do
    @uploads = Upload.all
    erb :index
    end

    post '/' do
    upload = Upload.new
    upload.file = params[:image]
    upload.save
    redirect to('/')
    end

    __END__

    @@ index
    <!DOCTYPE html>
    <html>
    <body>
    <form action="/" method="post" enctype="multipart/form-data"></div>
    <p><input type="file" name="image" /></p>
    <p><input type="submit" name="submit" value="Upload" /></p>
    </form>
    <% @uploads.each do |upload| %>
    <img src="<%= upload.file.thumbnail.url %>" />
    <% end %>
    </body>
    </html>