mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Moving our inputs to magical helpers
This commit is contained in:
@@ -2,10 +2,6 @@ Darkswarm.controller "DetailsCtrl", ($scope) ->
|
||||
angular.extend(this, new FieldsetMixin($scope))
|
||||
$scope.name = "details"
|
||||
|
||||
|
||||
#$scope.detailsValid = ->
|
||||
#$scope.detailsFields().every (field)->
|
||||
#$scope.checkout[field].$valid
|
||||
|
||||
#$scope.$watch ->
|
||||
#$scope.detailsValid()
|
||||
|
||||
@@ -17,44 +17,3 @@ Darkswarm.controller "CheckoutCtrl", ($scope, Order, storage) ->
|
||||
event.preventDefault()
|
||||
$scope.Order.submit()
|
||||
|
||||
FieldsetController = ($scope)->
|
||||
$scope.field = (path)->
|
||||
$scope[$scope.name][path]
|
||||
|
||||
$scope.fieldValid = (path)->
|
||||
not ($scope.dirty(path) and $scope.invalid(path))
|
||||
|
||||
$scope.dirty = (name)->
|
||||
$scope.field(name).$dirty
|
||||
|
||||
$scope.invalid = (name)->
|
||||
$scope.field(name).$invalid
|
||||
|
||||
$scope.error = (name)->
|
||||
$scope.field(name).$error
|
||||
|
||||
$scope.fieldErrors = (path)->
|
||||
# TODO: display server errors
|
||||
errors = for error, invalid of $scope.error(path)
|
||||
if invalid
|
||||
switch error
|
||||
when "required" then "must not be blank"
|
||||
when "number" then "must be number"
|
||||
when "email" then "must be email address"
|
||||
(errors.filter (error) -> error?).join ", "
|
||||
|
||||
|
||||
|
||||
Darkswarm.controller "DetailsSubCtrl", ($scope) ->
|
||||
angular.extend(this, new FieldsetController($scope))
|
||||
$scope.name = "details"
|
||||
#$scope.detailsValid = ->
|
||||
#$scope.detailsFields().every (field)->
|
||||
#$scope.checkout[field].$valid
|
||||
|
||||
#$scope.$watch ->
|
||||
#$scope.detailsValid()
|
||||
#, (valid)->
|
||||
#if valid
|
||||
#$scope.show("billing")
|
||||
|
||||
|
||||
@@ -10,4 +10,12 @@ module CheckoutHelper
|
||||
|
||||
adjustments
|
||||
end
|
||||
|
||||
def validated_input(name, path, args = {})
|
||||
defaults = {
|
||||
required: true,
|
||||
type: :text
|
||||
}.merge args
|
||||
render partial: "shared/validated_input", locals: {name: name, path: path}.merge(defaults)
|
||||
end
|
||||
end
|
||||
|
||||
8
app/views/shared/_validated_input.html.haml
Normal file
8
app/views/shared/_validated_input.html.haml
Normal file
@@ -0,0 +1,8 @@
|
||||
%label{for: path}= name
|
||||
%input.medium.input-text{name: path,
|
||||
"ng-model" => path,
|
||||
required: required,
|
||||
type: type,
|
||||
"ng-class" => "{error: '!fieldValid(\"#{path}\")'}"}
|
||||
%small.error.medium.input-text{"ng-show" => "!fieldValid('#{path}')"}
|
||||
= "{{ fieldErrors('#{path}') }}"
|
||||
@@ -11,29 +11,10 @@
|
||||
{{ order.bill_address.lastname }}
|
||||
.row
|
||||
.large-6.columns
|
||||
|
||||
- path = "order.email"
|
||||
%label{for: path} Email
|
||||
%input.medium.input-text{name: path,
|
||||
"ng-model" => path,
|
||||
required: true,
|
||||
type: :email,
|
||||
"ng-class" => "{error: '!fieldValid(\"#{path}\")'}"}
|
||||
%small.error.medium.input-text{"ng-show" => "!fieldValid('#{path}')"}
|
||||
= "{{ fieldErrors('#{path}') }}"
|
||||
|
||||
= validated_input('Email', 'order.email', type: :email)
|
||||
= f.fields_for :bill_address, @order.bill_address do |ba|
|
||||
.large-6.columns
|
||||
|
||||
- path = "order.bill_address.phone"
|
||||
%label{for: path} Phone
|
||||
%input.medium.input-text{name: path,
|
||||
"ng-model" => path,
|
||||
required: true,
|
||||
type: :text,
|
||||
"ng-class" => "{error: '!fieldValid(\"#{path}\")'}"}
|
||||
%small.error.medium.input-text{"ng-show" => "!fieldValid('#{path}')"}
|
||||
= "{{ fieldErrors('#{path}') }}"
|
||||
= validated_input 'Phone', 'order.bill_address.phone'
|
||||
|
||||
= f.fields_for :bill_address, @order.bill_address do |ba|
|
||||
.row
|
||||
|
||||
13
spec/helpers/checkout_helper_spec.rb
Normal file
13
spec/helpers/checkout_helper_spec.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'spec_helper'
|
||||
|
||||
|
||||
describe CheckoutHelper do
|
||||
it "generates html for validated inputs" do
|
||||
helper.should_receive(:render).with(
|
||||
partial: "shared/validated_input",
|
||||
locals: {name: "test", path: "foo", required: true, type: :email}
|
||||
)
|
||||
|
||||
helper.validated_input("test", "foo", type: :email)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user