summaryrefslogtreecommitdiff
path: root/app/controllers/user_group_memberships_controller.rb
blob: 1cbbd48e32f08dfadb5db9c49f13cf140e9dba5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class UserGroupMembershipsController < ApplicationController
  load_and_authorize_resource :user_group
  load_and_authorize_resource :user_group_membership, :through => [:user_group]
  
  before_filter :spread_breadcrumbs
  
  def index
    @potential_users_count = @user_group.tenant.users.count - @user_group.users.count
  end

  def show
  end

  def new
    @user_group_membership = @user_group.user_group_memberships.build
    @potential_users = (@user_group.tenant.users.order(:last_name) - @user_group.users)
    if @potential_users.count == 0
      redirect_to user_group_user_group_memberships_path(@user_group), :alert => t('user_group_memberships.controller.no_more_user_to_add')
    end
  end

  def create
    @user_group_membership = @user_group.user_group_memberships.build(params[:user_group_membership])
    if @user_group_membership.save
      redirect_to user_group_user_group_membership_path(@user_group, @user_group_membership), :notice => t('user_group_memberships.controller.successfuly_created')
    else
      render :new
    end
  end

  def destroy
    @user_group_membership.destroy
    redirect_to user_group_user_group_memberships_path(@user_group), :notice => t('user_group_memberships.controller.successfuly_destroyed')
  end
  
  private

  def spread_breadcrumbs
    add_breadcrumb t("user_groups.index.page_title"), tenant_user_groups_path(@user_group.tenant)
    add_breadcrumb @user_group, tenant_user_group_path(@user_group.tenant, @user_group)
    add_breadcrumb t("user_group_memberships.index.page_title"), user_group_user_group_memberships_path(@user_group)

    if @user_group_membership && !@user_group_membership.new_record?
      add_breadcrumb @user_group_membership, user_group_user_group_membership_path(@user_group, @user_group_membership)
    end
  end
  
end