Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow both vertical and horizontal forms to be used

  • Loading branch information...
commit fab4e3606adaee3e4ae366493489ca5d5f7b7761 1 parent 39ec281
Matt Huggins authored
3  Makefile
... ... @@ -1,6 +1,7 @@
1 1 NODE = node
2 2 TEST = ./node_modules/.bin/vows
3   -TESTS ?= test/*-test.js
  3 +TESTS ?= ./test/*-test.js ./test/**/*-test.js
  4 +TEST_FLAGS = -spec
4 5
5 6 test:
6 7 @NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)
39 lib/railway-bootstrap/forms/form-util.js
... ... @@ -0,0 +1,39 @@
  1 +module.exports = {
  2 + humanize: humanize,
  3 + divTag: divTag,
  4 + spanTag: spanTag,
  5 + genericTag: genericTag
  6 +};
  7 +
  8 +function humanize(name) {
  9 + return railway.utils.humanize(railway.utils.underscore(name));
  10 +}
  11 +
  12 +function divTag(params, block) {
  13 + return genericTag('div', params, block);
  14 +}
  15 +
  16 +function spanTag(params, block) {
  17 + return genericTag('span', params, block);
  18 +}
  19 +
  20 +function genericTag(name, params, block) {
  21 + var html = [];
  22 +
  23 + if (typeof(params) === 'function') {
  24 + block = params;
  25 + params = {};
  26 + }
  27 + params = params || {};
  28 +
  29 + html.push("<" + name + railway.utils.html_tag_params(params) + ">");
  30 +
  31 + if (typeof(block) === 'function') {
  32 + html.push(block());
  33 + } else if (block) {
  34 + html.push(block);
  35 + }
  36 +
  37 + html.push("</" + name + ">");
  38 + return html.join("\n");
  39 +}
94 lib/railway-bootstrap/forms/horizontal.js
... ... @@ -0,0 +1,94 @@
  1 +module.exports = HorizontalForm;
  2 +
  3 +function HorizontalForm(context, resource, params) {
  4 + var self = this;
  5 +
  6 + this.buf = arguments.callee.caller.buf;
  7 + this.context = context;
  8 + this.resource = resource;
  9 + this.params = railway.utils.safe_merge({ class: 'form-horizontal' }, params);
  10 +}
  11 +
  12 +HorizontalForm.prototype.fieldsFor = function (block) {
  13 + var context = this.context;
  14 + var resource = this.resource;
  15 + var util = require('./form-util');
  16 +
  17 + arguments.callee.buf = this.buf;
  18 +
  19 + context.formTag(this.params, function () {
  20 + if (block) {
  21 + block({
  22 + input: input('input'),
  23 + file: input('file'),
  24 + textarea: input('textarea'),
  25 + submit: submit
  26 + });
  27 + }
  28 + });
  29 +
  30 + function input(inputType) {
  31 + return function (name, params) {
  32 + var output;
  33 + params = params || {};
  34 +
  35 + context.fields_for(resource, function (form) {
  36 + output = controlGroup(name, function () {
  37 + var label = params.label;
  38 + var html = [];
  39 +
  40 + delete params.label;
  41 +
  42 + if (label !== false) {
  43 + label = label || util.humanize(name);
  44 + html.push(form.label(name, label, { class: 'control-label' }));
  45 + }
  46 +
  47 + html.push(controls(name, function () {
  48 + return form[inputType](name, params);
  49 + }));
  50 +
  51 + return html.join("\n");
  52 + });
  53 + });
  54 +
  55 + return output;
  56 + };
  57 + }
  58 +
  59 + function submit(name, params) {
  60 + var output;
  61 + params = params || {};
  62 +
  63 + context.fields_for(resource, function (form) {
  64 + output = actions(function () {
  65 + params = railway.utils.safe_merge({ class: 'btn btn-primary' }, params);
  66 + return form.submit(name, params);
  67 + });
  68 + });
  69 +
  70 + return output;
  71 + }
  72 +
  73 + function controlGroup(name, block) {
  74 + var css = 'control-group' + ((resource.errors && resource.errors[name]) ? ' error' : '');
  75 + return util.divTag({ class: css }, block);
  76 + }
  77 +
  78 + function controls(name, block) {
  79 + return util.divTag({ class: 'controls' }, function () {
  80 + var html = [];
  81 + var errors = resource.errors && resource.errors[name];
  82 +
  83 + html.push(block());
  84 + if (errors) {
  85 + html.push(util.spanTag({ class: 'help-inline' }, errors ? errors.join(', ') : null));
  86 + }
  87 + return html.join("\n");
  88 + });
  89 + }
  90 +
  91 + function actions(block) {
  92 + return util.divTag({ class: 'form-actions' }, block);
  93 + }
  94 +};
