rails-ujs-form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ Unobtrusive JS Form Extensions for rails 3.1
2
+ ======================================================
3
+
4
+ Builds on jquery-ujs to extend form functionality.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,70 @@
1
+ /**
2
+ * A small jQuery plugin to add 'rails-aware' form functionality.
3
+ *
4
+ * Basically, this understands the 'standard' rails way of structuring forms
5
+ * and makes some functions available to manipulate that.
6
+ */
7
+ (function($) {
8
+ $.fn.rails_form = function(method) {
9
+ if (methods[method]) {
10
+ return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
11
+ } else {
12
+ $.error('Method ' + method + ' does not exist on rails_form');
13
+ }
14
+ };
15
+
16
+ // in these 'this' is the $form
17
+ var methods = {
18
+ fields: function() {
19
+ return this.find('input, textarea, select');
20
+ },
21
+
22
+ error_fields: function() {
23
+ var $form = this;
24
+ return this.rails_form('fields').filter(function() {
25
+ return $form.rails_form('has_error', $(this));
26
+ });
27
+ },
28
+
29
+ submits: function() {
30
+ return this.find('input[type=submit], button[type=submit]');
31
+ },
32
+
33
+ label_for: function($field) {
34
+ return this.find('label[for=' + $field.attr('id') + ']');
35
+ },
36
+
37
+ error_on: function($field) {
38
+ return this.rails_form('label_for', $field).parents('.field_with_errors').next('.formError');
39
+ },
40
+
41
+ has_error: function($field) {
42
+ return $field.parent('.field_with_errors').length > 0;
43
+ },
44
+
45
+ clear_error: function($field) {
46
+ var id = $field.attr('id');
47
+ if (this.rails_form('has_error', $field)) { $field.unwrap() }
48
+
49
+ this.find('.field_with_errors label[for=' + id + ']')
50
+ .unwrap()
51
+ .next('.formError').remove();
52
+ },
53
+
54
+ add_error: function($field, error) {
55
+ var id = $field.attr('id');
56
+ $field.wrap('<div class="field_with_errors">');
57
+ this.find('label[for=' + id + ']')
58
+ .after('<div class="formError">' + error + '</div>')
59
+ .wrap('<div class="field_with_errors">');
60
+ },
61
+
62
+ // display standard errors as returned by JSON
63
+ set_errors: function(errors) {
64
+ for (var name in errors) {
65
+ this.rails_form('add_error', this.fields().filter('[name*=' + name + ']'), errors[name][0]);
66
+ }
67
+ }
68
+ };
69
+
70
+ }(jQuery));
@@ -0,0 +1,24 @@
1
+ (function($) {
2
+ var showErrorSelector = 'form[data-show-errors]';
3
+
4
+ $(showErrorSelector).live('ajax:error', function(event, request, status, error) {
5
+ console.log(request);
6
+ // CLIENT ERROR -- server-side validation failed. -- FIXME -should this be 422 only?
7
+ if (request.status >= 400 && request.status < 500) {
8
+ // if data is html, we replace this content of the form with the content
9
+ // of the form that we've just received back
10
+ var contentType = request.getResponseHeader('Content-Type');
11
+ if (contentType =~ /html/) {
12
+ // wrap in a div incase there are a bunch of floating elements, pull the form out
13
+ var $new_form = $('<div>' + request.responseText + '</div>').find('#' + $(this).attr('id'));
14
+ $(this).html($new_form.html());
15
+
16
+ } else if (contentType =~ /json/) {
17
+ // we will be receiving an error object back, we can pass it straight into rails_form.js
18
+ this.rails_form('set_errors', $.parseJSON(request.responseText));
19
+ } else {
20
+ throw "ujs-form/show-errors: Don't know how to handle dataType " + request.dataType;
21
+ }
22
+ }
23
+ });
24
+ }(jQuery));
@@ -0,0 +1 @@
1
+ //= require_tree ./ujs-form
@@ -0,0 +1,3 @@
1
+ module RailsUjsForm
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ module RailsUjsForm
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "rails-ujs-form/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'rails-ujs-form'
6
+ s.version = RailsUjsForm::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.author = 'Tom Coleman'
9
+ s.email = 'tom@percolatestudio.com'
10
+ s.summary = 'Unobtrusive JS Form extensions'
11
+
12
+ s.add_dependency 'jquery-rails'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-ujs-form
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Coleman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: jquery-rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: tom@percolatestudio.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - README.md
44
+ - Rakefile
45
+ - app/assets/javascripts/ujs-form.js
46
+ - app/assets/javascripts/ujs-form/rails_form.js
47
+ - app/assets/javascripts/ujs-form/show-errors.js
48
+ - lib/rails-ujs-form.rb
49
+ - lib/rails-ujs-form/version.rb
50
+ - rails-ujs-form.gemspec
51
+ homepage:
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Unobtrusive JS Form extensions
84
+ test_files: []
85
+