#--
# Copyright (c) 2007 Robert S. Thau, Smartleaf, Inc.
# 
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
class RoleAssignmentsController < GenericRestController

  resources_controller_for :role_assignments
  layout "admin"

  # Resources Controller doesn't really do the right thing
  # for us on AJAX, so...

  def index
    self.resources = find_resources

    respond_to do |format|
      format.html # index.rhtml
      format.js   { render :action => 'index', :layout => false }
      format.xml  { render :xml => resources.to_xml }
    end
    self.resources = find_resources
  end

  def create
    self.resource = new_resource
    resource.save!
    respond_to do |format|
      format.html do
        flash[:notice] = "#{resource_name.humanize} was successfully created."
        redirect_to resource_url
      end
      format.js   { index }
      format.xml  { head :created, :location => resource_url }
    end
  end

  def destroy
    self.resource = find_resource
    resource.destroy
    respond_to do |format|
      format.html do
        flash[:notice] = "#{resource_name.humanize} was successfully destroyed."
        redirect_to resources_url
      end
      format.js   { index }
      format.xml  { head :ok }
    end
  end

  protected

  helper :users                 # drag in user controller helpers
  helper_method :resource_class, :new_resource

  def js_render_args( action )

    # Always show the full assignment list when redisplaying within the 
    # users page...

    return { :action => "index", :layout => false }

  end

  def new_resource( attrs = params[:role_assignment] )
    ra_params = (attrs || {}).merge( :user => enclosing_resource )
    RoleAssignment.new ra_params
  end

  def can_edit?( x ); false; end

end

