Created
August 19, 2011 19:52
-
-
Save kmarsh/1157829 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment