diff app/controllers/groups_controller.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42
line wrap: on
line diff
--- a/app/controllers/groups_controller.rb	Wed Jun 27 14:54:18 2012 +0100
+++ b/app/controllers/groups_controller.rb	Mon Jan 07 12:01:42 2013 +0000
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -19,51 +19,34 @@
   layout 'admin'
 
   before_filter :require_admin
+  before_filter :find_group, :except => [:index, :new, :create]
+  accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user
 
   helper :custom_fields
 
-  # GET /groups
-  # GET /groups.xml
   def index
-    @groups = Group.find(:all, :order => 'lastname')
+    @groups = Group.sorted.all
 
     respond_to do |format|
-      format.html # index.html.erb
-      format.xml  { render :xml => @groups }
+      format.html
+      format.api
     end
   end
 
-  # GET /groups/1
-  # GET /groups/1.xml
   def show
-    @group = Group.find(params[:id])
-
     respond_to do |format|
-      format.html # show.html.erb
-      format.xml  { render :xml => @group }
+      format.html
+      format.api
     end
   end
 
-  # GET /groups/new
-  # GET /groups/new.xml
   def new
     @group = Group.new
-
-    respond_to do |format|
-      format.html # new.html.erb
-      format.xml  { render :xml => @group }
-    end
   end
 
-  # GET /groups/1/edit
-  def edit
-    @group = Group.find(params[:id], :include => :projects)
-  end
-
-  # POST /groups
-  # POST /groups.xml
   def create
-    @group = Group.new(params[:group])
+    @group = Group.new
+    @group.safe_attributes = params[:group]
 
     respond_to do |format|
       if @group.save
@@ -71,102 +54,87 @@
           flash[:notice] = l(:notice_successful_create)
           redirect_to(params[:continue] ? new_group_path : groups_path)
         }
-        format.xml  { render :xml => @group, :status => :created, :location => @group }
+        format.api  { render :action => 'show', :status => :created, :location => group_url(@group) }
       else
         format.html { render :action => "new" }
-        format.xml  { render :xml => @group.errors, :status => :unprocessable_entity }
+        format.api  { render_validation_errors(@group) }
       end
     end
   end
 
-  # PUT /groups/1
-  # PUT /groups/1.xml
+  def edit
+  end
+
   def update
-    @group = Group.find(params[:id])
+    @group.safe_attributes = params[:group]
 
     respond_to do |format|
-      if @group.update_attributes(params[:group])
+      if @group.save
         flash[:notice] = l(:notice_successful_update)
         format.html { redirect_to(groups_path) }
-        format.xml  { head :ok }
+        format.api  { render_api_ok }
       else
         format.html { render :action => "edit" }
-        format.xml  { render :xml => @group.errors, :status => :unprocessable_entity }
+        format.api  { render_validation_errors(@group) }
       end
     end
   end
 
-  # DELETE /groups/1
-  # DELETE /groups/1.xml
   def destroy
-    @group = Group.find(params[:id])
     @group.destroy
 
     respond_to do |format|
       format.html { redirect_to(groups_url) }
-      format.xml  { head :ok }
+      format.api  { render_api_ok }
     end
   end
 
   def add_users
-    @group = Group.find(params[:id])
-    users = User.find_all_by_id(params[:user_ids])
-    @group.users << users if request.post?
+    @users = User.find_all_by_id(params[:user_id] || params[:user_ids])
+    @group.users << @users if request.post?
     respond_to do |format|
       format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' }
-      format.js {
-        render(:update) {|page|
-          page.replace_html "tab-content-users", :partial => 'groups/users'
-          users.each {|user| page.visual_effect(:highlight, "user-#{user.id}") }
-        }
-      }
+      format.js
+      format.api { render_api_ok }
     end
   end
 
   def remove_user
-    @group = Group.find(params[:id])
     @group.users.delete(User.find(params[:user_id])) if request.delete?
     respond_to do |format|
       format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' }
-      format.js { render(:update) {|page| page.replace_html "tab-content-users", :partial => 'groups/users'} }
+      format.js
+      format.api { render_api_ok }
     end
   end
 
   def autocomplete_for_user
-    @group = Group.find(params[:id])
     @users = User.active.not_in_group(@group).like(params[:q]).all(:limit => 100)
     render :layout => false
   end
 
   def edit_membership
-    @group = Group.find(params[:id])
     @membership = Member.edit_membership(params[:membership_id], params[:membership], @group)
     @membership.save if request.post?
     respond_to do |format|
-      if @membership.valid?
-        format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' }
-        format.js {
-          render(:update) {|page|
-            page.replace_html "tab-content-memberships", :partial => 'groups/memberships'
-            page.visual_effect(:highlight, "member-#{@membership.id}")
-          }
-        }
-      else
-        format.js {
-          render(:update) {|page|
-            page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
-          }
-        }
-      end
+      format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' }
+      format.js
     end
   end
 
   def destroy_membership
-    @group = Group.find(params[:id])
     Member.find(params[:membership_id]).destroy if request.post?
     respond_to do |format|
       format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' }
-      format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'groups/memberships'} }
+      format.js
     end
   end
+
+  private
+
+  def find_group
+    @group = Group.find(params[:id])
+  rescue ActiveRecord::RecordNotFound
+    render_404
+  end
 end