summaryrefslogtreecommitdiff
path: root/app/controllers/parking_stalls_controller.rb
blob: d1bc0c77e397052c7b394718e2d7acf759e40bf0 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class ParkingStallsController < ApplicationController

  load_resource :tenant
  load_resource :user
  load_and_authorize_resource :parking_stall, :through => [:user, :tenant ]
  
  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs

  def index
  end

  def show
  end

  def new
    @parking_stall.lot = 'default'
    @parking_stall.name = ParkingStall.order(:name).last.try(:name).to_i + 1
  end

  def create
    @parking_stall = @parent.parking_stalls.build(params[:parking_stall])
    if @parking_stall.save
      redirect_to [@parent, @parking_stall], :notice => t('parking_stalls.controller.successfuly_created')
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @parking_stall.update_attributes(params[:parking_stall])
      redirect_to [@parent, @parking_stall], :notice  => t('parking_stalls.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @parking_stall.destroy
    m = method( :"#{@parent.class.name.underscore}_parking_stalls_url" )
    redirect_to m.(@parent), :notice => t('parking_stalls.controller.successfuly_destroyed')
  end

  private
  def set_and_authorize_parent
    @parent = @user || @tenant
    authorize! :read, @parent
  end

  def spread_breadcrumbs
    if @user
      add_breadcrumb t("users.index.page_title"), tenant_users_path(@user.current_tenant)
      add_breadcrumb @user, tenant_user_path(@user.current_tenant, @user)
      add_breadcrumb t("parking_stalls.index.page_title"), user_parking_stalls_path(@user)
      if @parking_stall && !@parking_stall.new_record?
        add_breadcrumb @parking_stall, user_parking_stall_path(@user, @parking_stall)
      end
    end
    if @tenant
      add_breadcrumb t("parking_stalls.index.page_title"), tenant_parking_stalls_path(@tenant)
      if @parking_stall && !@parking_stall.new_record?
        add_breadcrumb @parking_stall, tenant_parking_stall_path(@tenant, @parking_stall)
      end
    end
  end
end