#691: added #notifiers and #update actions for Users API

This commit is contained in:
Vokhmin Alexey V 2012-10-15 18:06:08 +04:00
parent 2ae671df21
commit 0879a7e46f
6 changed files with 58 additions and 17 deletions

View File

@ -10,6 +10,12 @@ class Api::V1::BaseController < ApplicationController
protected protected
def error_message(subject, message)
errors = subject.errors.full_messages.join('. ')
(message << '. ' << errors) if errors.present?
message
end
def add_member_to_subject(subject) def add_member_to_subject(subject)
class_name = subject.class.name.downcase class_name = subject.class.name.downcase
if member.present? && subject.add_member(member) if member.present? && subject.add_member(member)
@ -61,11 +67,7 @@ class Api::V1::BaseController < ApplicationController
end end
def render_validation_error(subject, message) def render_validation_error(subject, message)
errors = subject.errors.full_messages.join('. ') render_json_response(subject, error_message(subject, message), 422)
if errors.present?
message << '. ' << errors
end
render_json_response(subject, message, 422)
end end
private private

View File

@ -53,10 +53,7 @@ class Api::V1::RepositoriesController < Api::V1::BaseController
if key_pair.save if key_pair.save
render_json_response @repository, 'Signatures have been updated for repository successfully' render_json_response @repository, 'Signatures have been updated for repository successfully'
else else
message = 'Signatures have not been updated for repository' render_json_response @repository, error_message(key_pair, 'Signatures have not been updated for repository'), 422
errors = key_pair.errors.full_messages.join('. ')
(message << '. ' << errors) if errors.present?
render_json_response @repository, message, 422
end end
end end

View File

@ -3,14 +3,43 @@ class Api::V1::UsersController < Api::V1::BaseController
before_filter :authenticate_user! before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access'] skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :user, :only => :show
before_filter :set_current_user, :except => :show
def show def show
@user = User.where(:id => params[:id]).first @user = current_user if params[:id].nil?
if @user
render :show
else
render_json_response User.new, "User with id='#{params[:id]}' does not exist", 422
end end
def update
user_params = params[:user] || {}
send_confirmation = user_params[:email] != @user.email
if @user.update_without_password(user_params)
if send_confirmation
@user.confirmed_at, @user.confirmation_sent_at = nil
@user.send_confirmation_instructions
end
render_json_response @user, 'User has been updated successfully'
else
render_validation_error @user, "#{class_name} has not been updated"
end
end
def notifiers
if request.put?
if @user.notifier.update_attributes(params[:notifiers])
render_json_response @user, 'User notification settings have been updated successfully'
else
render_json_response @user, error_message(@user.notifier, 'User notification settings have not been updated'), 422
end
else
render :notifiers
end
end
protected
def set_current_user
@user = current_user
end end
end end

View File

@ -0,0 +1,8 @@
json.user do |json|
json.(@user, :id)
json.notifiers do |json_notifiers|
json_notifiers.(@user.notifier, :can_notify, :new_comment, :new_comment_reply, :new_issue, :issue_assign, :new_comment_commit_owner, :new_comment_commit_repo_owner, :new_comment_commit_commentor, :new_build, :new_associated_build)
end
end
json.url notifiers_api_v1_user_path(:json)

View File

@ -1,9 +1,8 @@
json.user do |json| json.user do |json|
json.(@user, :id, :name, :email, :uname,:language, :own_projects_count, :professional_experience, :site, :company, :location) json.(@user, :id, :name, :email, :uname,:language, :own_projects_count, :professional_experience, :site, :company, :location, :build_priority)
json.created_at @user.created_at.to_i json.created_at @user.created_at.to_i
json.updated_at @user.updated_at.to_i json.updated_at @user.updated_at.to_i
json.avatar_url avatar_url(@user,:big)
json.url api_v1_user_path(@user.id, :format => :json) json.url api_v1_user_path(@user.id, :format => :json)
json.html_url user_path(@user.uname) json.html_url user_path(@user.uname)
end end
json.url api_v1_user_path(@user.id, :format => :json)

View File

@ -48,6 +48,12 @@ Rosa::Application.routes.draw do
} }
end end
resources :users, :only => [:show] resources :users, :only => [:show]
resource :user, :only => [:show, :update] do
member {
get :notifiers
put :notifiers
}
end
end end
end end