summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-06-04 15:06:50 +0200
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-06-04 15:06:50 +0200
commit87cf6e55f76901c340ec99cf216637a2b9672cc2 (patch)
tree8f500293a84151d177a64e3ca892eb13cd20b8f4
parent26456e2298376ceb5bc3d7978d7c0aa146e44039 (diff)
JSON API for PagerGroup (new)
-rw-r--r--app/controllers/api/v1/pager_groups_controller.rb47
-rw-r--r--app/controllers/pager_groups_controller.rb4
-rw-r--r--app/models/pager_group.rb9
-rw-r--r--app/serializers/pager_group_destination_serializer.rb3
-rw-r--r--app/serializers/pager_group_serializer.rb7
-rw-r--r--config/routes.rb3
6 files changed, 71 insertions, 2 deletions
diff --git a/app/controllers/api/v1/pager_groups_controller.rb b/app/controllers/api/v1/pager_groups_controller.rb
new file mode 100644
index 0000000..6db9b90
--- /dev/null
+++ b/app/controllers/api/v1/pager_groups_controller.rb
@@ -0,0 +1,47 @@
+module Api
+ module V1
+ class PagerGroupsController < ApplicationController
+ respond_to :json
+
+ def index
+ # if params[:ids]
+ # @sip_accounts = SipAccount.where(:id => params[:ids])
+ # else
+ # @sip_accounts = SipAccount.all
+ # end
+ @pager_groups = PagerGroup.all
+
+ respond_with @pager_groups
+ end
+
+ def show
+ @pager_group = PagerGroup.find(params[:id])
+
+ respond_with @pager_group
+ end
+
+
+ def new
+ if params[:sip_account_id] && SipAccount.find(params[:sip_account_id])
+ @pager_group = SipAccount.find(params[:sip_account_id]).pager_groups.new
+ @pager_group.callback_url = params[:callback_url]
+ @pager_group.pager_group_destination_ids = params[:pager_group_destination_ids]
+ if @pager_group.save
+ respond_with @pager_group
+ end
+ end
+
+ end
+
+ def create
+ @pager_group = PagerGroup.new(params[:pager_group])
+ if @pager_group.save
+ redirect_to @pager_group, :notice => t('pager_groups.controller.successfuly_created')
+ else
+ render :new
+ end
+ end
+
+ end
+ end
+end
diff --git a/app/controllers/pager_groups_controller.rb b/app/controllers/pager_groups_controller.rb
index 4fa21c2..3b145b1 100644
--- a/app/controllers/pager_groups_controller.rb
+++ b/app/controllers/pager_groups_controller.rb
@@ -1,6 +1,8 @@
class PagerGroupsController < ApplicationController
+ load_resource :sip_account
+ load_resource :pager_group, :through => :sip_account
+
def index
- @pager_groups = PagerGroup.all
end
def show
diff --git a/app/models/pager_group.rb b/app/models/pager_group.rb
index 5c9c6ad..5c91ed8 100644
--- a/app/models/pager_group.rb
+++ b/app/models/pager_group.rb
@@ -1,5 +1,6 @@
class PagerGroup < ActiveRecord::Base
attr_accessible :sip_account_id, :callback_url
+ attr_writer :pager_group_destination_ids
has_many :pager_group_destinations, :dependent => :destroy
belongs_to :sip_account
@@ -9,6 +10,14 @@ class PagerGroup < ActiveRecord::Base
after_create :call
before_destroy :hangup_all
+ before_save :save_pager_group_destination_ids
+
+ def save_pager_group_destination_ids
+ if @pager_group_destination_ids
+ self.pager_group_destination_ids = @pager_group_destination_ids.split(/,/).map { |sip_account_id| self.pager_group_destinations.build(:sip_account_id => sip_account_id) }
+ end
+ end
+
def identifier
"pager#{self.id}"
end
diff --git a/app/serializers/pager_group_destination_serializer.rb b/app/serializers/pager_group_destination_serializer.rb
new file mode 100644
index 0000000..6b14884
--- /dev/null
+++ b/app/serializers/pager_group_destination_serializer.rb
@@ -0,0 +1,3 @@
+class PagerGroupDestinationSerializer < ActiveModel::Serializer
+ embed :ids, :include => true
+end
diff --git a/app/serializers/pager_group_serializer.rb b/app/serializers/pager_group_serializer.rb
new file mode 100644
index 0000000..ca748d9
--- /dev/null
+++ b/app/serializers/pager_group_serializer.rb
@@ -0,0 +1,7 @@
+class PagerGroupSerializer < ActiveModel::Serializer
+ embed :ids, :include => true
+
+ attributes :id, :sip_account_id, :callback_url
+
+ has_many :pager_group_destinations
+end
diff --git a/config/routes.rb b/config/routes.rb
index 6145765..46562eb 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,7 +1,7 @@
Gemeinschaft42c::Application.routes.draw do
+ # To-Do: Delete these two entries and remap path on the views.
resources :pager_group_destinations
-
resources :pager_groups
namespace :api, defaults: {format: 'json'} do
@@ -9,6 +9,7 @@ Gemeinschaft42c::Application.routes.draw do
resources :switchboards, :only => [:show, :index]
resources :switchboard_entries, :only => [:show, :index]
resources :sip_accounts, :only => [:show, :index]
+ resources :pager_groups
resources :phone_numbers, :only => [:show, :index]
end