Moving our inputs to magical helpers

This commit is contained in:
Will Marshall
2014-04-10 14:48:22 +10:00
parent 8b40092310
commit 00f5d09d65
6 changed files with 31 additions and 66 deletions

View File

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

View File

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

View File

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

View 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}') }}"

View File

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

View 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