Skip to content

Instantly share code, notes, and snippets.

@kibaekr
Created December 13, 2012 04:23
Show Gist options
  • Save kibaekr/4274025 to your computer and use it in GitHub Desktop.
Save kibaekr/4274025 to your computer and use it in GitHub Desktop.
# encoding: utf-8
class TrackImageUploader < CarrierWave::Uploader::Base
def cache_dir
"#{Rails.root}/tmp/uploads"
end
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# 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
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
process :resize_to_fit => [500, 500]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :featured do
process :resize_to_fill => [210, 145]
end
# Create another version for track index page. possibly 5 or 6 tracks in one
# 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
# GET /tracks/new
# GET /tracks/new.json
def new
@track = Track.new
@missions = @track.missions.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @track }
end
end
# GET /tracks/1/edit
def edit
@track = Track.find(params[:id])
#@missions = @track.missions.order("position ASC")
end
# POST /tracks
# POST /tracks.json
def create
if params[:commit] == "Publish Track"
trackordraft = "Track"
#@track = current_user.creations.build(params[:track])
#@mission = @track.mission.build(params[:missions_attributes][0])
@track = Track.new(:title => params[:track][:title], :category => params[:track][:category], :difficulty => params[:track][:difficulty], :description => params[:track][:description], :author_id => current_user.id)
@track.tag_list = params[:track][:tag_list]
#@track = Track.new(params[:track])
@track.author_id = current_user.id
@track.save!
params[:track][:missions_attributes].each do |a, b|
@mission = Mission.new(:title => b[:title], :content => b[:content], :track_id => @track.id, :author_id => current_user.id, :category => @track.category)
@mission.tag_list = @track.tag_list
@mission.save!
end
elsif params[:commit] == "Save Draft"
@track = current_user.creations.build(params[:track].merge(:active => false))
trackordraft = "Draft"
end
respond_to do |format|
if @track.save
if params[:commit] == "Publish Track"
@track.author.increment!(:userpoints, 25)
format.html { redirect_to @track, notice: "You've earned 25 Street Creds for creating " + trackordraft + "! Go to your profile if you want to update the track." }
format.json { render json: @track, status: :created, location: @track }
elsif params[:commit] == "Save Draft"
format.html { redirect_to edit_track_path(@track), notice: trackordraft + ' was successfully saved. You can keep writing or come back later and continue where you left off in your profile page' }
format.json { head :ok }
end
else
format.html { render action: "new" }
format.json { render json: @track.errors, status: :unprocessable_entity }
end
end
end
# PUT /tracks/1
# PUT /tracks/1.json
def update
@track = Track.find(params[:id])
@missions = @track.missions
respond_to do |format|
if (@track.author == current_user) || (current_user.admin == true)
if params[:commit] == "Publish Track"
if @track.active == false
@track.created_at = Time.now
end
params[:track].merge!(:active => true)
trackordraft = "Track"
elsif params[:commit] == "Save Draft"
trackordraft = "Draft"
params[:track].merge!(:active => false)
end
if @track.update_attributes(params[:track])
if params[:commit] == "Publish Track"
format.html { redirect_to @track, notice: trackordraft + ' was successfully updated.' }
format.json { head :ok }
elsif params[:commit] == "Save Draft"
format.html { redirect_to edit_track_path(@track), notice: trackordraft + ' was successfully saved. You can keep writing or come back later and continue where you left off in your profile page' }
format.json { head :ok }
end
else
format.html { render action: "edit" }
format.json { render json: @track.errors, status: :unprocessable_entity }
end
end
format.html { redirect_to @track, alert: 'You must be the track author in order to update this track' }
format.json { head :ok }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment