From 9441d14f02a508d80b37422210b4f8e894c9ffb2 Mon Sep 17 00:00:00 2001
From: Justin Aiken <60tonangel@gmail.com>
Date: Thu, 24 Oct 2013 08:08:37 -0600
Subject: [PATCH] Allow per-mailbox override options for #record calls

---
 CHANGELOG.md                                  |  1 +
 .../application_controller.rb                 |  8 +++++
 .../mailbox_set_greeting_controller.rb        |  2 +-
 .../call_controllers/voicemail_controller.rb  |  2 +-
 lib/voicemail/plugin.rb                       |  7 ++---
 .../mailbox_set_greeting_controller_spec.rb   | 30 +++++++++++++++----
 6 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ee8ed1..ab17785 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,5 @@
 #v1.0.0..
+  * FEATURE - Optional per-mailbox override of default recording hash
   * FEATURE - Optional rerecording of voicemail messages
   * FEATURE - I18n numeric methods
   * FEATURE - I18n support for keys
diff --git a/lib/voicemail/call_controllers/application_controller.rb b/lib/voicemail/call_controllers/application_controller.rb
index f5427ab..2835195 100644
--- a/lib/voicemail/call_controllers/application_controller.rb
+++ b/lib/voicemail/call_controllers/application_controller.rb
@@ -24,6 +24,14 @@ def fetch_mailbox
       storage.get_mailbox mailbox_id
     end
 
+    def record_options
+      if config.use_mailbox_opts_for_recording
+        config.recording.to_hash.merge mailbox[:record_options]
+      else
+        config.recording.to_hash
+      end
+    end
+
     def mailbox_not_found
       play config.mailbox_not_found
       hangup
diff --git a/lib/voicemail/call_controllers/mailbox_set_greeting_controller.rb b/lib/voicemail/call_controllers/mailbox_set_greeting_controller.rb
index 9c88043..998fcf9 100644
--- a/lib/voicemail/call_controllers/mailbox_set_greeting_controller.rb
+++ b/lib/voicemail/call_controllers/mailbox_set_greeting_controller.rb
@@ -35,7 +35,7 @@ def listen_to_current_greeting
 
     def record_greeting
       play config.set_greeting.before_record
-      record_comp = record config.set_greeting.recording.to_hash
+      record_comp = record record_options
       @temp_recording = record_comp.complete_event.recording.uri
       play_audio @temp_recording
 
diff --git a/lib/voicemail/call_controllers/voicemail_controller.rb b/lib/voicemail/call_controllers/voicemail_controller.rb
index da233ef..0101d7a 100644
--- a/lib/voicemail/call_controllers/voicemail_controller.rb
+++ b/lib/voicemail/call_controllers/voicemail_controller.rb
@@ -20,7 +20,7 @@ def play_greeting
     end
 
     def record_message
-      @recording = record config.recording.to_hash
+      @recording = record record_options
 
       config.allow_rerecording ? recording_menu : save_recording
     end
diff --git a/lib/voicemail/plugin.rb b/lib/voicemail/plugin.rb
index fc501b9..2c7aed4 100644
--- a/lib/voicemail/plugin.rb
+++ b/lib/voicemail/plugin.rb
@@ -29,7 +29,7 @@ class Plugin < Adhearsion::Plugin
       allow_rerecording true, desc: "Allow caller to rerecord their voicemail"
       after_record "Press 1 to save your voicemail.  Press 2 to rerecord.", desc: "Message to play if allow_rerecording is set"
 
-      desc "Voicemail recording options"
+      desc "Default recording options"
       recording {
         interruptible true, desc: "Whether you can stop the recording with a DTMF input"
         direction :send, desc: "The direction to record; you probably want :send"
@@ -38,6 +38,7 @@ class Plugin < Adhearsion::Plugin
         start_beep true, desc: "Play a beep before recording"
         stop_beep false, desc: "Play a beep after recording"
       }
+      use_mailbox_opts_for_recording false, desc: "Whether per-mailbox settings can override defaults"
 
       desc "Configuration for registered users"
       mailbox {
@@ -61,10 +62,6 @@ class Plugin < Adhearsion::Plugin
         before_record "Please speak after the beep. The prompt will be played back after.", desc: "Recording instructions"
         after_record "Press 1 to save your new greeting, 2 to discard it, 9 to go back to the menu", desc: "Menu to use after recording"
         no_personal_greeting "You do not currently have a personalized greeting.", desc: "What to play if there is no specific greeting"
-        recording {
-          max_duration 5_000, desc: "Maximum duration for recording in milliseconds"
-          start_beep true, desc: "Play a beep before recording"
-        }
       }
 
       desc "Set PIN configuration"
diff --git a/spec/voicemail/call_controllers/mailbox_set_greeting_controller_spec.rb b/spec/voicemail/call_controllers/mailbox_set_greeting_controller_spec.rb
index 0646177..daf2180 100644
--- a/spec/voicemail/call_controllers/mailbox_set_greeting_controller_spec.rb
+++ b/spec/voicemail/call_controllers/mailbox_set_greeting_controller_spec.rb
@@ -36,15 +36,33 @@
     let(:recording_component) { flexmock 'Record' }
     let(:file_path) { "/path/to/file" }
 
-    it "plays the appropriate sounds, records, plays back recording, and calls the recording menu" do
+    before do
       should_play config.set_greeting.before_record
-      recording_component.should_receive("complete_event.recording.uri").and_return(file_path)
-      subject.should_receive(:record).once.with(config.set_greeting.recording.to_hash).and_return recording_component
+      recording_component.should_receive("complete_event.recording.uri").and_return file_path
       subject.should_receive(:play_audio).with file_path
-      subject.should_receive(:menu).once.with(config.set_greeting.after_record,
-          { timeout: config.menu_timeout,
-            tries: config.menu_tries }, Proc)
+      subject.should_receive(:menu).once.with config.set_greeting.after_record, {timeout: config.menu_timeout, tries: config.menu_tries}, Proc
+    end
+
+    after do
       controller.record_greeting
+      config.use_mailbox_opts_for_recording = false
+    end
+
+    context "without mailbox settings" do
+      it "plays the appropriate sounds, records, plays back recording, and calls the recording menu" do
+        subject.should_receive(:record).once.with(config.recording.to_hash).and_return recording_component
+      end
+    end
+
+    context "with mailbox settings" do
+      let(:mailbox) { {id: 100, record_options: {final_timeout: 31}} }
+
+      before { config.use_mailbox_opts_for_recording = true }
+
+      it "records using the mailbox's record options" do
+        expected_options = {direction: :send, final_timeout: 31, interruptible: true, max_duration: 30, start_beep: true, stop_beep: false}
+        subject.should_receive(:record).once.with(expected_options).and_return recording_component
+      end
     end
   end