validation_annotations 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ require 'validation_annotations/version'
2
+ require 'validation_annotations/validations'
3
+ require 'validation_annotations/form_builder'
4
+
5
+ module ValidationAnnotations
6
+ # Your code goes here...
7
+ end
8
+
9
+ require 'active_support'
10
+ ActiveSupport.on_load(:action_view) do
11
+ ActionView::Base.default_form_builder = ValidationAnnotations::FormBuilder
12
+ end
@@ -0,0 +1,78 @@
1
+ require 'action_view'
2
+
3
+ module ValidationAnnotations
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+
6
+ def initialize(object_name, object, template, options, proc)
7
+ puts options.inspect
8
+ @skip_annotations = !options[:validation_annotations].nil? && options[:validation_annotations] === false
9
+ super(object_name, object, template, options, proc)
10
+ end
11
+
12
+ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
13
+ options = setup_validation_options(method, options)
14
+ super(method, options, checked_value, unchecked_value)
15
+ end
16
+
17
+ def password_field(method, options = {})
18
+ options = setup_validation_options(method, options)
19
+ super(method, options)
20
+ end
21
+
22
+ def text_area(method, options = {})
23
+ options = setup_validation_options(method, options)
24
+ super(method, options)
25
+ end
26
+
27
+ def text_field(method, options = {})
28
+ options = setup_validation_options(method, options)
29
+ super(method, options)
30
+ end
31
+
32
+ def radio_buttons(method, options, &block)
33
+ options = setup_validation_options(method, options)
34
+ options[:class].push("radio") if options[:class].is_a?(Array)
35
+
36
+ @template.content_tag(:div, options, &block)
37
+ end
38
+
39
+ def check_boxes(method, options, &block)
40
+ options = setup_validation_options(method, options)
41
+ options[:class].push("checkboxes") if options[:class].is_a?(Array)
42
+
43
+ @template.content_tag(:div, options, &block)
44
+ end
45
+
46
+ protected
47
+
48
+ def setup_validation_options(attribute, options)
49
+ return options if @skip_annotations
50
+
51
+ validators = @object.class.validators_on(attribute) # the list of all validators assigned to attribute
52
+ validators.each do |validator|
53
+ next if skip_validation?(@object, validator.options)
54
+
55
+ if validator.respond_to?(:add_html_annotations!)
56
+ validator.add_html_annotations!(options)
57
+ end
58
+ end
59
+
60
+ options.delete(:validate)
61
+ options
62
+ end
63
+
64
+ def skip_validation?(object, voptions)
65
+ if voptions[:if].present?
66
+ return true if voptions[:if].is_a?(Proc) && voptions[:if].call(object) == false
67
+ return true if voptions[:if].is_a?(Symbol) && object.send(voptions[:if]) == false
68
+ end
69
+
70
+ if voptions[:unless].present?
71
+ return true if voptions[:unless].is_a?(Proc) && voptions[:unless].call(object) == true
72
+ return true if voptions[:unless].is_a?(Symbol) && object.send(voptions[:unless]) == true
73
+ end
74
+
75
+ false
76
+ end
77
+ end
78
+ end
@@ -0,0 +1 @@
1
+ require 'validation_annotations/validations/presence'
@@ -0,0 +1,17 @@
1
+ module ValidationAnnotations
2
+ module Validations
3
+ module Presence
4
+ def add_html_annotations!(options)
5
+ options['required'] = 'required'
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ module ActiveModel
12
+ module Validations
13
+ class PresenceValidator
14
+ include ValidationAnnotations::Validations::Presence
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ValidationAnnotations
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/validation_annotations/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Getty Images", "Andy Alm"]
6
+ gem.email = ["opensourcereview@gettyimages.com"]
7
+ gem.description = %q{Provides an extensible hook for adding custom html attributes to input elements based on validation rules defined in your model.}
8
+ gem.summary = gem.description
9
+ gem.homepage = "http://github.com/gettyimages/validation_annotations"
10
+
11
+ gem.files = Dir['lib/**/*.*'] + Dir['*.gemspec']
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "validation_annotations"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ValidationAnnotations::VERSION
17
+
18
+ gem.add_dependency 'activemodel', '~> 3.2'
19
+ gem.add_dependency 'actionpack', '~> 3.2'
20
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validation_annotations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Getty Images
9
+ - Andy Alm
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-04-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activemodel
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '3.2'
31
+ - !ruby/object:Gem::Dependency
32
+ name: actionpack
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '3.2'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.2'
47
+ description: Provides an extensible hook for adding custom html attributes to input
48
+ elements based on validation rules defined in your model.
49
+ email:
50
+ - opensourcereview@gettyimages.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - lib/validation_annotations/form_builder.rb
56
+ - lib/validation_annotations/validations/presence.rb
57
+ - lib/validation_annotations/validations.rb
58
+ - lib/validation_annotations/version.rb
59
+ - lib/validation_annotations.rb
60
+ - validation_annotations.gemspec
61
+ homepage: http://github.com/gettyimages/validation_annotations
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Provides an extensible hook for adding custom html attributes to input elements
85
+ based on validation rules defined in your model.
86
+ test_files: []