Skip to content

Commit

Permalink
Get rid of eval for image upload!
Browse files Browse the repository at this point in the history
  • Loading branch information
gmanley committed Apr 19, 2012
1 parent 8d80a6e commit ff57739
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -10,6 +10,8 @@ group :assets do
gem 'sass-rails', '~> 3.2' gem 'sass-rails', '~> 3.2'
gem 'coffee-rails', '~> 3.2' gem 'coffee-rails', '~> 3.2'
gem 'uglifier' gem 'uglifier'
gem 'haml_coffee_assets'
gem 'execjs'
end end


gem 'jquery-rails' gem 'jquery-rails'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -115,6 +115,10 @@ GEM
activesupport (~> 3.0) activesupport (~> 3.0)
haml (~> 3.0) haml (~> 3.0)
railties (~> 3.0) railties (~> 3.0)
haml_coffee_assets (0.8.5)
execjs (>= 1.2.9)
sprockets (>= 2.0.3)
tilt (>= 1.3.3)
hike (1.2.1) hike (1.2.1)
i18n (0.6.0) i18n (0.6.0)
journey (1.0.3) journey (1.0.3)
Expand Down Expand Up @@ -256,6 +260,7 @@ DEPENDENCIES
coffeebeans coffeebeans
database_cleaner database_cleaner
devise devise
execjs
fabrication fabrication
ffaker ffaker
fog fog
Expand All @@ -264,6 +269,7 @@ DEPENDENCIES
guard-rspec guard-rspec
guard-spork guard-spork
haml-rails haml-rails
haml_coffee_assets
jquery-rails jquery-rails
kaminari kaminari
launchy launchy
Expand Down
21 changes: 7 additions & 14 deletions app/assets/javascripts/images.js.coffee
Expand Up @@ -4,13 +4,12 @@ $ ->
runtimes: 'html5,flash' runtimes: 'html5,flash'
browse_button: 'select_files' browse_button: 'select_files'
max_file_size: '10mb' max_file_size: '10mb'
chunk_size : '1mb', url: "#{window.location.pathname}/images.json"
url: "#{window.location.pathname}/images.js"
file_data_name: 'image[image]' file_data_name: 'image[image]'
flash_swf_url: '/assets/plupload.flash.swf' flash_swf_url: '/assets/plupload.flash.swf'
drop_element: 'upload_box' drop_element: 'content'
filters: filters:
{title: "Image files", extensions: "jpg,gif,png"} {title: 'Image files', extensions: 'jpg,jpeg,gif,png'}
multipart: true multipart: true
multipart_params: multipart_params:
authenticity_token: authenticity_token authenticity_token: authenticity_token
Expand All @@ -19,14 +18,7 @@ $ ->


uploader.bind "FilesAdded", (up, files) -> uploader.bind "FilesAdded", (up, files) ->
$.each files, (i, file) -> $.each files, (i, file) ->
$("#file_list").append(""" $("#file_list").append(JST['templates/file'](file: file))
<li id="#{file.id}">
<div class='file_info'>#{file.name} (#{plupload.formatSize(file.size)}) <b></b></div>
<div class="progress striped">
<div class="bar" style="width: 0%;"></div>
</div>
</li>
""")


uploader.bind 'UploadProgress', (up, file) -> uploader.bind 'UploadProgress', (up, file) ->
$("##{file.id} b").html("#{file.percent}%") $("##{file.id} b").html("#{file.percent}%")
Expand All @@ -37,8 +29,9 @@ $ ->
$(this).button('loading') $(this).button('loading')
e.preventDefault() e.preventDefault()


uploader.bind 'FileUploaded', (up, file, response) -> uploader.bind 'FileUploaded', (up, file, request) ->
eval(response.response) response = JSON.parse(request.response)
$(".thumbnails").append(JST['templates/image'](image: response.image))
$("##{file.id} .progress") $("##{file.id} .progress")
.toggleClass('active') .toggleClass('active')
.prev('.file_info b').text('Done') .prev('.file_info b').text('Done')
Expand Down
6 changes: 6 additions & 0 deletions app/assets/javascripts/templates/file.jst.hamlc
@@ -0,0 +1,6 @@
%li{id: @file.id}
.file_info
= "#{@file.name} (#{plupload.formatSize(@file.size)})"
%b
.progress.striped
.bar{style: 'width: 0%;'}
3 changes: 3 additions & 0 deletions app/assets/javascripts/templates/image.jst.hamlc
@@ -0,0 +1,3 @@
%li
%a.thumbnail{href: @image.url, rel: 'gallery'}
%img{src: @image.thumb.url }
4 changes: 2 additions & 2 deletions app/controllers/images_controller.rb
@@ -1,13 +1,13 @@
class ImagesController < ApplicationController class ImagesController < ApplicationController
respond_to :html, :json, :js respond_to :js, :json


def create def create
@album = Album.find_by_slug(params[:album_id]) @album = Album.find_by_slug(params[:album_id])
@image = @album.images.new(params[:image]) @image = @album.images.new(params[:image])
authorize!(:create, @image) authorize!(:create, @image)


@image.save @image.save
respond_with(@image) respond_with(@image, location: nil) # set location to nil as the image resource has no show
end end


def destroy def destroy
Expand Down
3 changes: 0 additions & 3 deletions app/views/images/create.js.coffee

This file was deleted.

1 change: 1 addition & 0 deletions vendor/assets/javascripts/libraries.js
Expand Up @@ -7,3 +7,4 @@
//= require plupload.base //= require plupload.base
//= require plupload.flash //= require plupload.flash
//= require plupload.html5 //= require plupload.html5
//= require hamlcoffee

0 comments on commit ff57739

Please sign in to comment.