64 lib/railway-bootstrap/forms/vertical.js
... ... @@ -0,0 +1,64 @@
  1 +module.exports = VerticalForm;
  2 +
  3 +function VerticalForm(context, resource, params) {
  4 + var self = this;
  5 +
  6 + this.buf = arguments.callee.caller.buf;
  7 + this.context = context;
  8 + this.resource = resource;
  9 + this.params = railway.utils.safe_merge({ class: 'form-vertical' }, params);
  10 +}
  11 +
  12 +VerticalForm.prototype.fieldsFor = function (block) {
  13 + var context = this.context;
  14 + var resource = this.resource;
  15 + var util = require('./form-util');
  16 +
  17 + arguments.callee.buf = this.buf;
  18 +
  19 + context.formTag(this.params, function () {
  20 + if (block) {
  21 + block({
  22 + input: input('input'),
  23 + file: input('file'),
  24 + textarea: input('textarea'),
  25 + submit: submit
  26 + });
  27 + }
  28 + });
  29 +
  30 + function input(inputType) {
  31 + return function (name, params) {
  32 + var output = [];
  33 + params = params || {};
  34 +
  35 + context.fields_for(resource, function (form) {
  36 + var label = params.label;
  37 + delete params.label;
  38 +
  39 + if (label !== false) {
  40 + label = label || util.humanize(name);
  41 + output.push(form.label(name, label));
  42 + }
  43 +
  44 + output.push(form[inputType](name, params));
  45 + });
  46 +
  47 + return output.join("\n");
  48 + };
  49 + }
  50 +
  51 + function submit(name, params) {
  52 + var output;
  53 + params = params || {};
  54 +
  55 + context.fields_for(resource, function (form) {
  56 + params = railway.utils.safe_merge({ class: 'btn btn-primary' }, params);
  57 + output = util.divTag(function () {
  58 + return form.submit(name, params);
  59 + });
  60 + });
  61 +
  62 + return output;
  63 + }
  64 +};
147 lib/railway-bootstrap/helpers.js
... ... @@ -1,140 +1,39 @@
  1 +var util = require('util');
  2 +
