custom_validations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "custom_validations/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Kauplus Social Commerce"]
7
+ gem.email = ["suporte@kauplus.com.br"]
8
+ gem.description = %q{Collection of custom validations for Rails}
9
+ gem.summary = %q{Adds custom validations code for ActiveModel and javascript code for ClientSideValidations (https://github.com/bcardarella/client_side_validations)}
10
+ gem.homepage = "https://github.com/kauplus/custom_validations"
11
+
12
+ gem.files = `git ls-files -- {lib/*,vendor/*,*.gemspec}`.split("\n")
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "custom_validations"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = CustomValidations::VERSION
18
+
19
+
20
+ #
21
+ # Dependencies
22
+ #
23
+ gem.add_development_dependency 'rails', '>= 3.0'
24
+ gem.add_development_dependency 'rspec'
25
+
26
+ end
@@ -0,0 +1,11 @@
1
+ # Require dependencies used by many classes of this Gem
2
+ require 'active_model'
3
+
4
+ # Require base code
5
+ require 'custom_validations/version'
6
+ require 'custom_validations/engine'
7
+ require 'custom_validations/email_validator'
8
+
9
+ # Load translations
10
+ require 'active_support/i18n'
11
+ I18n.load_path << File.dirname(__FILE__) + '/custom_validations/locale/en.yml'
@@ -0,0 +1,30 @@
1
+ module ActiveModel
2
+ module Validations
3
+
4
+
5
+ class EmailValidator < ActiveModel::EachValidator
6
+
7
+ REGEX = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
8
+
9
+ def validate_each(record, attribute, value)
10
+ return if value.to_s.blank? # it doesn't validate presence
11
+ unless value.to_s =~ REGEX
12
+ record.errors.add(attribute, :email, options)
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ module HelperMethods
19
+ def validates_email_of(*attr_names)
20
+ validates_with EmailValidator, _merge_attributes(attr_names)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ #
28
+ # REGEX taken from:
29
+ # http://guides.rubyonrails.org/v3.1.3/active_record_validations_callbacks.html#custom-validators
30
+ #
@@ -0,0 +1,11 @@
1
+ if defined?(Rails)
2
+
3
+ module CustomValidations
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ # Rails should look in our vendor directory.
7
+ end
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ errors:
3
+ # The values :model, :attribute and :value are always available for interpolation
4
+ # The value :count is available when applicable. Can be used for pluralization.
5
+ messages:
6
+ email: "not an email address"
@@ -0,0 +1,3 @@
1
+ module CustomValidations
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+
2
+ /*
3
+ # Rails 3 Custom Validations - v0.0.1
4
+ # https://github.com/kauplus/custom_validations
5
+ #
6
+ # Copyright (c) 2013 Kauplus
7
+ # Licensed under the MIT license
8
+ # http://www.opensource.org/licenses/mit-license.php
9
+ */
10
+
11
+
12
+ (function() {
13
+
14
+ jQuery(function() {
15
+ var is_blank, validators;
16
+ is_blank = function(element) {
17
+ return /^\s*$/.test(element.val() || '');
18
+ };
19
+ validators = window.ClientSideValidations.validators;
20
+ return validators.local['email'] = function(element, options) {
21
+ if (is_blank(element)) {
22
+ return;
23
+ }
24
+ if (!/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test(element.val())) {
25
+ return options.message;
26
+ }
27
+ };
28
+ });
29
+
30
+ }).call(this);
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kauplus Social Commerce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Collection of custom validations for Rails
47
+ email:
48
+ - suporte@kauplus.com.br
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - custom_validations.gemspec
54
+ - lib/custom_validations.rb
55
+ - lib/custom_validations/email_validator.rb
56
+ - lib/custom_validations/engine.rb
57
+ - lib/custom_validations/locale/en.yml
58
+ - lib/custom_validations/version.rb
59
+ - vendor/assets/javascripts/rails.custom_validations.js
60
+ homepage: https://github.com/kauplus/custom_validations
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ segments:
73
+ - 0
74
+ hash: -2993300514968256952
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
82
+ - 0
83
+ hash: -2993300514968256952
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Adds custom validations code for ActiveModel and javascript code for ClientSideValidations
90
+ (https://github.com/bcardarella/client_side_validations)
91
+ test_files: []