From f1f9a2e7fc506dbf41b6951da0b792136cbdfa1d Mon Sep 17 00:00:00 2001 From: Rob H Date: Thu, 21 Aug 2014 17:04:40 +1000 Subject: [PATCH] Updating on later pages of registration process --- .../registration_controller.js.coffee | 7 ++- .../enterprise_creation_service.js.coffee | 60 ++++++++++++------- .../templates/registration/about.html.haml | 2 +- .../templates/registration/social.html.haml | 2 +- app/controllers/api/enterprises_controller.rb | 13 +++- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee index 3e9dddb2d4..2cf7c50c8d 100644 --- a/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee @@ -1,8 +1,9 @@ -Darkswarm.controller "RegistrationCtrl", ($scope, RegistrationService, EnterpriseCreationService, availableCountries) -> +Darkswarm.controller "RegistrationCtrl", ($scope, RegistrationService, EnterpriseRegistrationService, availableCountries) -> $scope.currentStep = RegistrationService.currentStep $scope.select = RegistrationService.select - $scope.enterprise = EnterpriseCreationService.enterprise - $scope.create = EnterpriseCreationService.create + $scope.enterprise = EnterpriseRegistrationService.enterprise + $scope.create = EnterpriseRegistrationService.create + $scope.update = EnterpriseRegistrationService.update $scope.steps = ['details','address','contact','about','images','social'] diff --git a/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee b/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee index e5400f1b46..52baea146a 100644 --- a/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee @@ -1,5 +1,5 @@ -Darkswarm.factory "EnterpriseCreationService", ($http, RegistrationService, CurrentUser, SpreeApiKey, availableCountries) -> - new class EnterpriseCreationService +Darkswarm.factory "EnterpriseRegistrationService", ($http, RegistrationService, CurrentUser, SpreeApiKey, Loading, availableCountries) -> + new class EnterpriseRegistrationService enterprise: user_ids: [CurrentUser.id] email: CurrentUser.email @@ -7,27 +7,47 @@ Darkswarm.factory "EnterpriseCreationService", ($http, RegistrationService, Curr country: availableCountries[0] create: => - # Loading.message = "Creating " + $scope.enterprise.name - # $http( - # method: "POST" - # url: "/api/enterprises" - # data: - # enterprise: @prepare() - # params: - # token: SpreeApiKey - # ).success((data) -> - # Loading.clear() - # RegistrationService.select('about') - # ).error((data) -> - # Loading.clear() - # console.log angular.toJson(data) - # alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') - # ) - RegistrationService.select('about') + Loading.message = "Creating " + @enterprise.name + $http( + method: "POST" + url: "/api/enterprises" + data: + enterprise: @prepare() + params: + token: SpreeApiKey + ).success((data) => + Loading.clear() + @enterprise.id = data + RegistrationService.select('about') + ).error((data) => + Loading.clear() + console.log angular.toJson(data) + alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') + ) + #RegistrationService.select('about') + + update: (step) => + Loading.message = "Updating " + @enterprise.name + $http( + method: "PUT" + url: "/api/enterprises/#{@enterprise.id}" + data: + enterprise: @prepare() + params: + token: SpreeApiKey + ).success((data) -> + Loading.clear() + RegistrationService.select(step) + ).error((data) -> + Loading.clear() + console.log angular.toJson(data) + alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') + ) + #RegistrationService.select(step) prepare: => enterprise = {} - for a, v of @enterprise when a isnt 'address' && a isnt 'country' + for a, v of @enterprise when a isnt 'address' && a isnt 'country' && a isnt 'id' enterprise[a] = v enterprise.address_attributes = @enterprise.address enterprise.address_attributes.country_id = @enterprise.country.id diff --git a/app/assets/javascripts/templates/registration/about.html.haml b/app/assets/javascripts/templates/registration/about.html.haml index 29ec5282b1..e2dad4a66b 100644 --- a/app/assets/javascripts/templates/registration/about.html.haml +++ b/app/assets/javascripts/templates/registration/about.html.haml @@ -32,4 +32,4 @@ %input.chunky.small-12.columns{ id: 'enterprise_acn', placeholder: "eg. 123 456 789", ng: { model: 'enterprise.acn' } } .row.buttons .small-12.columns - %input.button.primary.right{ type: "button", value: "Continue", ng: { click: "select('social')" } } \ No newline at end of file + %input.button.primary.right{ type: "button", value: "Continue", ng: { click: "update('social')" } } \ No newline at end of file diff --git a/app/assets/javascripts/templates/registration/social.html.haml b/app/assets/javascripts/templates/registration/social.html.haml index c66e4422bd..40717b6190 100644 --- a/app/assets/javascripts/templates/registration/social.html.haml +++ b/app/assets/javascripts/templates/registration/social.html.haml @@ -30,4 +30,4 @@ .row.buttons .small-12.columns %input.button.primary.left{ type: "button", value: "Back", ng: { click: "select('about')" } } - %input.button.primary.right{ type: "button", value: "Continue", ng: { click: "select('finished')" } } \ No newline at end of file + %input.button.primary.right{ type: "button", value: "Continue", ng: { click: "update('finished')" } } \ No newline at end of file diff --git a/app/controllers/api/enterprises_controller.rb b/app/controllers/api/enterprises_controller.rb index d80e10849a..f2263a600b 100644 --- a/app/controllers/api/enterprises_controller.rb +++ b/app/controllers/api/enterprises_controller.rb @@ -17,7 +17,18 @@ module Api @enterprise = Enterprise.new(params[:enterprise]) if @enterprise.save - render text: '', :status => 201 + render text: @enterprise.id, :status => 201 + else + invalid_resource!(@enterprise) + end + end + + def update + authorize! :update, Enterprise + + @enterprise = Enterprise.find(params[:id]) + if @enterprise.update_attributes(params[:enterprise]) + render text: @enterprise.id, :status => 200 else invalid_resource!(@enterprise) end