1 3 /**
2   - * Form tag helper.
  4 + * Twitter Bootstrap form tag helper.
3 5 *
4 6 * bootstrapFieldsFor(user, function (form) { ... });
5 7 */
6   -module.exports.bootstrapFormFor = function (resource, params, block) {
  8 +exports.bootstrapFormFor = exports.bootstrap_form_for = function (resource, params, block) {
7 9 var self = this;
8 10 var buf = arguments.callee.buf = arguments.callee.caller.buf;
9 11
10   - if (resource && resource.modelName) {
11   - if (typeof params !== 'object') {
12   - params = {};
13   - }
14   - if (!params.method) {
15   - params.method = 'PUT';
16   - }
17   - if (!params.action) {
18   - params.action = railway.routeMapper.pathTo[railway.utils.underscore(resource.modelName)](resource);
19   - }
20   - }
21   -
22   - params = railway.utils.safe_merge({ class: 'form-horizontal' }, params);
23   -
24   - this.formTag(params, function () {
25   - if (block) {
26   - module.exports.bootstrapFieldsFor.call(self, resource, block);
27   - }
28   - });
29   -};
30   -module.exports.bootstrap_form_for = module.exports.bootstrapFormFor;
31   -
32   -/**
33   - * Form fields for resource helper.
34   - *
35   - * bootstrapFieldsFor(user, function (form) {
36   - * form.input('email', { label: 'E-mail Address' });
37   - * form.input('password', { type: 'password' });
38   - * form.submit('Login');
39   - * });
40   - */
41   -module.exports.bootstrapFieldsFor = function (resource, block) {
42   - var self = this;
43   -
44   - var inputs = {
45   - submit: function (name, params) {
46   - var output;
47   - params = params || {};
48   -
49   - self.fields_for(resource, function (form) {
50   - output = actions(function () {
51   - params = railway.utils.safe_merge({ class: 'btn btn-primary' }, params);
52   - return form.submit(name, params);
53   - });
54   - });
55   - return output;
56   - }
57   - };
58   -
59   - ['input', 'file', 'textarea'].forEach(function (inputType) {
60   - inputs[inputType] = function (name, params) {
61   - var output;
62   - params = params || {};
63   -
64   - self.fields_for(resource, function (form) {
65   - output = controlGroup(name, function () {
66   - var label = params.label;
67   - var html = [];
68   -
69   - if (label !== false) {
70   - label = label || humanize(name);
71   - html.push(form.label(name, label, { class: 'control-label' }));
72   - }
73   -
74   - html.push(controls(name, function () {
75   - return form[inputType](name, params);
76   - }));
77   -
78   - return html.join("\n");
79   - });
80   - });
81   - return output;
82   - };
83   - });
84   -
85   - block(inputs);
86   -
87   - function humanize(name) {
88   - var u = railway.utils;
89   - return u.humanize(u.underscore(name));
  12 + if (typeof(params) == 'function') {
  13 + block = params;
  14 + params = {};
90 15 }
  16 + params = params || {};
91 17
92   - function controlGroup(name, block) {
93   - var css = 'control-group' + ((resource.errors && resource.errors[name]) ? ' error' : '');
94   - return divTag({ class: css }, block);
95   - }
  18 + var format = params.format || 'vertical';
  19 + delete params.format;
96 20
97   - function controls(name, block) {
98   - return divTag({ class: 'controls' }, function () {
99   - var html = [];
100   - var errors = resource.errors && resource.errors[name];
101   -
102   - html.push(block());
103   - if (errors) {
104   - html.push(spanTag({ class: 'help-inline' }, errors ? errors.join(', ') : null));
105   - }
106   - return html.join("\n");
107   - });
  21 + if (resource && resource.modelName) {
  22 + params.method = params.method || 'PUT';
  23 + params.action = params.action || railway.routeMapper.pathTo[railway.utils.underscore(resource.modelName)](resource);
108 24 }
109 25
110   - function actions(block) {
111   - return divTag({ class: 'form-actions' }, block);
112   - }
  26 + var Form = require('./forms/' + format);
  27 + var form = new Form(this, resource, params);
  28 + form.fieldsFor(block);
113 29 };
114   -module.exports.bootstrap_fields_for = module.exports.bootstrapFieldsFor;
115 30
116 31 /**
117   - * Private helper methods.
  32 + * Twitter Bootstrap form tag helpers for specific form types.
118 33 */
119   -function divTag(params, block) {
120   - return genericTag('div', params, block);
121   -}
122   -
123   -function spanTag(params, block) {
124   - return genericTag('span', params, block);
125   -}
126   -
127   -function genericTag(name, params, block) {
128   - var html = [];
129   -
130   - html.push("<" + name + railway.utils.html_tag_params(params) + ">");
131   -
132   - if (typeof(block) === 'function') {
133   - html.push(block());
134   - } else if (block) {
135   - html.push(block);
136   - }
137   -
138   - html.push("</" + name + ">");
139   - return html.join("\n");
140   -}
  34 +['basic', 'horizontal'].forEach(function (format) {
  35 + exports[format + 'FormFor'] = exports[format + '_form_for'] = function (resource, params, block) {
  36 + params = railway.utils.safe_merge({ format: format }, params);
  37 + exports.bootstrapFormFor(resource, params, block);
  38 + };
  39 +});
18 lib/railway-bootstrap/index.js
@@ -6,3 +6,21 @@ exports.init = function () {
6 6 HelperSet.prototype[name] = helpers[name];
7 7 }
8 8 };
  9 +
  10 +exports.bootstrapFormFor = function (resource, params, block) {
  11 + var self = this;
  12 + var buf = arguments.callee.buf = arguments.callee.caller.buf;
  13 +
  14 + params = params || {};
  15 + params.format = params.format || 'horizontal';
  16 +
  17 + if (resource && resource.modelName) {
  18 + params.method = params.method || 'PUT';
  19 + params.action = params.action || railway.routeMapper.pathTo[railway.utils.underscore(resource.modelName)](resource);
  20 + }
  21 +
  22 + var Form = require('./forms/' + params.format);
  23 + new Form(this, resource, params, block);
  24 +};
  25 +
  26 +exports.bootstrap_form_for = exports.bootstrapFormFor;
2  package.json
@@ -13,7 +13,7 @@
13 13 "railway": ">= 0.2.6"
14 14 },
15 15 "scripts": {
16   - "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
  16 + "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js test/**/*-test.js -spec"
17 17 },
18 18 "engines": { "node": ">= 0.4.0" },
19 19 "licenses": [ {
26 readme.md
Source Rendered
@@ -54,19 +54,29 @@ key/value pairs included in the object will be converted to HTML attributes on t
54 54
55 55 This will produce the following HTML:
56 56
57   - <form class="form-horizontal" action="/users/new" method="post">
  57 + <form class="well" action="/users/new" method="post">
58 58 <input type="hidden" name="authenticity_token" value="f46d9de27e45fef8dce10a36dcdc7be7fa8612af" />
59 59 <input type="hidden" name="_method" value="PUT" />
60 60 <!-- any other calls made on the form object will render here -->
61 61 </form>
62 62
63   -For either format of `bootstrapFormFor` that is used, the `callback` parameter
64   -will be provided with `form` object. This object provides several methods used
65   -for rendering form fields. By default, each field is wrapped according to
66   -Twitter Bootstrap's [horizontal form](http://twitter.github.com/bootstrap/base-css.html#forms)
67   -format with an accompanying label
  63 +The type of the Bootstrap form that you want to use can be provided in the
  64 +`options` parameter as well. Currently, only "vertical" and "horizontal" are
  65 +provided, with "vertical" being the default. The available types map to the
  66 +following CSS classes. (See the [Forms section of the Twitter Bootstrap docs](http://twitter.github.com/bootstrap/base-css.html#forms)
  67 +for more details on form types.)
68 68
69   -The following methods are provided:
  69 +* `vertical` = `form-vertical`
  70 +* `horizontal` = `form-horizontal`
  71 +
  72 +Finally, for either format of `bootstrapFormFor` that is used, the `callback`
  73 +parameter will be provided with `form` object. This object provides several
  74 +methods used for rendering form fields. By default, each field is wrapped
  75 +according to Twitter Bootstrap's [horizontal form](http://twitter.github.com/bootstrap/base-css.html#forms)
  76 +format with an accompanying label.
  77 +
  78 +The `form` object that is provided to your callback has the following methods
  79 +available:
70 80
71 81 * `input (name, options)`: Creates an `<input>` element, where `name` is the name of
72 82 the resource's property to render. If no `options` are provided, the input type
@@ -84,4 +94,4 @@ Created By:
84 94
85 95 License:
86 96 --------
87   -Railway-Bootstrap is released under the [MIT license](http://www.opensource.org/licenses/MIT).
  97 +Railway Bootstrap is released under the [MIT license](http://www.opensource.org/licenses/MIT).
167 test/forms/horizontal-test.js
... ... @@ -0,0 +1,167 @@
  1 +require('railway').createServer();
  2 +require('railway-bootstrap').init();
  3 +
  4 +var vows = require('vows');
  5 +var assert = require('assert');
  6 +var HorizontalForm = require('railway-bootstrap/forms/horizontal');
  7 +
  8 +function User() {
  9 + this.id = 1;
  10 + this.username = 'AzureDiamond';
  11 + this.email = 'hunter2@example.com';
  12 + this.description = 'doesnt look like stars to me';
  13 + this.avatar = null;
  14 +}
  15 +
  16 +var user = new User();
  17 +
  18 +vows.describe('HorizontalForm').addBatch({
  19 + before: function () {
  20 + railway.helpers.controller = {
  21 + request: { csrfParam: 'csrf_name', csrfToken: 'csrf_value' }
  22 + }
  23 + },
  24 +
  25 + 'constructor': {
  26 + topic: function () {
  27 + arguments.callee.buf = [];
  28 + return new HorizontalForm(railway.helpers, user);
  29 + },
  30 +
  31 + 'includes class in form tag': function (form) {
  32 + form.fieldsFor();
  33 + var output = form.buf.join('');
  34 + var expected = /<form[^>]*class="form-horizontal"[^>]*>/;
  35 + assert.match(output, expected);
  36 + }
  37 + },
  38 +
  39 + 'fieldsFor': {
  40 + topic: function () {
  41 + arguments.callee.buf = [];
  42 + return new HorizontalForm(railway.helpers, user)
  43 + },
  44 +
  45 + 'with type input': {
  46 + 'creates control group': function (form) {
  47 + form.fieldsFor(function (form) {
  48 + var output = form.input('username');
  49 + assert.match(output, /^<div class="control-group">/ );
  50 + assert.match(output, /<\/div>$/ );
  51 + });
  52 + },
  53 +
  54 + 'creates label': function (form) {
  55 + form.fieldsFor(function (form) {
  56 + var output = form.input('username');
  57 + var expected = '<label for="username" class="control-label">Username</label>';
  58 + assert.include(output, expected);
  59 + });
  60 + },
  61 +
  62 + 'creates text input': function (form) {
  63 + form.fieldsFor(function (form) {
  64 + var output = form.input('username');
  65 + var expected = '<input name="username" id="username" value="' + user.username + '" />';
  66 + assert.include(output, expected);
  67 + });
  68 + },
  69 +
  70 + 'creates control container around input': function (form) {
  71 + form.fieldsFor(function (form) {
  72 + var output = form.input('username');
  73 + var expected = /<div class="controls">\s*<input\b[^>]*\/>\s*<\/div>/;
  74 + assert.match(output, expected);
  75 + });
  76 + }
  77 + },
  78 +
  79 + 'with type textarea': {
  80 + 'creates control group': function (form) {
  81 + form.fieldsFor(function (form) {
  82 + var output = form.textarea('description');
  83 + assert.match(output, /^<div class="control-group">/ );
  84 + assert.match(output, /<\/div>$/ );
  85 + });
  86 + },
  87 +
  88 + 'creates label': function (form) {
  89 + form.fieldsFor(function (form) {
  90 + var output = form.textarea('description');
  91 + var expected = '<label for="description" class="control-label">Description</label>';
  92 + assert.include(output, expected);
  93 + });
  94 + },
  95 +
  96 + 'creates textarea': function (form) {
  97 + form.fieldsFor(function (form) {
  98 + var output = form.textarea('description');
  99 + var expected = '<textarea name="description" id="description">' + user.description + '</textarea>';
  100 + assert.include(output, expected);
  101 + });
  102 + },
  103 +
  104 + 'creates control container around textarea': function (form) {
  105 + form.fieldsFor(function (form) {
  106 + var output = form.textarea('description');
  107 + var expected = /<div class="controls">\s*<textarea\b[^>]*>.*<\/textarea>\s*<\/div>/;
  108 + assert.match(output, expected);
  109 + });
  110 + }
  111 + },
  112 +
  113 + 'with type file': {
  114 + 'creates control group': function (form) {
  115 + form.fieldsFor(function (form) {
  116 + var output = form.file('avatar');
  117 + var expectedStart = /^<div class="control-group">/;
  118 + var expectedEnd = /<\/div>$/;
  119 + assert.match(output, expectedStart);
  120 + assert.match(output, expectedEnd);
  121 + });
  122 + },
  123 +
  124 + 'creates label': function (form) {
  125 + form.fieldsFor(function (form) {
  126 + var output = form.file('avatar');
  127 + var expected = '<label for="avatar" class="control-label">Avatar</label>';
  128 + assert.include(output, expected);
  129 + });
  130 + },
  131 +
  132 + 'creates file input': function (form) {
  133 + form.fieldsFor(function (form) {
  134 + var output = form.file('avatar');
  135 + var expected = '<input name="avatar" id="avatar" type="file" />';
  136 + assert.include(output, expected);
  137 + });
  138 + },
  139 +
  140 + 'creates control container around input': function (form) {
  141 + form.fieldsFor(function (form) {
  142 + var output = form.file('avatar');
  143 + var expected = /<div class="controls">\s*<input\b[^>]*\/>\s*<\/div>/;
  144 + assert.match(output, expected);
  145 + });
  146 + }
  147 + },
  148 +
  149 + 'with type submit': {
  150 + 'creates form actions container': function (form) {
  151 + form.fieldsFor(function (form) {
  152 + var output = form.submit();
  153 + assert.match(output, /^<div class="form-actions">/ );
  154 + assert.match(output, /<\/div>$/ );
  155 + });
  156 + },
  157 +
  158 + 'creates submit button': function (form) {
  159 + form.fieldsFor(function (form) {
  160 + var output = form.submit('Register');
  161 + var expected = '<button type="submit" class="btn btn-primary">Register</button>';
  162 + assert.include(output, expected);
  163 + });
  164 + }
  165 + }
  166 + }
  167 +}).export(module);
109 test/forms/vertical-test.js
... ... @@ -0,0 +1,109 @@
  1 +require('railway').createServer();
  2 +require('railway-bootstrap').init();
  3 +
  4 +var vows = require('vows');
  5 +var assert = require('assert');
  6 +var VerticalForm = require('railway-bootstrap/forms/vertical');
  7 +
  8 +function User() {
  9 + this.id = 1;
  10 + this.username = 'AzureDiamond';
  11 + this.email = 'hunter2@example.com';
  12 + this.description = 'doesnt look like stars to me';
  13 + this.avatar = null;
  14 +}
  15 +
  16 +var user = new User();
  17 +
  18 +vows.describe('VerticalForm').addBatch({
  19 + before: function () {
  20 + railway.helpers.controller = {
  21 + request: { csrfParam: 'csrf_name', csrfToken: 'csrf_value' }
  22 + }
  23 + },
  24 +
  25 + 'constructor': {
  26 + topic: function () {
  27 + arguments.callee.buf = [];
  28 + return new VerticalForm(railway.helpers, user);
  29 + },
  30 +
  31 + 'includes class in form tag': function (form) {
  32 + form.fieldsFor();
  33 + var output = form.buf.join('');
  34 + var expected = /<form[^>]*class="form-vertical"[^>]*>/;
  35 + assert.match(output, expected);
  36 + }
  37 + },
  38 +
  39 + 'fieldsFor': {
  40 + topic: function () {
  41 + arguments.callee.buf = [];
  42 + return new VerticalForm(railway.helpers, user)
  43 + },
  44 +
  45 + 'with type input': {
  46 + 'creates label': function (form) {
  47 + form.fieldsFor(function (form) {
  48 + var output = form.input('username');
  49 + var expected = '<label for="username">Username</label>';
  50 + assert.include(output, expected);
  51 + });
  52 + },
  53 +
  54 + 'creates text input': function (form) {
  55 + form.fieldsFor(function (form) {
  56 + var output = form.input('username');
  57 + var expected = '<input name="username" id="username" value="' + user.username + '" />';
  58 + assert.include(output, expected);
  59 + });
  60 + }
  61 + },
  62 +
  63 + 'with type textarea': {
  64 + 'creates label': function (form) {
  65 + form.fieldsFor(function (form) {
  66 + var output = form.textarea('description');
  67 + var expected = '<label for="description">Description</label>';
  68 + assert.include(output, expected);
  69 + });
  70 + },
  71 +
  72 + 'creates textarea': function (form) {
  73 + form.fieldsFor(function (form) {
  74 + var output = form.textarea('description');
  75 + var expected = '<textarea name="description" id="description">' + user.description + '</textarea>';
  76 + assert.include(output, expected);
  77 + });
  78 + }
  79 + },
  80 +
  81 + 'with type file': {
  82 + 'creates label': function (form) {
  83 + form.fieldsFor(function (form) {
  84 + var output = form.file('avatar');
  85 + var expected = '<label for="avatar">Avatar</label>';
  86 + assert.include(output, expected);
  87 + });
  88 + },
  89 +
  90 + 'creates file input': function (form) {
  91 + form.fieldsFor(function (form) {
  92 + var output = form.file('avatar');
  93 + var expected = '<input name="avatar" id="avatar" type="file" />';
  94 + assert.include(output, expected);
  95 + });
  96 + }
  97 + },
  98 +
  99 + 'with type submit': {
  100 + 'creates submit button': function (form) {
  101 + form.fieldsFor(function (form) {
  102 + var output = form.submit('Register');
  103 + var expected = '<button type="submit" class="btn btn-primary">Register</button>';
  104 + assert.include(output, expected);
  105 + });
  106 + }
  107 + }
  108 + }
  109 +}).export(module);
138 test/helpers-test.js
... ... @@ -1,17 +1,9 @@
1 1 require('railway').createServer();
2   -require('../lib/railway-bootstrap').init();
  2 +require('railway-bootstrap').init();
3 3
4 4 var vows = require('vows');
5 5 var assert = require('assert');
6 6
7   -function User() {
8   - this.id = 1;
9   - this.username = 'AzureDiamond';
10   - this.email = 'hunter2@example.com';
11   - this.description = 'doesnt look like stars to me';
12   - this.avatar = null;
13   -}
14   -
15 7 vows.describe('Helpers').addBatch({
16 8 before: function () {
17 9 railway.helpers.controller = {
@@ -25,7 +17,7 @@ vows.describe('Helpers').addBatch({
25 17 railway.helpers.bootstrapFormFor(null, function () {});
26 18
27 19 var output = buf.join("\n");
28   - var expectedStart = /^<form class="form-horizontal" method="POST">/;
  20 + var expectedStart = /^<form[^>]*method="POST"[^>]*>/;
29 21 var expectedEnd = /<\/form>$/;
30 22
31 23 assert.match(output, expectedStart);
@@ -40,131 +32,5 @@ vows.describe('Helpers').addBatch({
40 32 var expected = '<input type="hidden" name="csrf_name" value="csrf_value" />';
41 33 assert.include(output, expected);
42 34 }
43   - },
44   -
45   - 'bootstrapFieldsFor': {
46   - topic: new User(),
47   -
48   - 'with type input': {
49   - 'creates control group': function (user) {
50   - railway.helpers.bootstrapFieldsFor(user, function (form) {
51   - var output = form.input('username');
52   - assert.match(output, /^<div class="control-group">/ );
53   - assert.match(output, /<\/div>$/ );
54   - });
55   - },
56   -
57   - 'creates label': function (user) {
58   - railway.helpers.bootstrapFieldsFor(user, function (form) {
59   - var output = form.input('username');
60   - var expected = '<label for="username" class="control-label">Username</label>';
61   - assert.include(output, expected);
62   - });
63   - },
64   -
65   - 'creates text input': function (user) {
66   - railway.helpers.bootstrapFieldsFor(user, function (form) {
67   - var output = form.input('username');
68   - var expected = '<input name="username" id="username" value="' + user.username + '" />';
69   - assert.include(output, expected);
70   - });
71   - },
72   -
73   - 'creates control container around input': function (user) {
74   - railway.helpers.bootstrapFieldsFor(user, function (form) {
75   - var output = form.input('username');
76   - var expected = /<div class="controls">\s*<input\b[^>]*\/>\s*<\/div>/;
77   - assert.match(output, expected);
78   - });
79   - }
80   - },
81   -
82   - 'with type textarea': {
83   - 'creates control group': function (user) {
84   - railway.helpers.bootstrapFieldsFor(user, function (form) {
85   - var output = form.textarea('description');
86   - assert.match(output, /^<div class="control-group">/ );
87   - assert.match(output, /<\/div>$/ );
88   - });
89   - },
90   -
91   - 'creates label': function (user) {
92   - railway.helpers.bootstrapFieldsFor(user, function (form) {
93   - var output = form.textarea('description');
94   - var expected = '<label for="description" class="control-label">Description</label>';
95   - assert.include(output, expected);
96   - });
97   - },
98   -
99   - 'creates textarea': function (user) {
100   - railway.helpers.bootstrapFieldsFor(user, function (form) {
101   - var output = form.textarea('description');
102   - var expected = '<textarea name="description" id="description">' + user.description + '</textarea>';
103   - assert.include(output, expected);
104   - });
105   - },
106   -
107   - 'creates control container around textarea': function (user) {
108   - railway.helpers.bootstrapFieldsFor(user, function (form) {
109   - var output = form.textarea('description');
110   - var expected = /<div class="controls">\s*<textarea\b[^>]*>.*<\/textarea>\s*<\/div>/;
111   - assert.match(output, expected);
112   - });
113   - }
114   - },
115   -
116   - 'with type file': {
117   - 'creates control group': function (user) {
118   - railway.helpers.bootstrapFieldsFor(user, function (form) {
119   - var output = form.file('avatar');
120   - var expectedStart = /^<div class="control-group">/;
121   - var expectedEnd = /<\/div>$/;
122   - assert.match(output, expectedStart);
123   - assert.match(output, expectedEnd);
124   - });
125   - },
126   -
127   - 'creates label': function (user) {
128   - railway.helpers.bootstrapFieldsFor(user, function (form) {
129   - var output = form.file('avatar');
130   - var expected = '<label for="avatar" class="control-label">Avatar</label>';
131   - assert.include(output, expected);
132   - });
133   - },
134   -
135   - 'creates file input': function (user) {
136   - railway.helpers.bootstrapFieldsFor(user, function (form) {
137   - var output = form.file('avatar');
138   - var expected = '<input name="avatar" id="avatar" type="file" />';
139   - assert.include(output, expected);
140   - });
141   - },
142   -
143   - 'creates control container around input': function (user) {
144   - railway.helpers.bootstrapFieldsFor(user, function (form) {
145   - var output = form.file('avatar');
146   - var expected = /<div class="controls">\s*<input\b[^>]*\/>\s*<\/div>/;
147   - assert.match(output, expected);
148   - });
149   - }
150   - },
151   -
152   - 'with type submit': {
153   - 'creates form actions container': function (user) {
154   - railway.helpers.bootstrapFieldsFor(user, function (form) {
155   - var output = form.submit();
156   - assert.match(output, /^<div class="form-actions">/ );
157   - assert.match(output, /<\/div>$/ );
158   - });
159   - },
160   -
161   - 'creates submit button': function (user) {
162   - railway.helpers.bootstrapFieldsFor(user, function (form) {
163   - var output = form.submit('Register');
164   - var expected = '<button type="submit" class="btn btn-primary">Register</button>';
165   - assert.include(output, expected);
166   - });
167   - }
168   - }
169 35 }
170 36 }).export(module);

0 comments on commit fab4e36

Please sign in to comment.
Something went wrong with that request. Please try again.