Permalink
Browse files

Asset uploader files with mongodb

  • Loading branch information...
1 parent 665973b commit 1699dd82ef07010a766c4351072d8b9da0613f73 @vimalraj vimalraj committed Feb 16, 2012
@@ -0,0 +1,42 @@
+<div>
+ <div id="filelist"></div>
+ <br />
+ <a id="pickfiles" href="#">[Select files]</a>
+ <a id="uploadfiles" href="#">[Upload files]</a>
+</div>
+
+<script type="text/javascript">
+$(function(){
+ var uploader = new plupload.Uploader({
+ runtimes : "html5",
+ browse_button : 'pickfiles',
+ max_file_size : '10mb',
+ url : "/assets",
+ multipart: true,
+ multipart_params: {
+ "authenticity_token" : '<%= form_authenticity_token %>'
+ }
+ });
+
+ uploader.bind('FilesAdded', function(up, files) {
+ $.each(files, function(i, file) {
+ $('#filelist').append(
+ '<div id="' + file.id + '">' +
+ 'File: ' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
+ '</div>'
+ );
+ });
+ });
+
+ uploader.bind('UploadProgress', function(up, file) {
+ $('#' + file.id + " b").html(file.percent + "%");
+ });
+
+ $('#uploadfiles').click(function(e) {
+ uploader.start();
+ e.preventDefault();
+ });
+
+ uploader.init();
+});
+</script>
View
@@ -0,0 +1,6 @@
+class Asset
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ mount_uploader :asset, AssetUploader
+end
@@ -0,0 +1,48 @@
+# encoding: utf-8
+
+class AssetUploader < CarrierWave::Uploader::Base
+
+ include CarrierWave::MiniMagick
+ # Choose what kind of storage to use for this uploader:
+ storage :file
+ # storage :s3
+
+ # Override the directory where uploaded files will be stored.
+ # This is a sensible default for uploaders that are meant to be mounted:
+ def store_dir
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
+ end
+
+ # Provide a default URL as a default if there hasn't been a file uploaded:
+ # def default_url
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
+ # end
+
+ # Process files as they are uploaded:
+ # process :scale => [200, 300]
+ #
+ # def scale(width, height)
+ # # do something
+ # end
+
+ # Create different versions of your uploaded files:
+ # version :thumb do
+ # process :scale => [50, 50]
+ # end
+
+ version :thumb do
+ process :resize_to_fill => [80,80]
+ end
+
+ # Add a white list of extensions which are allowed to be uploaded.
+ # For images you might use something like this:
+ # def extension_white_list
+ # %w(jpg jpeg gif png)
+ # end
+
+ # Override the filename of the uploaded files:
+ # def filename
+ # "something.jpg" if original_filename
+ # end
+
+end
@@ -0,0 +1,44 @@
+class AssetsController < ApplicationController
+
+ def index
+ @assets = Asset.all
+ end
+
+ def show
+ @asset = Asset.find(params[:id])
+ end
+
+ def new
+ @asset = Asset.new
+ end
+
+ def edit
+ @asset = Asset.find(params[:id])
+ end
+
+ def create
+ @asset = Asset.new(:asset => params[:file])
+ if @asset.save
+ head 200
+ else
+ render :action => "new"
+ end
+ end
+
+ def update
+ @asset = Asset.find(params[:id])
+
+ if @asset.update_attributes(params[:asset])
+ redirect_to(@asset, :notice => 'Asset was successfully updated.')
+ else
+ render :action => "edit"
+ end
+ end
+
+ def destroy
+ @asset = Asset.find(params[:id])
+ @asset.destroy
+ redirect_to(assets_url)
+ end
+end
+
@@ -0,0 +1,6 @@
+<h1>Editing asset</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @asset %> |
+<%= link_to 'Back', assets_path %>
@@ -0,0 +1,21 @@
+<h1>Listing assets</h1>
+
+<table>
+ <tr>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @assets.each do |asset| %>
+ <tr>
+ <td><%= link_to 'Show', asset %></td>
+ <td><%= link_to 'Edit', edit_asset_path(asset) %></td>
+ <td><%= link_to 'Destroy', asset, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Asset', new_asset_path %>
@@ -0,0 +1,7 @@
+<%= javascript_include_tag 'plupload.min', 'plupload.html5.min' %>
+
+<h1>New asset</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', assets_path %>
@@ -0,0 +1,6 @@
+<p id="notice"><%= notice %></p>
+
+<%= image_tag @asset.asset.url(:thumb) %>
+
+<%= link_to 'Edit', edit_asset_path(@asset) %> |
+<%= link_to 'Back', assets_path %>

0 comments on commit 1699dd8

Please sign in to comment.