summaryrefslogtreecommitdiff
path: root/app/controllers/calls_controller.rb
blob: 5534b1bcfecb24582070da17569eac6a27726c66 (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
class CallsController < ApplicationController
  load_resource :sip_account
  load_resource :call

  before_filter :set_and_authorize_parent
  
  def index
    if @parent
      @calls = @parent.calls
    else
      @calls = Call.all
    end
  end

  def new
    if !params[:url].blank?
      protocol, separator, phone_number = params[:url].partition(':')
      if ! phone_number.blank? 
        @call = @parent.calls.new()
        @call.dest = phone_number
      end
    elsif !params[:number].blank?
      @call = @parent.calls.new()
      @call.dest = params[:number]
    end
  end

  def show
    redirect_to :index
  end

  def create
    params[:call][:sip_account] = @sip_account
    @call = Call.create(params[:call])

    if @call && @call.call
      m = method( :"#{@parent.class.name.underscore}_calls_url" )
      redirect_to m.( @parent ), :notice => t('calls.controller.successfuly_created')
    else
      render :new
    end
  end

  def destroy
    @call.destroy
    if @parent
      m = method( :"#{@parent.class.name.underscore}_calls_url" )
    else
      m = method( :"calls_url" )
    end
    redirect_to m.(@parent), :notice => t('calls.controller.successfuly_destroyed')
  end

  private
  def set_and_authorize_parent
    @parent = @sip_account
  end
end