Skip to content

Instantly share code, notes, and snippets.

@IvanShamatov
Created July 17, 2011 21:45
Show Gist options
  • Save IvanShamatov/1088105 to your computer and use it in GitHub Desktop.
Save IvanShamatov/1088105 to your computer and use it in GitHub Desktop.
#model/article.rb
class Article
include DataMapper::Resource
property :id, Integer, :serial => true
property :title, String, :nullable => false
property :body, Text
property :created_at, DateTime
property :updated_at, DateTime
default_scope(:default).update(:order => [:created_at.desc])
# DataMapper.auto_upgrade!
end
DataMapper.auto_migrate!
#controller/article.rb
# CREATE
get '/admin/articles/new' do
protected!
if session[:error]=='' then
@error=''
else
@error='Sorry.'
session[:error]=''
end
erb :'admin/articles/new', :layout => :'admin/layout'
end
post '/admin/articles/new' do
protected!
@article = Article.new
@article.attributes = {:title=>params['title'], :body=>params['body'], :created_at=>Time.now}
if @article.save then
redirect '/admin/articles'
else
session[:error] = 'Something wrong'
redirect '/admin/article/new'
end
end
# READ
get '/admin/articles' do
protected!
@articles = Article.all
erb :'/admin/articles', :layout => :'admin/layout'
end
get '/articles' do
@articles = Article.all
erb :'/articles', :layout => :'layout'
end
# UPDATE
get '/admin/articles/edit/:id' do
protected!
@article = Article.get(params[:id])
erb :'admin/articles/edit', :layout => :'admin/layout'
end
post '/admin/articles/edit/:id' do
@error=''
protected!
@article = Article.get(params[:id])
if @article.update(:title=>params['title'], :body=>params['body'], :updated_at=>Time.now) then
session[:success] = 'Oh yeah baby! IZMENENIA SOHRANENY'
redirect '/admin/articles'
else
session[:error] = 'SORRY.'
redirect ('/admin/articles/edit/'+params[:id])
end
end
# DELETE
get '/admin/articles/delete/:id' do
protected!
'deleeeeteeee #{params[:id]}'
@article = Article.get(params[:id])
@article.destroy
session[:success] = 'Article deleted.'
redirect '/admin/articles'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment