Skip to content

Commit

Permalink
Merge pull request lokka#167 from tomykaira/admin_404
Browse files Browse the repository at this point in the history
  • Loading branch information
komagata committed Feb 29, 2012
2 parents a963d60 + 5d4a746 commit e950797
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 19 deletions.
37 changes: 21 additions & 16 deletions lib/lokka/app/admin.rb
Expand Up @@ -70,12 +70,12 @@ class App
end

get '/admin/comments/:id/edit' do |id|
@comment = Comment.get(id)
@comment = Comment.get(id) or raise Sinatra::NotFound
render_any :'comments/edit'
end

put '/admin/comments/:id' do |id|
@comment = Comment.get(id)
@comment = Comment.get(id) or raise Sinatra::NotFound
if @comment.update(params['comment'])
flash[:notice] = t('comment_was_successfully_updated')
redirect '/admin/comments'
Expand All @@ -91,7 +91,8 @@ class App
end

delete '/admin/comments/:id' do |id|
Comment.get(id).destroy
comment = Comment.get(id) or raise Sinatra::NotFound
comment.destroy
flash[:notice] = t('comment_was_successfully_deleted')
redirect '/admin/comments'
end
Expand Down Expand Up @@ -120,12 +121,12 @@ class App
end

get '/admin/categories/:id/edit' do |id|
@category = Category.get(id)
@category = Category.get(id) or raise Sinatra::NotFound
render_any :'categories/edit'
end

put '/admin/categories/:id' do |id|
@category = Category.get(id)
@category = Category.get(id) or raise Sinatra::NotFound
if @category.update(params['category'])
flash[:notice] = t('category_was_successfully_updated')
redirect '/admin/categories'
Expand All @@ -135,7 +136,8 @@ class App
end

delete '/admin/categories/:id' do |id|
Category.get(id).destroy
category = Category.get(id) or raise Sinatra::NotFound
category.destroy
flash[:notice] = t('category_was_successfully_deleted')
redirect '/admin/categories'
end
Expand All @@ -148,12 +150,12 @@ class App
end

get '/admin/tags/:id/edit' do |id|
@tag = Tag.get(id)
@tag = Tag.get(id) or raise Sinatra::NotFound
render_any :'tags/edit'
end

put '/admin/tags/:id' do |id|
@tag = Tag.get(id)
@tag = Tag.get(id) or raise Sinatra::NotFound
if @tag.update(params['tag'])
flash[:notice] = t('tag_was_successfully_updated')
redirect '/admin/tags'
Expand All @@ -163,7 +165,8 @@ class App
end

delete '/admin/tags/:id' do |id|
Tag.get(id).destroy
tag = Tag.get(id) or raise Sinatra::NotFound
tag.destroy
flash[:notice] = t('tag_was_successfully_deleted')
redirect '/admin/tags'
end
Expand Down Expand Up @@ -191,12 +194,12 @@ class App
end

get '/admin/users/:id/edit' do |id|
@user = User.get(id)
@user = User.get(id) or raise Sinatra::NotFound
render_any :'users/edit'
end

put '/admin/users/:id' do |id|
@user = User.get(id)
@user = User.get(id) or raise Sinatra::NotFound
if @user.update(params['user'])
flash[:notice] = t('user_was_successfully_updated')
redirect '/admin/users'
Expand All @@ -206,7 +209,7 @@ class App
end

delete '/admin/users/:id' do |id|
target_user = User.get(id)
target_user = User.get(id) or raise Sinatra::NotFound
if current_user == target_user
flash[:alert] = 'Can not delete your self.'
else
Expand Down Expand Up @@ -241,12 +244,12 @@ class App
end

get '/admin/snippets/:id/edit' do |id|
@snippet = Snippet.get(id)
@snippet = Snippet.get(id) or raise Sinatra::NotFound
render_any :'snippets/edit'
end

put '/admin/snippets/:id' do |id|
@snippet = Snippet.get(id)
@snippet = Snippet.get(id) or raise Sinatra::NotFound
if @snippet.update(params['snippet'])
flash[:notice] = t('snippet_was_successfully_updated')
redirect '/admin/snippets'
Expand All @@ -256,7 +259,8 @@ class App
end

delete '/admin/snippets/:id' do |id|
Snippet.get(id).destroy
snippet = Snippet.get(id) or raise Sinatra::NotFound
snippet.destroy
flash[:notice] = t('snippet_was_successfully_deleted')
redirect '/admin/snippets'
end
Expand Down Expand Up @@ -389,7 +393,8 @@ class App
end

