Skip to content

Commit

Permalink
Added FormBuilder and formFor. Releasing new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
malomalo committed Mar 17, 2014
1 parent 892ccf3 commit 7e3446e
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## Edge
## 0.5.0 (March 17th, 2014)

New Features:

- added `FormBuilder` helper
- added `Viking.View.Helpers.formFor` helper for creating and rendering `FormBuilder`s

Bugfixes:

Expand Down
13 changes: 10 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'erb'
require 'json'
require 'rest_client'
require './test/source_annotation_extractor'
require 'rake/clean'

# Setup Sprockets
environment = Sprockets::Environment.new
Expand All @@ -14,16 +15,22 @@ environment.append_path 'test/dependencies'
environment.unregister_postprocessor 'application/javascript', Sprockets::SafetyColons

desc "Compile viking.js"
task :compile do
file "viking.js" => FileList["lib/**/*.js"] do
FileUtils.rm_f('./viking.js')

File.open('./viking.js', "w") do |file|
file.write(environment['viking.js'].to_s)
end
end

desc "Compile viking.js"
task :compile => ["viking.js"]

CLEAN.include("test/coverage")
CLOBBER.include("viking.js")

desc "Build the docco documentation"
task :doc => :compile do
task :doc => "viking.js" do
check 'docco', 'docco', 'https://github.com/jashkenas/docco'
system 'docco --css docco.css viking.js'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/viking/preamble.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Viking.js 0.4.0
// Viking.js 0.5.0
//
// (c) 2012-2013 Jonathan Bracy, 42Floors Inc.
// Viking.js may be freely distributed under the MIT license.
Expand Down
3 changes: 2 additions & 1 deletion lib/viking/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
// TODO: date_select
// TODO: day_field
// TODO: file_field
//= require viking/view/helpers/form_helpers/form_for
//= require viking/view/helpers/form_helpers/hidden_field
//= require viking/view/helpers/form_helpers/label
// TODO: month_field
Expand All @@ -60,4 +61,4 @@
// TODO: week_field
// TODO: year_field
//
// require viking/view/form_builder
//= require viking/view/form_builder
78 changes: 78 additions & 0 deletions lib/viking/view/form_builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*global contentTag, checkBox, hiddenField, hiddenFieldTag, label, passwordField, radioButton, textArea, textField*/

function FormBuilder(model, options, content) {
if (typeof options === 'function') {
content = options;
options = {};
}

this.model = model;
this.options = options;
this.content = content;
}

FormBuilder.prototype = {

render: function () {
var content = this.content;
var options = _.clone(this.options);
var method = options.method;

if (options.multipart === true) {
options.enctype = "multipart/form-data";
options.method = 'post';
delete options.multipart;
} else if (!options.method) {
options.method = 'get';
}


if ( (options.method !== 'get' && options.method !== 'post') || (method && method !== options.method) ) {
var form = this;
options.method = 'post';
content = _.wrap(content, function(func, form) {
var hiddenInput = Viking.View.Helpers.hiddenFieldTag('_method', method);
return Viking.View.Helpers.contentTag('div', hiddenInput, {style: 'margin:0;padding:0;display:inline'}, false) + func(form);
});
}

return Viking.View.Helpers.contentTag('form', content(this), options, false);
},

checkBox: function(attribute, options, checkedValue, uncheckedValue) {
return Viking.View.Helpers.checkBox(this.model, attribute, options, checkedValue, uncheckedValue);
},

collectionSelect: function(attribute, collection, valueAttribute, textAttribute, options) {
return Viking.View.Helpers.collectionSelect(this.model, attribute, collection, valueAttribute, textAttribute, options);
},

hiddenField: function(attribute, options) {
return Viking.View.Helpers.hiddenField(this.model, attribute, options);
},

label: function(attribute, content, options) {
return Viking.View.Helpers.label(this.model, attribute, content, options);
},

passwordField: function(attribute, options) {
return Viking.View.Helpers.passwordField(this.model, attribute, options);
},

radioButton: function(attribute, tagValue, options) {
return Viking.View.Helpers.radioButton(this.model, attribute, tagValue, options);
},

select: function(attribute, collection, options) {
return Viking.View.Helpers.select(this.model, attribute, collection, options);
},

textArea: function(attribute, options) {
return Viking.View.Helpers.textArea(this.model, attribute, options);
},

textField: function(attribute, options) {
return Viking.View.Helpers.textField(this.model, attribute, options);
}

};
20 changes: 20 additions & 0 deletions lib/viking/view/helpers/form_helpers/form_for.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// formFor(model, options = {}, content = func(f))
// ===============================================
//
// Creates a FormBuilder for the model and passes it as the first argument to
// the `content` function.
//
// Examples
// --------
// formFor(account, function (f) { return f.hiddenField('pass_confirm'); })
// // => <input type="hidden" name="account[pass_confirm]" value="">
Viking.View.Helpers.formFor = function (model, options, content) {
if (typeof options === 'function') {
content = options;
options = {};
}

var form = new FormBuilder(model, options, content);

return form.render();
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"test": "phantomjs test/runner.js http://127.0.0.1:4321/test/index.html"
},
"main" : "viking.js",
"version" : "0.4.0"
"version" : "0.5.0"
}
Loading

0 comments on commit 7e3446e

Please sign in to comment.