#692: updated API

This commit is contained in:
Vokhmin Alexey V 2012-10-16 16:35:30 +04:00
parent 7327d195c4
commit 5795e1cea5
9 changed files with 55 additions and 15 deletions

View File

@ -2,6 +2,8 @@
class Api::V1::BaseController < ApplicationController
#respond_to :json
helper_method :member_path
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.json { render :json => {:message => t("flash.exception_message")}.to_json, :status => 403 }
@ -14,6 +16,15 @@ class Api::V1::BaseController < ApplicationController
[message, subject.errors.full_messages].flatten.join('. ')
end
def create_subject(subject)
class_name = subject.class.name
if subject.save
render_json_response subject, "#{class_name} has been created successfully"
else
render_validation_error subject, "#{class_name} has not been created"
end
end
def add_member_to_subject(subject)
class_name = subject.class.name.downcase
if member.present? && subject.add_member(member)
@ -68,6 +79,14 @@ class Api::V1::BaseController < ApplicationController
render_json_response(subject, error_message(subject, message), 422)
end
def member_path(subject)
if subject.is_a?(User)
api_v1_user_path(subject.id, :format => :json)
else
api_v1_group_path(subject.id, :format => :json)
end
end
private
def member

View File

@ -3,7 +3,7 @@ class Api::V1::GroupsController < Api::V1::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :group
load_and_authorize_resource
def index
# accessible_by(current_ability)
@ -13,9 +13,24 @@ class Api::V1::GroupsController < Api::V1::BaseController
def show
end
def update
def members
@members = @group.members.
where('actor_id != ?', @group.owner_id).
order('name').paginate(paginate_params)
end
def update
update_subject @group
end
def destroy
destroy_subject @group
end
def create
@group = Group.new params[:group]
@group.owner = current_user
create_subject @group
end
end

View File

@ -22,11 +22,7 @@ class Api::V1::PlatformsController < Api::V1::BaseController
platform_params = params[:platform] || {}
owner = User.where(:id => platform_params[:owner_id]).first
@platform.owner = owner || get_owner
if @platform.save
render_json_response @platform, 'Platform has been created successfully'
else
render_validation_error @platform, 'Platform has not been created'
end
create_subject @platform
end
def update

View File

@ -5,7 +5,7 @@ json.groups @groups do |json, group|
json.owner do |json_owner|
json_owner.(group.owner, :id, :name)
json_owner.type 'User'
json_owner.url api_v1_user_path(group.owner.id, :format => :json)
json_owner.url api_v1_user_path(group.owner_id, :format => :json)
end
json.avatar_url avatar_url(group, :big)
json.url api_v1_group_path(group.id, :format => :json)

View File

@ -0,0 +1,9 @@
json.group do |json|
json.(@group, :id)
json.members @members do |json_members, member|
json_members.(member, :id)
json_members.type member.class.name
json_members.url member_path(member)
end
end
json.url members_api_v1_group_path(@group.id, :format => :json)

View File

@ -5,7 +5,7 @@ json.group do |json|
json.owner do |json_owner|
json_owner.(@group.owner, :id, :name)
json_owner.type 'User'
json_owner.url api_v1_user_path(@group.owner.id, :format => :json)
json_owner.url api_v1_user_path(@group.owner_id, :format => :json)
end
json.avatar_url avatar_url(@group, :big)
json.url api_v1_group_path(@group.id, :format => :json)

View File

@ -3,13 +3,13 @@ json.platforms @platforms do |json, platform|
json.owner do |json_owner|
json_owner.(platform.owner, :id, :name)
json_owner.type platform.owner_type
json_owner.url url_for(platform.owner)
json_owner.url member_path(platform.owner)
end
json.repositories platform.repositories do |json_repos, repo|
json_repos.(repo, :id, :name)
json_repos.url api_v1_repository_path(repo.id, :format => :json)
end
json.url api_v1_platform_path(platform.name, :format => :json)
json.url api_v1_platform_path(platform.id, :format => :json)
end
json.url api_v1_platforms_path(:format => :json)

View File

@ -3,6 +3,7 @@ json.platform do |json|
json.members @members do |json_members, member|
json_members.(member, :id)
json_members.type member.class.name
json_members.url member_path(member)
end
end
json.url members_api_v1_platform_path(@platform, :format => :json)
json.url members_api_v1_platform_path(@platform.id, :format => :json)

View File

@ -5,11 +5,11 @@ json.platform do |json|
json.owner do |json_owner|
json_owner.(@platform.owner, :id, :name)
json_owner.type @platform.owner_type
json_owner.url url_for(@platform.owner)
json_owner.url member_path(@platform.owner)
end
json.repositories @platform.repositories do |json_repos, repo|
json_repos.(repo, :id, :name)
json_repos.url api_v1_repository_path(repo.id, :format => :json)
end
end
json.url api_v1_platform_path(@platform, :format => :json)
json.url api_v1_platform_path(@platform.id, :format => :json)