From ef8f611458aab5e2f21b07ae50ecbb97e4cdb0f7 Mon Sep 17 00:00:00 2001 From: Rob H Date: Thu, 21 Aug 2014 14:06:41 +1000 Subject: [PATCH] Adding countries and states to registration --- .../registration_controller.js.coffee | 38 +++---------------- .../enterprise_creation_service.js.coffee | 31 +++++++++++++++ .../templates/registration/about.html.haml | 2 +- .../templates/registration/address.html.haml | 9 +---- .../templates/registration/contact.html.haml | 2 +- app/helpers/injection_helper.rb | 4 ++ app/serializers/api/country_serializer.rb | 5 +++ app/serializers/api/state_serializer.rb | 3 ++ app/views/registration/index.html.haml | 1 + spec/features/consumer/registration_spec.rb | 6 +-- 10 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee create mode 100644 app/serializers/api/country_serializer.rb create mode 100644 app/serializers/api/state_serializer.rb diff --git a/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee index 7fa8ad0350..dc1bb2a80e 100644 --- a/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/registration_controller.js.coffee @@ -1,39 +1,13 @@ -Darkswarm.controller "RegistrationCtrl", ($scope, $http, RegistrationService, CurrentUser, SpreeApiKey) -> - $scope.current_user = CurrentUser - +Darkswarm.controller "RegistrationCtrl", ($scope, RegistrationService, EnterpriseCreationService, availableCountries) -> $scope.currentStep = RegistrationService.currentStep $scope.select = RegistrationService.select + $scope.enterprise = EnterpriseCreationService.enterprise + $scope.create = EnterpriseCreationService.create $scope.steps = ['details','address','contact','about'] # ,'images','social' - $scope.enterprise = - user_ids: [CurrentUser.id] - email: CurrentUser.email - address: { - country_id: 12 - state_id: 1061493592 - } + $scope.countries = availableCountries - $scope.createEnterprise = -> - $http( - method: "POST" - url: "/api/enterprises" - data: - enterprise: $scope.prepare($scope.enterprise) - params: - token: SpreeApiKey - ).success((data) -> - $scope.select('about') - ).error((data) -> - console.log angular.toJson(data) - alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') - ) - # $scope.select('about') - - $scope.prepare = (ent_obj) -> - enterprise = {} - for a, v of ent_obj when a isnt 'address' - enterprise[a] = v - enterprise.address_attributes = ent_obj.address - enterprise + $scope.countryHasStates = -> + $scope.enterprise.country.states.length > 0 diff --git a/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee b/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee new file mode 100644 index 0000000000..f23f11700a --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/enterprise_creation_service.js.coffee @@ -0,0 +1,31 @@ +Darkswarm.factory "EnterpriseCreationService", ($http, RegistrationService, CurrentUser, SpreeApiKey, availableCountries) -> + new class EnterpriseCreationService + enterprise: + user_ids: [CurrentUser.id] + email: CurrentUser.email + address: {} + country: availableCountries[0] + + create: => + $http( + method: "POST" + url: "/api/enterprises" + data: + enterprise: @prepare() + params: + token: SpreeApiKey + ).success((data) -> + RegistrationService.select('about') + ).error((data) -> + console.log angular.toJson(data) + alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') + ) + # RegistrationService.select('about') + + prepare: => + enterprise = {} + for a, v of @enterprise when a isnt 'address' && a isnt 'country' + enterprise[a] = v + enterprise.address_attributes = @enterprise.address + enterprise.address_attributes.country_id = @enterprise.country.id + enterprise \ No newline at end of file diff --git a/app/assets/javascripts/templates/registration/about.html.haml b/app/assets/javascripts/templates/registration/about.html.haml index 5dac45cb13..ad44ebbf7f 100644 --- a/app/assets/javascripts/templates/registration/about.html.haml +++ b/app/assets/javascripts/templates/registration/about.html.haml @@ -1 +1 @@ -YAY! You created an enterprise! \ No newline at end of file +Yay! You created an enterprise! \ No newline at end of file diff --git a/app/assets/javascripts/templates/registration/address.html.haml b/app/assets/javascripts/templates/registration/address.html.haml index 4b4db863fe..ffea2db77d 100644 --- a/app/assets/javascripts/templates/registration/address.html.haml +++ b/app/assets/javascripts/templates/registration/address.html.haml @@ -19,15 +19,10 @@ .row .small-12.large-8.columns %label{ for: 'enterprise_country' } Country: - -#= select :country_id, available_countries.map{|c|[c.name, c.id]}, {include_blank: false} - %input.chunky.small-12.columns{ id: 'enterprise_country', placeholder: "eg. Australia" } - -# , ng: { model: 'enterprise.country' } } + %select.chunky.small-12.columns{ id: 'enterprise_country', ng: { model: 'enterprise.country', options: 'c as c.name for c in countries' } } .small-12.large-4.columns %label{ for: 'enterprise_state' } State: - - binding.pry - -#= select :state_id, available_countries.first.states.map{|c|[c.name, c.id]} - %input.chunky.small-12.columns{ id: 'enterprise_state', placeholder: "eg. Victoria" } - -# , ng: { model: 'enterprise.state' } } + %select.chunky.small-12.columns{ id: 'enterprise_state', ng: { model: 'enterprise.address.state_id', options: 's.id as s.abbr for s in enterprise.country.states', show: 'countryHasStates()' } } .small-5.columns %h6 Location display diff --git a/app/assets/javascripts/templates/registration/contact.html.haml b/app/assets/javascripts/templates/registration/contact.html.haml index 40c7948767..4ac93894c0 100644 --- a/app/assets/javascripts/templates/registration/contact.html.haml +++ b/app/assets/javascripts/templates/registration/contact.html.haml @@ -34,4 +34,4 @@ .row .small-12.columns %input.button.primary{ type: "button", value: "Back", ng: { click: "select('address')" }, style: 'float:left' } - %input.button.primary{ type: "button", value: "Continue", ng: { click: "createEnterprise()" }, style: 'float:right' } \ No newline at end of file + %input.button.primary{ type: "button", value: "Continue", ng: { click: "create()" }, style: 'float:right' } \ No newline at end of file diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 8ef6b9b43f..a43fab3b4c 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -25,6 +25,10 @@ module InjectionHelper render partial: "json/injection_ams", locals: {name: 'SpreeApiKey', json: "'#{@spree_api_key.to_s}'"} end + def inject_available_countries + inject_json_ams "availableCountries", available_countries, Api::CountrySerializer + end + def inject_json(name, partial, opts = {}) render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts) end diff --git a/app/serializers/api/country_serializer.rb b/app/serializers/api/country_serializer.rb new file mode 100644 index 0000000000..6561692cf6 --- /dev/null +++ b/app/serializers/api/country_serializer.rb @@ -0,0 +1,5 @@ +class Api::CountrySerializer < ActiveModel::Serializer + attributes :id, :name, :states + + has_many :states, serializer: Api::StateSerializer +end \ No newline at end of file diff --git a/app/serializers/api/state_serializer.rb b/app/serializers/api/state_serializer.rb new file mode 100644 index 0000000000..bcf9221ec5 --- /dev/null +++ b/app/serializers/api/state_serializer.rb @@ -0,0 +1,3 @@ +class Api::StateSerializer < ActiveModel::Serializer + attributes :id, :name, :abbr +end \ No newline at end of file diff --git a/app/views/registration/index.html.haml b/app/views/registration/index.html.haml index 5d883c43ae..205b3582bb 100644 --- a/app/views/registration/index.html.haml +++ b/app/views/registration/index.html.haml @@ -1,2 +1,3 @@ =inject_spree_api_key +=inject_available_countries %div{ "ng-controller" => "RegistrationCtrl" } \ No newline at end of file diff --git a/spec/features/consumer/registration_spec.rb b/spec/features/consumer/registration_spec.rb index 592ee789e2..5041aae1a5 100644 --- a/spec/features/consumer/registration_spec.rb +++ b/spec/features/consumer/registration_spec.rb @@ -32,8 +32,8 @@ feature "Registration", js: true do fill_in 'enterprise_address', with: '123 Abc Street' fill_in 'enterprise_city', with: 'Northcote' fill_in 'enterprise_zipcode', with: '3070' - fill_in 'enterprise_country', with: 'Australia' - fill_in 'enterprise_state', with: 'Victoria' + select 'Australia', from: 'enterprise_country' + select 'Vic', from: 'enterprise_state' click_button 'Continue' # Filling in Contact Details @@ -44,8 +44,6 @@ feature "Registration", js: true do click_button 'Continue' # Enterprise should be created - sleep 10 - save_screenshot '/Users/rob/Desktop/ss.png' expect(page).to have_content 'Yay! You created an enterprise!' end end