-
-
Save tagrudev/5143707 to your computer and use it in GitHub Desktop.
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 PerspectivesController < ApplicationController | |
# GET /perspectives | |
# GET /perspectives.json | |
def index | |
@perspectives = Perspective.all | |
unless params['perspective'] | |
@perspective = @perspectives.where(default: true).first || Perspective.new | |
else | |
@perspective = Perspective.new | |
end | |
respond_to do |format| | |
format.html | |
format.json { render json: @perspectives } | |
end | |
end | |
# GET /perspectives/1 | |
# GET /perspectives/1.json | |
def show | |
@perspective = current_resource | |
respond_to do |format| | |
format.html | |
format.json { render json: @perspective } | |
end | |
end | |
# GET /perspectives/new | |
# GET /perspectives/new.json | |
def new | |
@perspective = Perspective.new | |
respond_to do |format| | |
format.html | |
format.json { render json: @perspective } | |
end | |
end | |
# GET /perspectives/1/edit | |
def edit | |
@perspective = current_resource | |
end | |
# POST /perspectives | |
# POST /perspectives.json | |
def create | |
@perspective = Perspective.new(params[:perspective]) | |
@perspective.user = current_user | |
respond_to do |format| | |
if @perspective.save | |
format.html { redirect_to @perspective, notice: 'Perspective was successfully created.' } | |
format.json { render json: @perspective, status: :created, location: @perspective } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @perspective.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /perspectives/1 | |
# PUT /perspectives/1.json | |
def update | |
@perspective = current_resource | |
respond_to do |format| | |
if @perspective.update_attributes(params[:perspective]) | |
format.html { redirect_to @perspective, notice: 'Perspective was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @perspective.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /perspectives/1 | |
# DELETE /perspectives/1.json | |
def destroy | |
@perspective = current_resource | |
@perspective.destroy | |
respond_to do |format| | |
format.html { redirect_to perspectives_url, notice: 'Perspective was successfully deleted.' } | |
format.json { head :no_content } | |
end | |
end | |
# Allows manually sorting perspectives | |
def sort | |
params[:perspective].each_with_index do |id, index| | |
Perspective.find(id).update_attribute(:position, index+1) | |
end | |
render nothing: true | |
end | |
private | |
def current_resource | |
@current_resource ||= Perspective.find(params[:id]) if params[:id] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment