diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-02-06 09:47:10 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-02-06 09:47:10 +0000 |
commit | c5665276b7a465f29230341f8800af4f77b3c141 (patch) | |
tree | 63789c1cb348d3a95bfe3a11dd4852809191202e /app/controllers | |
parent | 942651ecdfd860b9da68254b2a7d0919dc8151d2 (diff) | |
download | redmine-c5665276b7a465f29230341f8800af4f77b3c141.tar.gz redmine-c5665276b7a465f29230341f8800af4f77b3c141.zip |
REST API for project memberships (#7420).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8798 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/members_controller.rb | 89 |
1 files changed, 64 insertions, 25 deletions
diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index ae2f57a13..80328aa7f 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -17,29 +17,52 @@ class MembersController < ApplicationController model_object Member - before_filter :find_model_object, :except => [:create, :autocomplete] - before_filter :find_project_from_association, :except => [:create, :autocomplete] - before_filter :find_project_by_project_id, :only => [:create, :autocomplete] + before_filter :find_model_object, :except => [:index, :create, :autocomplete] + before_filter :find_project_from_association, :except => [:index, :create, :autocomplete] + before_filter :find_project_by_project_id, :only => [:index, :create, :autocomplete] before_filter :authorize + accept_api_auth :index, :show, :create, :update, :destroy + + def index + @offset, @limit = api_offset_and_limit + @member_count = @project.member_principals.count + @member_pages = Paginator.new self, @member_count, @limit, params['page'] + @offset ||= @member_pages.current.offset + @members = @project.member_principals.all( + :order => "#{Member.table_name}.id", + :limit => @limit, + :offset => @offset + ) + + respond_to do |format| + format.html { head 406 } + format.api + end + end + + def show + respond_to do |format| + format.html { head 406 } + format.api + end + end def create members = [] - if params[:membership] && request.post? + if params[:membership] && params[:membership][:user_ids] attrs = params[:membership].dup - if (user_ids = attrs.delete(:user_ids)) - user_ids.each do |user_id| - members << Member.new(attrs.merge(:user_id => user_id)) - end - else - members << Member.new(attrs) + user_ids = attrs.delete(:user_ids) + user_ids.each do |user_id| + members << Member.new(attrs.merge(:user_id => user_id)) end - @project.members << members + else + members << Member.new(params[:membership]) end + @project.members << members + respond_to do |format| if members.present? && members.all? {|m| m.valid? } - format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } - format.js { render(:update) {|page| page.replace_html "tab-content-members", :partial => 'projects/settings/members' @@ -47,8 +70,11 @@ class MembersController < ApplicationController members.each {|member| page.visual_effect(:highlight, "member-#{member.id}") } } } + format.api { + @member = members.first + render :action => 'show', :status => :created, :location => membership_url(@member) + } else - format.js { render(:update) {|page| errors = members.collect {|m| @@ -58,7 +84,7 @@ class MembersController < ApplicationController page.alert(l(:notice_failed_to_save_members, :errors => errors.join(', '))) } } - + format.api { render_validation_errors(members.first) } end end end @@ -67,17 +93,23 @@ class MembersController < ApplicationController if params[:membership] @member.role_ids = params[:membership][:role_ids] end - if request.put? && @member.save - respond_to do |format| - format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } - format.js { - render(:update) {|page| - page.replace_html "tab-content-members", :partial => 'projects/settings/members' - page << 'hideOnLoad()' - page.visual_effect(:highlight, "member-#{@member.id}") - } + saved = @member.save + respond_to do |format| + format.html { redirect_to :controller => 'projects', :action => 'settings', :tab => 'members', :id => @project } + format.js { + render(:update) {|page| + page.replace_html "tab-content-members", :partial => 'projects/settings/members' + page << 'hideOnLoad()' + page.visual_effect(:highlight, "member-#{@member.id}") } - end + } + format.api { + if saved + head :ok + else + render_validation_errors(@member) + end + } end end @@ -92,6 +124,13 @@ class MembersController < ApplicationController page << 'hideOnLoad()' } } + format.api { + if @member.destroyed? + head :ok + else + head :unprocessable_entity + end + } end end |