delete '/admin/field_names/:id' do |id|
FieldName.get(id).destroy
field_name = FieldName.get(id) or raise Sinatra::NotFound
field_name.destroy
flash[:notice] = t('field_name_was_successfully_deleted')
redirect '/admin/field_names'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/lokka/helpers.rb
Expand Up @@ -222,7 +222,7 @@ def get_admin_entry_new(entry_class)

def get_admin_entry_edit(entry_class, id)
@name = entry_class.name.downcase
@entry = entry_class.get(id)
@entry = entry_class.get(id) or raise Sinatra::NotFound
@categories = Category.all.map {|c| [c.id, c.title] }.unshift([nil, t('not_select')])
@field_names = FieldName.all(:order => :name.asc)
render_any :'entries/edit'
Expand All @@ -248,7 +248,7 @@ def post_admin_entry(entry_class)

def put_admin_entry(entry_class, id)
@name = entry_class.name.downcase
@entry = entry_class.get(id)
@entry = entry_class.get(id) or raise Sinatra::NotFound
if params['preview']
render_preview entry_class.new(params[@name])
else
Expand All @@ -265,7 +265,7 @@ def put_admin_entry(entry_class, id)

def delete_admin_entry(entry_class, id)
name = entry_class.name.downcase
entry = entry_class.get(id)
entry = entry_class.get(id) or raise Sinatra::NotFound
entry.destroy
flash[:notice] = t("#{name}_was_successfully_deleted")
if entry.draft
Expand Down
19 changes: 19 additions & 0 deletions spec/integration/admin/categories_spec.rb
Expand Up @@ -54,4 +54,23 @@
Category(@category.id).should be_nil
end
end

context 'when the category does not exist' do
before { Category.destroy }

context 'GET' do
before { get '/admin/categories/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/categories/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/categories/9999' }
it_behaves_like 'a not found page'
end
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/comments_spec.rb
Expand Up @@ -70,4 +70,23 @@
Comment.spam.size.should == 0
end
end

context 'when the comment does not exist' do
before { Comment.destroy }

context 'GET' do
before { get '/admin/comments/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/comments/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/comments/9999' }
it_behaves_like 'a not found page'
end
end
end
9 changes: 9 additions & 0 deletions spec/integration/admin/field_names_spec.rb
Expand Up @@ -37,4 +37,13 @@
FieldName.get(@field_name.id).should be_nil
end
end

context 'when the field name does not exist' do
before { FieldName.destroy }

context 'DELETE' do
before { delete '/admin/field_names/9999' }
it_behaves_like 'a not found page'
end
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/pages_spec.rb
Expand Up @@ -65,4 +65,23 @@
Page(@page.id).should be_nil
end
end

context 'when the page does not exist' do
before { Page.destroy }

context 'GET' do
before { get '/admin/pages/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/pages/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/pages/9999' }
it_behaves_like 'a not found page'
end
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/posts_spec.rb
Expand Up @@ -65,4 +65,23 @@
Post(@post.id).should be_nil
end
end

context 'when the post does not exist' do
before { Post.destroy }

context 'GET' do
before { get '/admin/posts/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/posts/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/posts/9999' }
it_behaves_like 'a not found page'
end
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/snippets_spec.rb
Expand Up @@ -52,4 +52,23 @@
Snippet.get(@snippet.id).should be_nil
end
end

context 'when the snippet does not exist' do
before { Snippet.destroy }

context 'GET' do
before { get '/admin/snippets/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/snippets/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/snippets/9999' }
it_behaves_like 'a not found page'
end
end
end
6 changes: 6 additions & 0 deletions spec/integration/admin/spec_helper.rb
Expand Up @@ -11,3 +11,9 @@

after { User.destroy }
end

shared_examples_for 'a not found page' do
it 'should return 404' do
last_response.status.should == 404
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/tags_spec.rb
Expand Up @@ -35,4 +35,23 @@
Tag.get(@tag.id).should be_nil
end
end

context 'when the tag does not exist' do
before { Tag.destroy }

context 'GET' do
before { get '/admin/tags/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/tags/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/tags/9999' }
it_behaves_like 'a not found page'
end
end
end
19 changes: 19 additions & 0 deletions spec/integration/admin/users_spec.rb
Expand Up @@ -73,4 +73,23 @@
User.get(@user.id).should_not be_nil
end
end

context 'when the user does not exist' do
before { User.destroy }

context 'GET' do
before { get '/admin/users/9999/edit' }
it_behaves_like 'a not found page'
end

context 'PUT' do
before { put '/admin/users/9999' }
it_behaves_like 'a not found page'
end

context 'DELETE' do
before { delete '/admin/users/9999' }
it_behaves_like 'a not found page'
end
end
end

0 comments on commit e950797

Please sign in to comment.