form_angular 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df338d7e4cd77e4e218a855bd39eac24349834dd
4
+ data.tar.gz: a533b71363ac59792bce35ec008d073bfaa5db57
5
+ SHA512:
6
+ metadata.gz: 20cc36741a1e26b7df18c0d1f2bc0ad075808e9b13eebc131cc70a9b42b890d256a78ebd72ae287214e1d4a823a266eb7e2d626fb583ead04c18f4186cb49668
7
+ data.tar.gz: e35f8772c5f0e3e7c9f6de0bf466a1be1eee7229c9e0daae28ffc2dd532a3ff55e90bca6e87b3fd2fb6e4cd3254c84477d0d179084d9418601723df8e341d083
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 NexKap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ form_angular
2
+ ============
3
+
4
+ Rails gem for forms using Angular
5
+
6
+ Currently uses simple forms, might grow out of it and stay compatible for performance purposes.
7
+
8
+ # Enabling an angular form
9
+
10
+ `simple_form_for Object|Symbol|Class ngform:true`
11
+
12
+ This enables ng-model on every field in the form. The value of the attribute is extracted from the context, where every simple_fields_for instruction adds a nested level within the context, using snake case constant names
13
+
14
+ Therefore, for an object `class A attr SomeString b, AThirdAttr` becomes a.some_string.a_third_attr
15
+
16
+ The form name is set by the object class name or symbol as a camel-case string.
17
+
18
+
19
+ # TBD
20
+ - implement black box tests
21
+ - use autoload
22
+ - grow out of simple form
23
+ - factor code that climbs up the form/fieldset tree to build strings
24
+ - add option to override default rails attribute naming with the ng_model's value
25
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'form_angular/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "form_angular"
8
+ spec.version = FormAngular::VERSION
9
+ spec.authors = ["Guillaume Balaine", "Finexkap"]
10
+ spec.email = ["igosuki@gmail.com", "dev@finexkap.com"]
11
+ spec.description = %q{AngularJS Rails Forms}
12
+ spec.summary = %q{AngularJS Rails Forms}
13
+ spec.homepage = "http://nexkap.github.io/form_angular"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", ">= 1.3.0"
22
+ spec.add_development_dependency "rake"
23
+ end
24
+
@@ -0,0 +1,11 @@
1
+ module FormAngular
2
+
3
+ end
4
+
5
+ %w(helpers errors fields forms).each do |lib|
6
+ Dir.glob("#{File.dirname(__FILE__)}/form_angular/#{lib}/*").each { |file|
7
+ require file
8
+ }
9
+ end
10
+
11
+
@@ -0,0 +1,31 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helpers', 'ng_option_utils')
2
+
3
+ module FormAngular
4
+ module Errors
5
+ module NgError
6
+ include NgOptionUtils
7
+
8
+ def ng_error
9
+ error_tags = ""
10
+ error_tags = template.content_tag(:span, "{{errors['#{tag_name}'].server}}", html_options("server"))
11
+ if @options[:ng_errors]
12
+ @options[:ng_errors].each do |validate_key|
13
+ error_tags += template.content_tag(:span, I18n.t("activerecord.errors.#{lookup_model_names.last}.#{attribute_name}.#{validate_key}"), html_options(validate_key))
14
+ end
15
+ end
16
+ error_tags
17
+ end
18
+
19
+ def has_html_error
20
+ ng_error.present?
21
+ end
22
+
23
+ def html_options(key)
24
+ html_options ||= {}
25
+ html_options.merge!({'ng-show' => "#{ng_access_expression}.$error.#{key} && #{ng_access_expression}.$invalid && #{ng_access_expression}.$dirty", class: "ng-cloak text-danger"})
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ SimpleForm::Inputs::Base.send(:include, FormAngular::Errors::NgError)
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helpers', 'ng_option_utils')
2
+
3
+ module FormAngular
4
+ module Errors
5
+ module NgInputError
6
+ include NgOptionUtils
7
+
8
+ def ng_input_error
9
+ html_wrapper_options()
10
+ nil
11
+ end
12
+
13
+ def has_ng_input_error
14
+ ng_input_error.present?
15
+ end
16
+
17
+ def html_wrapper_options
18
+ @options[:wrapper_html] ||= {}
19
+ @options[:wrapper_html].merge!(
20
+ 'ng-class' => "{success:'has-success', error:'has-error'}[#{ng_form_expression}.validityClass('#{tag_name}')]"
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ SimpleForm::Inputs::Base.send(:include, FormAngular::Errors::NgInputError)
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helpers', 'ng_option_utils')
2
+
3
+ module FormAngular
4
+ module FieldSet
5
+ module NgFields
6
+ include NgOptionUtils
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ alias_method_chain :simple_fields_for, :ng_model
11
+ end
12
+
13
+ def simple_fields_for_with_ng_model(record, options = {}, &block)
14
+ if @options[:ngform]
15
+ @options[:fields_context] = fields_context(record, @options)
16
+ else
17
+ parent_builder = @options[:parent_builder]
18
+ until !parent_builder || parent_builder.options[:ngform]
19
+ parent_builder = parent_builder.options[:parent_builder]
20
+ end
21
+ if parent_builder && parent_builder.options[:ngform]
22
+ @options[:fields_context] = fields_context(record, @options[:parent_builder].options)
23
+ end
24
+ end
25
+
26
+ simple_fields_for_without_ng_model(record, options, &block)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ SimpleForm::FormBuilder.send :include, FormAngular::FieldSet::NgFields
@@ -0,0 +1,31 @@
1
+ module FormAngular
2
+ module Fields
3
+ module NgModelInput
4
+ include NgOptionUtils
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ alias_method_chain :input, :ng_model
9
+ end
10
+
11
+ def input_with_ng_model(attribute_name, options = {}, &block)
12
+ options[:input_html] ||= {}
13
+ if @options[:ngform]
14
+ options[:input_html].merge! build_angular_options(attribute_name, @options)
15
+ else
16
+ parent_builder = @options[:parent_builder]
17
+ until !parent_builder || parent_builder.options[:ngform]
18
+ parent_builder = parent_builder.options[:parent_builder]
19
+ end
20
+ if parent_builder && parent_builder.options[:ngform]
21
+ options[:input_html].merge! build_angular_options(attribute_name, @options[:parent_builder].options)
22
+ end
23
+ end
24
+
25
+ input_without_angular(attribute_name, options, &block)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ SimpleForm::FormBuilder.send :include, FormAngular::Fields::NgModelInput
@@ -0,0 +1,24 @@
1
+ module FormAngular
2
+ module Forms
3
+ module NgForm
4
+ extend ActiveSupport::Concern
5
+ include NgOptionUtils
6
+
7
+ included do
8
+ alias_method_chain :simple_form_for, :ng_model
9
+ end
10
+
11
+ def simple_form_for_with_ng_model(record, options = {}, &block)
12
+ if options[:ngform]
13
+ options[:html] ||= {}
14
+ options[:html] = build_angular_form_options(record).merge! options[:html]
15
+ options[:fields_context] = form_context(record)
16
+ end
17
+
18
+ simple_form_for_without_ng_model(record, options, &block)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ SimpleForm::ActionViewExtensions::FormHelper.send :include, FormAngular::Forms::NgForm
@@ -0,0 +1,68 @@
1
+ module FormAngular
2
+ module NgOptionUtils
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ def build_angular_form_options(record)
8
+ angular_options = {
9
+ name: "#{form_context(record)}Form",
10
+ novalidate: true
11
+ }
12
+ end
13
+
14
+ def form_context(record)
15
+ record.class.name.camelize(:lower)
16
+ end
17
+
18
+ def fields_context(record, options)
19
+ "#{options[:fields_context]}.#{context_name(record)}"
20
+ end
21
+
22
+ def build_angular_options(attribute_name, form_options)
23
+ angular_options = {
24
+ 'ng-model' => "#{form_options[:fields_context]}.#{attribute_name}",
25
+ 'server-error' => "true"
26
+ }
27
+ end
28
+
29
+ def context_name(record)
30
+ if record.is_a?(String) || record.is_a?(Symbol)
31
+ record
32
+ else
33
+ record.class.name.underscore
34
+ end
35
+ end
36
+
37
+ #Find a better way to make the form name trickledown globally in the wrapper api
38
+ def form_name
39
+ name = name_from_options(@builder.options)
40
+ return name unless name.blank?
41
+ builder = @builder
42
+ until !builder || !name.blank?
43
+ name = name_from_options(builder.options)
44
+ builder = builder.options[:parent_builder]
45
+ end
46
+ name
47
+ end
48
+
49
+ def name_from_options(from_options)
50
+ if from_options[:html]
51
+ from_options[:html][:name]
52
+ end
53
+ end
54
+
55
+ def ng_access_expression
56
+ "#{form_name}['#{tag_name}']"
57
+ end
58
+
59
+ def ng_form_expression
60
+ "#{form_name}"
61
+ end
62
+
63
+ def tag_name(multiple = false)
64
+ "#{object_name}[#{attribute_name}]"
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module FormAngular
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: form_angular
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Balaine
8
+ - Finexkap
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.3.0
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.3.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: AngularJS Rails Forms
43
+ email:
44
+ - igosuki@gmail.com
45
+ - dev@finexkap.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - form_angular.gemspec
55
+ - lib/form_angular.rb
56
+ - lib/form_angular/errors/ng_error.rb
57
+ - lib/form_angular/errors/ng_input_error.rb
58
+ - lib/form_angular/fields/ng_fieldset.rb
59
+ - lib/form_angular/fields/ng_model.rb
60
+ - lib/form_angular/forms/ng_form.rb
61
+ - lib/form_angular/helpers/ng_option_utils.rb
62
+ - lib/form_angular/version.rb
63
+ homepage: http://nexkap.github.io/form_angular
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: AngularJS Rails Forms
87
+ test_files: []