Created
April 16, 2013 07:40
-
-
Save pzuraq/5394132 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
Case.RESTAdapter = DS.RESTAdapter.extend({ | |
serializer: Case.RESTSerializer, | |
createRecord: function(store, type, record) { | |
if(type == 'Case.VehiclePhoto') { | |
this.createPhotoRecord(store, type, record); | |
} else { | |
var root = this.rootForType(type); | |
var data = {}; | |
data[root] = this.serialize(record, { includeId: true }); | |
this.ajax(this.buildURL(root), "POST", { | |
data: data, | |
context: this, | |
success: function(json) { | |
Ember.run(this, function(){ | |
this.didCreateRecord(store, type, record, json); | |
}); | |
}, | |
error: function(xhr) { | |
this.didError(store, type, record, xhr); | |
} | |
}); | |
} | |
}, | |
createPhotoRecord: function(store, type, record) { | |
var root = this.rootForType(type), json = {}; | |
console.log(record); | |
var data = new FormData(); | |
console.log(record.get('vehicleId')); | |
data.append('vehicle_id', record.get('vehicleId')) | |
data.append('name', record.get('name')); | |
data.append('image', record.get('image')); | |
this.fileAjax(this.buildURL(root), "POST", { | |
data: data, | |
context: this, | |
success: function(preJSON) { | |
console.log('success'); | |
json[root] = preJSON; | |
this.didCreateRecord(store, type, record, json); | |
} | |
}); | |
}, | |
fileAjax: function(url, type, hash) { | |
hash.url = url; | |
hash.type = type; | |
hash.contentType = false; | |
hash.processData = false; | |
hash.context = this; | |
jQuery.ajax(hash); | |
} | |
}); |
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
Started POST "/vehicle_photos" for 127.0.0.1 at 2013-04-16 00:25:42 -0700 | |
Processing by VehiclePhotosController#create as */* | |
Parameters: {"vehicle_id"=>"1001", "name"=>"crossing-the-gap.jpg", "image"=>"data:image/jpeg;base64,/9j/4RDaRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAcAAAAcgEyAAIAAAAUAAAAjodpAAQAAAABAAAApAAAANAACvyAAAAnEAAK/IAAACcQQWRvYmUgUGhvdG9zaG9wIENTNSBXaW5kb3dzADIwMTI6MDQ6MTAgMTI6NTU6MTIAAAAAA6ABAAMAAAABAAEAAKACA...lots more base64 goodness"} | |
[1m[36m (0.2ms)[0m [1mBEGIN[0m | |
[1m[35mSQL (0.3ms)[0m INSERT INTO `vehicle_photos` (`created_at`, `image`, `name`, `position`, `updated_at`, `vehicle_id`) VALUES ('2013-04-16 07:25:43', NULL, 'crossing-the-gap.jpg', NULL, '2013-04-16 07:25:43', 1001) | |
[1m[36m (1.8ms)[0m [1mCOMMIT[0m | |
Redirected to http://case.dev/vehicle_photos/7 | |
Completed 302 Found in 88ms (ActiveRecord: 6.0ms) |
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
class VehiclePhoto < ActiveRecord::Base | |
attr_accessible :image, :vehicle_id, :position, :name | |
belongs_to :vehicle | |
mount_uploader :image, VehiclePhotoUploader | |
end |
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
class VehiclePhotoSerializer < ActiveModel::Serializer | |
attributes :id, :vehicle_id, :image, :position, :name | |
end |
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
# encoding: utf-8 | |
class VehiclePhotoUploader < CarrierWave::Uploader::Base | |
# Include RMagick or MiniMagick support: | |
# include CarrierWave::RMagick | |
include CarrierWave::MiniMagick | |
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: | |
# include Sprockets::Helpers::RailsHelper | |
# include Sprockets::Helpers::IsolatedHelper | |
# Choose what kind of storage to use for this uploader: | |
storage :file | |
# storage :fog | |
# 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 | |
# # For Rails 3.1+ asset pipeline compatibility: | |
# # asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) | |
# | |
# "/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 => [296, 150] | |
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: | |
# Avoid using model.id or version_name here, see uploader/store.rb for details. | |
# def filename | |
# "something.jpg" if original_filename | |
# end | |
end |
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
class VehiclePhotosController < ApplicationController | |
# GET /vehicle_photos | |
# GET /vehicle_photos.json | |
def index | |
@vehicle_photos = VehiclePhoto.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @vehicle_photos } | |
end | |
end | |
# GET /vehicle_photos/1 | |
# GET /vehicle_photos/1.json | |
def show | |
@vehicle_photo = VehiclePhoto.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @vehicle_photo } | |
end | |
end | |
# GET /vehicle_photos/new | |
# GET /vehicle_photos/new.json | |
def new | |
@vehicle_photo = VehiclePhoto.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @vehicle_photo } | |
end | |
end | |
# GET /vehicle_photos/1/edit | |
def edit | |
@vehicle_photo = VehiclePhoto.find(params[:id]) | |
end | |
# POST /vehicle_photos | |
# POST /vehicle_photos.json | |
def create | |
@vehicle_photo = VehiclePhoto.new(params.slice(:name, :vehicle_id, :image, :position)) | |
respond_to do |format| | |
if @vehicle_photo.save | |
format.html { redirect_to @vehicle_photo, notice: 'Vehicle photo was successfully created.' } | |
format.json { render json: @vehicle_photo, status: :created, location: @vehicle_photo } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @vehicle_photo.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /vehicle_photos/1 | |
# PUT /vehicle_photos/1.json | |
def update | |
@vehicle_photo = VehiclePhoto.find(params[:id]) | |
respond_to do |format| | |
if @vehicle_photo.update_attributes(params[:vehicle_photo]) | |
format.html { redirect_to @vehicle_photo, notice: 'Vehicle photo was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @vehicle_photo.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /vehicle_photos/1 | |
# DELETE /vehicle_photos/1.json | |
def destroy | |
@vehicle_photo = VehiclePhoto.find(params[:id]) | |
@vehicle_photo.destroy | |
respond_to do |format| | |
format.html { redirect_to vehicle_photos_url } | |
format.json { head :no_content } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment