From 8023d6481cbadd52e58b9a4342ac7636ce1807e3 Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Mon, 6 Jul 2015 11:26:02 -0500 Subject: [PATCH] VRFS-3276 : Hook calendar creation into user controller API. Add test to verify. --- ruby/lib/jam_ruby/models/user.rb | 14 ++++++++++ web/app/controllers/api_users_controller.rb | 2 +- .../controllers/api_users_controller_spec.rb | 27 ++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 2d9612081..0500a896b 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -701,6 +701,20 @@ module JamRuby end end + # Build calendars using given parameter. + # @param calendars (array of hash) + def update_calendars(calendars) + unless self.new_record? + Calendar.where("user_id = ?", self.id).delete_all + end + + unless calendars.nil? + calendars.each do |cal| + self.calendars << self.calendars.create(cal) + end + end + end + # given an array of instruments, update a user's instruments def update_instruments(instruments) # delete all instruments for this user first diff --git a/web/app/controllers/api_users_controller.rb b/web/app/controllers/api_users_controller.rb index f0a878ffa..5c8f27377 100644 --- a/web/app/controllers/api_users_controller.rb +++ b/web/app/controllers/api_users_controller.rb @@ -149,7 +149,7 @@ class ApiUsersController < ApiController @user.update_online_presences(params[:online_presences]) if params.has_key?(:online_presences) @user.update_performance_samples(params[:performance_samples]) if params.has_key?(:performance_samples) - + @user.update_calendars(params[:calendars]) if params.has_key?(:calendars) @user.save if @user.errors.any? diff --git a/web/spec/controllers/api_users_controller_spec.rb b/web/spec/controllers/api_users_controller_spec.rb index a95ffebf7..065602e77 100644 --- a/web/spec/controllers/api_users_controller_spec.rb +++ b/web/spec/controllers/api_users_controller_spec.rb @@ -59,6 +59,27 @@ describe ApiUsersController do end end + describe "calendars" do + before :each do + Calendar.destroy_all + end + + it "adds calendar via update" do + cals = [{ + :name=>"Test Cal", + :description=>"This is a test", + :start_at=>(Time.now), + :end_at=>Time.now, + :trigger_delete=>true, + :target_uid=>"2112" + }] + post :update, id:user.id, calendars: cals, :format=>'json' + response.should be_success + user.reload + user.calendars.should have(1).items + end + end + describe "update mod" do it "empty mod" do post :update, id:user.id, mods: {}, :format=>'json' @@ -83,13 +104,13 @@ describe ApiUsersController do end end - describe 'site validation' do + describe 'site validation' do - it 'checks valid and invalid site types' do + it 'checks valid and invalid site types' do site_types = Utils::SITE_TYPES.clone << 'bandcamp-fan' site_types.each do |sitetype| rec_id = nil - case sitetype + case sitetype when 'url' valid, invalid = 'http://jamkazam.com', 'http://jamkazamxxx.com' when 'youtube'