json_form_fields 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b61ed2181a1b510d31ec7f06d9a1805c63c0478f
4
- data.tar.gz: a951d299e770397a8d452809409cf980cb7076e9
3
+ metadata.gz: f9e124f442dece25bbafa3263cef4b834e4d99f7
4
+ data.tar.gz: 81b223d4b2cdea0215e8c7755407d46f5cde73cd
5
5
  SHA512:
6
- metadata.gz: c61ad918396b72d92ae57c9d6f7a68f6d22eb502d8ce50b8b67a87f9452455384a4e25258033816f981b0f74d74a53110b6151124efa5596c5d5bccca50ed86b
7
- data.tar.gz: fdca574e145db27ea87e929f81f6c1b53da437895fcf80604049357321b211a01e13de65369e9acf38759b9f06884a69fc41d1307c66b5e4c320e96ba7593d56
6
+ metadata.gz: fb7341f1f7995b412d42627d4b0289fbda9f4ed697b6ba28f0ac95eab6163c70211518c88c219ca8b0ead9f8cbf20d2c6c23efe57ccd3318c6e28529f0101f4e
7
+ data.tar.gz: b2a7f568de0d463a052575e79077b6846d19d1032edd7310f252f265ddbc7e49c6b25f8dfa88c7cba0cf7f415c95325ef02e3540957bc44204546bd027106fa3
data/README.md CHANGED
@@ -23,10 +23,24 @@ Or install it yourself as:
23
23
  ```ruby
24
24
  JsonFormFields::Generator.generate_inputs({ method: ["GET", "POST"] })
25
25
  ```
26
-
27
26
  Will generate a select with GET and POST options. Works also with
28
27
  password, email and text inputs.
29
28
 
29
+
30
+ You also can use the javascript generator in a Rails app, just add this to your application.js
31
+
32
+ ```javascript
33
+ //= require json_form_fields
34
+ ```
35
+
36
+ And use as follows and the script will get the JSON from the content
37
+ of the input and will generate the inputs bellow:
38
+
39
+ ```javascript
40
+ $('.your_input_id_or_class').json_form_fields();
41
+
42
+ ```
43
+
30
44
  ## Contributing
31
45
 
32
46
  1. Fork it ( https://github.com/Lucasosf/json_form_fields/fork )
@@ -2,23 +2,48 @@ module JsonFormFields
2
2
  class Generator
3
3
  def self.generate_inputs(json)
4
4
  json = JSON.parse(json)
5
- str = ''
5
+ fields = ''
6
+ json['template'] ||= "<div>yield</div>"
6
7
  json.keys.each do |key|
8
+ next if key == 'template'
9
+ str = "<label>#{key.gsub('_', ' ').capitalize}</label>"
7
10
  if json[key].kind_of?(Array)
8
- str += "<select id=\"#{key}\">"
9
- json[key].each do |option|
10
- str += "<option value=\"#{option}\">#{option}</option>"
11
+ multiple = false
12
+
13
+ if json[key].delete "_multiple"
14
+ multiple = true
11
15
  end
12
- str += "</select>"
16
+
17
+ if json[key].delete "_checkbox"
18
+ json[key].each do |value|
19
+ name = multiple ? "#{key}[]" : "#{key}"
20
+ str += "<input type=\"checkbox\" name=\"#{name}\" "\
21
+ "value=\"#{value}\" />#{value}"
22
+ end
23
+ elsif json[key].delete "_radio"
24
+ json[key].each do |value|
25
+ str += "<input type=\"radio\" name=\"#{key}\" value=\"#{value}\" />#{value}"
26
+ end
27
+ else
28
+ str += "<select name=\"#{key}\" " + (multiple ? "multiple " : "") +
29
+ "id=\"#{key}\">"
30
+ json[key].each do |option|
31
+ str += "<option value=\"#{option}\">#{option}</option>"
32
+ end
33
+ str += "</select>"
34
+ end
35
+
13
36
  elsif /password.*/ === key
14
- str += "<input type=\"password\" id=\"#{key}\">"
37
+ str += "<input name=\"#{key}\" type=\"password\" id=\"#{key}\" />"
15
38
  elsif /email.*/ === key
16
- str += "<input type=\"email\" id=\"#{key}\">"
39
+ str += "<input name=\"#{key}\" type=\"email\" id=\"#{key}\" value=\"#{json[key]}\" />"
17
40
  else
18
- str += "<input type=\"text\" id=\"#{key}\">"
41
+ str += "<input name=\"#{key}\" type=\"text\" id=\"#{key}\" value=\"#{json[key]}\" />"
19
42
  end
43
+ str = "%s" % json['template'].gsub("yield", str)
44
+ fields += str
20
45
  end
21
- str
46
+ fields
22
47
  end
23
48
  end
24
49
  end
@@ -1,3 +1,3 @@
1
1
  module JsonFormFields
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "json_form_fields/version"
2
+ require "json_form_fields/generator"
2
3
  require 'json'
3
4
 
4
5
  module JsonFormFields
@@ -0,0 +1,17 @@
1
+
2
+ $.fn.json_form_fields = function() {
3
+ json = $.parseJSON('['+ this.val() +']');
4
+ str = ''
5
+
6
+ for(var key in json){
7
+ if(json[key].constructor === Array){
8
+ str += '<select id="'+key+'">';
9
+ for(var option in json[key]){
10
+ str += '<option value="'+option+'">'+key+'</option>';
11
+ }
12
+ str += '</select>';
13
+ }else{
14
+ str += '<input type="text" id="'+key+'" value="'+json[key]+'">'
15
+ }
16
+ }
17
+ };
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_form_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-06 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.8
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.8
41
55
  description: A gem to write form fields from JSON documents
42
56
  email:
43
57
  - loslucassilva@gmail.com
@@ -50,6 +64,7 @@ files:
50
64
  - lib/json_form_fields.rb
51
65
  - lib/json_form_fields/generator.rb
52
66
  - lib/json_form_fields/version.rb
67
+ - vendor/assets/javascripts/json_form_fields.js
53
68
  homepage: https://github.com/Lucasosf/json_form_fields
54
69
  licenses:
55
70
  - MIT