Adding countries and states to registration

This commit is contained in:
Rob H
2014-08-21 14:06:41 +10:00
parent 7dc42c9e39
commit ef8f611458
10 changed files with 56 additions and 45 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1 +1 @@
YAY! You created an enterprise!
Yay! You created an enterprise!

View File

@@ -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

View File

@@ -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' }
%input.button.primary{ type: "button", value: "Continue", ng: { click: "create()" }, style: 'float:right' }

View File

@@ -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

View File

@@ -0,0 +1,5 @@
class Api::CountrySerializer < ActiveModel::Serializer
attributes :id, :name, :states
has_many :states, serializer: Api::StateSerializer
end

View File

@@ -0,0 +1,3 @@
class Api::StateSerializer < ActiveModel::Serializer
attributes :id, :name, :abbr
end

View File

@@ -1,2 +1,3 @@
=inject_spree_api_key
=inject_available_countries
%div{ "ng-controller" => "RegistrationCtrl" }

View File

@@ -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