Created
July 9, 2012 16:16
-
-
Save kibaekr/3077397 to your computer and use it in GitHub Desktop.
missions controller full
This file contains 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 MissionsController < ApplicationController | |
# GET /missions | |
# GET /missions.json | |
before_filter :store_location | |
before_filter :authenticate_user!, :except => [:show, :index] | |
def vote_for_mission | |
@mission = Mission.find(params[:id]) | |
@originalpage = session[:page_a] | |
puts 'vvvvvvv22 below is original page that should be missions_path, not user sign in' | |
puts @originalpage | |
puts session[:page_a] | |
if @mission.voted_by?(current_user) | |
redirect_to request.referer, alert: 'You already voted on this mission.' | |
else | |
@mission.increment!(:karma) | |
@mission.active = true | |
@mission.real_author.increment!(:userpoints) unless @mission.real_author.blank? | |
current_user.vote_for(@mission) | |
redirect_to request.referer, notice: 'Your vote was successfully recorded.' | |
end | |
end | |
def vote_against_mission | |
@mission = Mission.find(params[:id]) | |
if @mission.voted_by?(current_user) | |
redirect_to request.referer, alert: 'You already voted on this mission.' | |
else | |
@mission.decrement!(:karma) | |
@mission.real_author.decrement!(:userpoints) unless @mission.real_author.blank? | |
current_user.vote_against(@mission) | |
redirect_to request.referer, notice: 'Your vote was successfully recorded.' | |
end | |
end | |
def index | |
#how would we separate the query, category, and sort | |
# so that multiple variations can apply without having to bloat up the code for each | |
# individual case? -keith 5.18 | |
if params[:query].blank? | |
if params[:category].blank? | |
if params[:sort].blank? | |
@missions = Mission.where(:active => true).order("karma desc").page(params[:page]).per(12) | |
else | |
case params[:sort] | |
when "Top" | |
@missions = Mission.where(:active => true).order("karma desc").page(params[:page]).per(12) | |
when "Recent" | |
@missions = Mission.where(:active => true).order("created_at desc").page(params[:page]).per(12) | |
end | |
end | |
else | |
if params[:sort].blank? | |
@missions = Mission.where(:category => params[:category], :active => true).order("created_at desc").page(params[:page]).per(12) | |
else | |
case params[:sort] | |
when "Top" | |
@missions = Mission.where(:category => params[:category], :active => true).order("karma desc").page(params[:page]).per(12) | |
when "Recent" | |
@missions = Mission.where(:category => params[:category], :active => true).order("created_at desc").page(params[:page]).per(12) | |
end | |
end | |
end | |
else | |
@missions = Mission.search_by_weight(params[:query]).page(params[:page]).per(12) | |
#expand on this so it takes into consideration sorting by top or recent or combination | |
end | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @missions } | |
end | |
end | |
# GET /missions/1 | |
# GET /missions/1.json | |
def show | |
@mission = Mission.find(params[:id]) | |
@author = @mission.real_author | |
@comments = @mission.mission_comments #unless @mission.mission_comments.blank? | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @mission } | |
end | |
end | |
# GET /missions/new | |
# GET /missions/new.json | |
def new | |
@mission = Mission.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.json { render json: @mission } | |
end | |
end | |
# GET /missions/1/edit | |
def edit | |
@mission = Mission.find(params[:id]) | |
end | |
# POST /missions | |
# POST /missions.json | |
def create | |
@mission = Mission.new(params[:mission]) | |
@mission.author_id = current_user.id | |
@mission.active = true | |
respond_to do |format| | |
if @mission.save | |
@mission.author.increment!(:userpoints, 5) | |
format.html { redirect_to @mission, notice: "You've earned 5 Street Creds for creating a mission!" } | |
format.json { render json: @mission, status: :created, location: @mission } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @mission.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /missions/1 | |
# PUT /missions/1.json | |
def update | |
@mission = Mission.find(params[:id]) | |
respond_to do |format| | |
if (@mission.real_author == current_user) || (current_user.admin == true) | |
if @mission.update_attributes(params[:mission]) | |
format.html { redirect_to @mission, notice: 'Mission was successfully updated.' } | |
format.json { head :ok } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @mission.errors, status: :unprocessable_entity } | |
end | |
else | |
format.html { redirect_to @mission, alert: 'You must be the Mission author in order to update this mission' } | |
format.json { head :ok } | |
end | |
end | |
end | |
# DELETE /missions/1 | |
# DELETE /missions/1.json | |
def destroy | |
@mission = Mission.find(params[:id]) | |
if (@mission.real_author == current_user) || (current_user.admin == true) | |
@mission.destroy | |
respond_to do |format| | |
@mission.real_author.decrement!(:userpoints, 5) | |
format.html { redirect_to missions_url } | |
format.json { head :ok } | |
end | |
else | |
respond_to do |format| | |
format.html { redirect_to @mission, alert: 'You do not have permission to delete this mission' } | |
format.json { head :ok } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment