Orbit/app/controllers/admin/snippets_controller.rb

99 lines
2.4 KiB
Ruby

class Admin::SnippetsController < ApplicationController
layout "admin"
# GET /snippets
# GET /snippets.xml
def index
@snippets = Snippet.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @snippets }
end
end
# GET /snippets/1
# GET /snippets/1.xml
def show
@snippet = Snippet.find(params[:id])
redirect_to "/#{@snippet.name}"
end
# GET /snippets/new
# GET /snippets/new.xml
def new
@snippet = Snippet.new
if params[:menu]
@snippet.name = "#{params[:menu]}_nav"
lis = Page.find(:all, :conditions => { :parent_page_id => params[:menu], :is_published => true }, :order => "position" ).map do |page|
if page.external_link.blank?
"<li><a href='/#{page.name}'>#{page.name}</a></li>\n"
else
"<li><a href='#{page.external_link}'>#{page.name}</a></li>\n"
end
end
@snippet.content = "<ul>\n#{lis}</ul>"
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @snippets }
end
end
# GET /snippets/1/edit
def edit
@snippet = Snippet.find(params[:id])
end
# POST /snippets
# POST /snippets.xml
def create
@snippet = Snippet.new(params[:snippet])
respond_to do |format|
if @snippet.save
flash[:notice] = 'Snippet was successfully created.'
format.html { redirect_to admin_snippets_url }
format.xml { render :xml => @snippet, :status => :created, :location => @snippets }
else
format.html { render :action => "new" }
format.xml { render :xml => @snippet.errors, :status => :unprocessable_entity }
end
end
end
# PUT /snippets/1
# PUT /snippets/1.xml
def update
@snippet = Snippet.find(params[:id])
respond_to do |format|
if @snippet.update_attributes(params[:snippet])
flash[:notice] = 'Snippet was successfully updated.'
format.html { redirect_to admin_snippets_url }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @snippet.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /snippets/1
# DELETE /snippets/1.xml
def destroy
@snippet = Snippet.find(params[:id])
@snippet.destroy
respond_to do |format|
format.html { redirect_to admin_snippets_url }
format.xml { head :ok }
end
end
end