Revision d00e1bee
Added by Tomáš Strachota almost 12 years ago
- ID d00e1beeaa176e7e18dafbcbc7004976ff51357b
app/controllers/api/base_controller.rb | ||
---|---|---|
#TODO: inherit from application controller after cleanup
|
||
class BaseController < ActionController::Base
|
||
|
||
before_filter :set_default_response_format
|
||
|
||
protected
|
||
# searches for an object based on its name and assign it to an instance variable
|
||
# required for models which implement the to_param method
|
||
... | ... | |
head :status => 404
|
||
end
|
||
|
||
def set_default_response_format
|
||
request.format = :json if params[:format].nil?
|
||
end
|
||
|
||
end
|
||
end
|
app/controllers/api/v1/bookmarks_controller.rb | ||
---|---|---|
before_filter :find_by_name, :only => [:show, :update, :destroy]
|
||
|
||
def index
|
||
@bookmarks = Bookmark.paginate(:page => params[:page])
|
||
@bookmarks = Bookmark.all
|
||
end
|
||
|
||
def show
|
||
end
|
||
|
||
def create
|
||
respond_with Bookmark.create(params[:bookmark])
|
||
@bookmark = Bookmark.create(params[:bookmark])
|
||
end
|
||
|
||
def update
|
||
respond_with @bookmark.update_attributes(params[:bookmark])
|
||
@bookmark.update_attributes(params[:bookmark])
|
||
end
|
||
|
||
def destroy
|
app/views/api/v1/bookmarks/create.json.rabl | ||
---|---|---|
./show.json.rabl
|
test/functional/api/v1/bookmarks_controller_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Api::V1::BookmarksControllerTest < ActionController::TestCase
|
||
|
||
|
||
bookmark_base = {
|
||
:public => false,
|
||
:controller => "hosts"
|
||
}
|
||
|
||
simple_bookmark = bookmark_base.merge({
|
||
:name => "foo-bar",
|
||
:query => "bar"
|
||
})
|
||
|
||
dot_bookmark = bookmark_base.merge({
|
||
:name => "facts.architecture",
|
||
:query => " facts.architecture = x86_64"
|
||
})
|
||
|
||
|
||
test "should get index" do
|
||
get :index, {}, set_session_user
|
||
assert_response :success
|
||
assert_not_nil assigns(:bookmarks)
|
||
end
|
||
|
||
test "should show bookmark" do
|
||
get :show, {:id => :one}, set_session_user
|
||
assert_response :success
|
||
end
|
||
|
||
test "should create bookmark" do
|
||
User.current = users(:one)
|
||
assert_difference('Bookmark.count') do
|
||
post :create, {:bookmark => simple_bookmark}, set_session_user
|
||
end
|
||
assert_response :success
|
||
end
|
||
|
||
test "should create bookmark with a dot" do
|
||
User.current = users(:one)
|
||
assert_difference('Bookmark.count') do
|
||
post :create, {:bookmark => dot_bookmark}, set_session_user
|
||
end
|
||
assert_response :success
|
||
end
|
||
|
||
|
||
test "should update bookmark" do
|
||
put :update, {:id => bookmarks(:one).to_param, :bookmark => {} }, set_session_user
|
||
assert_response :success
|
||
end
|
||
|
||
test "should destroy bookmark" do
|
||
assert_difference('Bookmark.count', -1) do
|
||
delete :destroy, {:id => bookmarks(:one).to_param}, set_session_user
|
||
end
|
||
assert_response :success
|
||
end
|
||
end
|
Also available in: Unified diff
api v1 - fisrt version of bookmarks controller