Permalink
Browse files

Get rid of eval for image upload!

  • Loading branch information...
1 parent 8d80a6e commit ff57739e680cc1b18d8a5d5bf2115299516e551a @gmanley committed Apr 19, 2012
View
2 Gemfile
@@ -10,6 +10,8 @@ group :assets do
gem 'sass-rails', '~> 3.2'
gem 'coffee-rails', '~> 3.2'
gem 'uglifier'
+ gem 'haml_coffee_assets'
+ gem 'execjs'
end
gem 'jquery-rails'
View
6 Gemfile.lock
@@ -115,6 +115,10 @@ GEM
activesupport (~> 3.0)
haml (~> 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)
i18n (0.6.0)
journey (1.0.3)
@@ -256,6 +260,7 @@ DEPENDENCIES
coffeebeans
database_cleaner
devise
+ execjs
fabrication
ffaker
fog
@@ -264,6 +269,7 @@ DEPENDENCIES
guard-rspec
guard-spork
haml-rails
+ haml_coffee_assets
jquery-rails
kaminari
launchy
View
21 app/assets/javascripts/images.js.coffee
@@ -4,13 +4,12 @@ $ ->
runtimes: 'html5,flash'
browse_button: 'select_files'
max_file_size: '10mb'
- chunk_size : '1mb',
- url: "#{window.location.pathname}/images.js"
+ url: "#{window.location.pathname}/images.json"
file_data_name: 'image[image]'
flash_swf_url: '/assets/plupload.flash.swf'
- drop_element: 'upload_box'
+ drop_element: 'content'
filters:
- {title: "Image files", extensions: "jpg,gif,png"}
+ {title: 'Image files', extensions: 'jpg,jpeg,gif,png'}
multipart: true
multipart_params:
authenticity_token: authenticity_token
@@ -19,14 +18,7 @@ $ ->
uploader.bind "FilesAdded", (up, files) ->
$.each files, (i, file) ->
- $("#file_list").append("""
- <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>
- """)
+ $("#file_list").append(JST['templates/file'](file: file))
uploader.bind 'UploadProgress', (up, file) ->
$("##{file.id} b").html("#{file.percent}%")
@@ -37,8 +29,9 @@ $ ->
$(this).button('loading')
e.preventDefault()
- uploader.bind 'FileUploaded', (up, file, response) ->
- eval(response.response)
+ uploader.bind 'FileUploaded', (up, file, request) ->
+ response = JSON.parse(request.response)
+ $(".thumbnails").append(JST['templates/image'](image: response.image))
$("##{file.id} .progress")
.toggleClass('active')
.prev('.file_info b').text('Done')
View
6 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%;'}
View
3 app/assets/javascripts/templates/image.jst.hamlc
@@ -0,0 +1,3 @@
+%li
+ %a.thumbnail{href: @image.url, rel: 'gallery'}
+ %img{src: @image.thumb.url }
View
4 app/controllers/images_controller.rb
@@ -1,13 +1,13 @@
class ImagesController < ApplicationController
- respond_to :html, :json, :js
+ respond_to :js, :json
def create
@album = Album.find_by_slug(params[:album_id])
@image = @album.images.new(params[:image])
authorize!(:create, @image)
@image.save
- respond_with(@image)
+ respond_with(@image, location: nil) # set location to nil as the image resource has no show
end
def destroy
View
3 app/views/images/create.js.coffee
@@ -1,3 +0,0 @@
-$(".thumbnails").append "<%= escape_javascript(render 'image', image: @image) %>"
-authenticity_token = "<%= form_authenticity_token %>"
-session_token = "<%= request.session_options[:id] %>"
View
1 vendor/assets/javascripts/libraries.js
@@ -7,3 +7,4 @@
//= require plupload.base
//= require plupload.flash
//= require plupload.html5
+//= require hamlcoffee

0 comments on commit ff57739

Please sign in to comment.