0
0
Fork 0

added some changes to the admin layout, stylesheet and added some

templates to edit posts
This commit is contained in:
Gibheer 2011-06-30 21:39:01 +02:00
parent 1f19368e4d
commit 928be4bf8a
7 changed files with 146 additions and 16 deletions

View File

@ -4,20 +4,61 @@ class Admin < Sinatra::Base
use Rack::Flash, :accessorize => [:error, :warning, :notice]
set :haml, :layout => :admin_layout
before do
@account = session_read
get '/index' do
haml :admin_index
end
before %r{^(?!\/(login)?$)} do
if @account.nil?
flash.notice = 'something is wrong'
redirect '/admin'
get '/post' do
@posts = Post.all(:order => [:id.desc])
haml :admin_posts
end
get '/post/:id' do
@post = Post.get(params[:id])
if @post
haml :admin_post
else
session[:last_updated] = Time.now
flash.notice = 'session is valid!'
flash.warning = "Post with id #{params[:id]} not found!"
redirect './post'
end
end
post '/post/:id' do
# read the checkbox value
if params['post'].has_key?('released')
params['post']['released'] = true
else
params['post']['released'] = false
end
# get the post and update it
@post = Post.get(params[:id])
if @post
unless @post.update(params[:post])
flash.warning = 'Error at saving the post!'
flash[:errors] = true
end
haml :admin_post
else
flash.warning = "Post with id #{params[:id]} not found!"
redirect './post'
end
end
get '/post/new' do
haml :admin_post_new
end
put '/post' do
@post = Post.new(param[:post])
if @post.save
flash.notice = 'Post saved'
else
flash.error = 'Error at saving the post'
flash[:errors] = @post.errors
end
haml :admin_post_new
end
get '/' do
haml :admin_index_no_login
end
@ -32,14 +73,15 @@ class Admin < Sinatra::Base
flash.notice = 'Login successful'
session[:id] = account.id
session[:last_updated] = Time.now
redirect '/admin/index'
# redirect to the url set from the #before block
if session.has_key? :to_path
redirect "/admin#{session.delete(:to_path)}"
else
redirect '/admin/index'
end
end
end
get '/index' do
haml :admin_index
end
get '/logout' do
session[:id] = nil
session[:last_updated] = nil
@ -60,5 +102,27 @@ class Admin < Sinatra::Base
nil
end
end
def keys_to_sym hash
new_hash = {}
hash.each do |k, v|
new_hash[k.to_sym] = v
end
hash = new_hash
end
end
before do
@account = session_read
end
before %r{^(?!\/(login|stylesheet\.css)?$)} do
if @account.nil?
flash.warning = 'You are not logged in!'
session[:to_path] = request.path_info
redirect '/admin'
else
session[:last_updated] = Time.now
end
end
end

View File

@ -1,2 +1,10 @@
%a{:href => '/admin/logout'}="Logout"
%h1="Index"
#posts
%a{:href => '/admin/post'}="Posts"
-@account.posts.all(:order => [:written.desc]).each do |post|
.post
%p.title
%a{:href => "/admin/post/#{post.id}"}=post.title
%span.released{:class => "post-released-#{post.released}"}
%span.written=post.written

View File

@ -1,4 +1,3 @@
%p="==> #{@path}"
%h1="Login"
%form{:action => '/admin/login', :method => 'post'}
.username

View File

@ -15,5 +15,8 @@
-unless flash.notice.nil?
%p.notice=flash.notice
#content=yield
%script
="head.js('http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js','/public/admin.js')"
:javascript
head.js(
'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js',
'/public/admin.js'
)

33
views/admin_post.haml Normal file
View File

@ -0,0 +1,33 @@
%a{:href => '/admin/post'}="Back"
-if flash[:errors].nil?
-@post.errors.each do |error|
.error=error
%form{:action => "/admin/post/#{@post.id}", :method => :post}
.title
%label{:for => :post_title}="Title"
%input{:id => :post_title,
:name => 'post[title]',
:value => @post.title}
.author
%label{:for => :post_author}="Author"
%select{:name => 'post[account_id]', :id => :post_author}
-Account.all(:order => [:id]).each do |account|
-if @post.account == account
%option{:value => account.id, :selected => :selected}
=account.username
-else
%option{:value => account.id}=account.username
.written
%label{:for => :post_written}="written"
%input{:id => :post_written,
:name => 'post[written]', :value => @post.written}
.released
%label{:for => :post_released}="released"
%input{:type => :checkbox, :name => 'post[released]',
:class => "post-released-#{@post.released}", :value => 0,
:checked => @post.released, :id => :post_released}
%textarea.content{:name => 'post[content]'}=@post.content
.buttons
%button{:type => :submit}="Update"
%button{:type => :submit, :formmethod => :get,
:formaction => '/admin/post'}="Back"

6
views/admin_posts.haml Normal file
View File

@ -0,0 +1,6 @@
-@posts.each do |post|
.post
%p.title
%a{:href => "./post/#{post.id}"}=post.title
%span.released{:class => "post-released-#{post.released}"}
%span.written=post.written

View File

@ -0,0 +1,17 @@
#messages {
.message {
border: 1px solid black;
}
.error {
@extend .message;
background-color: red;
}
.warning {
@extend .message;
background-color: orange;
}
.notice {
@extend .message;
background-color: green;
}
}