evelpidon_validators 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,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ .rvmrc
5
+ pkg/*
6
+ .idea
7
+ *.iml
@@ -0,0 +1,8 @@
1
+ # Evelpidon Validators changelog
2
+
3
+ ## 0.1.0 / 2011-11-01
4
+
5
+ * Birthday with support for the following validations :
6
+ * Different (than)
7
+ * Greater (than)
8
+ * Less (than)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evelpidon_validators.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyrignt (c) 2011 E-Travel S.A.
2
+ Copyrignt (c) 2011 Fraudpointer Ltd.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # Evelpidon Validators (for Rails / ActiveModel / ActiveRecord)
2
+
3
+ Collection of various ActiveModel validators, alongside their client side implementation
4
+ as ClientSideValidations::Middleware (see [ClientSideValidations](https://github.com/bcardarella/client_side_validations)
5
+ for more info). Client side validations are optional and are enabled only if the client_side_validations gem is
6
+ already loaded.
7
+
8
+ ## Validators
9
+
10
+ * Different
11
+ * Less
12
+ * More
13
+
14
+ ## Installation
15
+
16
+ ### Bundler
17
+
18
+ Add on your Gemfile :
19
+
20
+ ```ruby
21
+ gem 'evelpidon_validators'
22
+ ```
23
+
24
+ ### By hand
25
+
26
+ On the console :
27
+
28
+ ```bash
29
+ gem install evelpidon_validators
30
+ ```
31
+
32
+ On your code :
33
+
34
+ ```ruby
35
+ require 'evelpidon_validators'
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ Validators are *not* automatically loaded. You can require the validators you need either explicitly one-by-one
41
+ or all of them. So for example :
42
+
43
+ ```ruby
44
+ # Load only "Greater than" and "Less than" validators :
45
+ require 'evelpidon_validators/greater'
46
+ require 'evelpidon_validators/greater'
47
+ ```
48
+
49
+ or
50
+
51
+ ```ruby
52
+ # Load everything
53
+ require 'evelpidon_validators/all'
54
+ ```
55
+
56
+ ### Enable client side validations support
57
+
58
+ Client-side validations work out of the box with Rails 3.1 (through the asset pipeline) :
59
+
60
+ - Configure / initialize ClientSideValidations (gem, js, etc...) per it's documentation.
61
+ - Require the 'evelpidon_validators' js. This will be served through the asset pipeline
62
+
63
+ So for example the following should work :
64
+
65
+ ```
66
+ //= require rails.validations
67
+ //= require evelpidon_validators
68
+ ```
69
+
70
+ ## TODOs
71
+
72
+ * Better documentation
73
+ * Gather other useful validations found around the net...
74
+
75
+ ## Note on Patches/Pull Requests
76
+
77
+ * Fork the project.
78
+ * Make your feature addition or bug fix.
79
+ * Add tests for it. This is important so I don't break it in a
80
+ future version unintentionally (not really...).
81
+ * Commit, do not mess with gemspec, version, or history.
82
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
83
+ * Send a pull request. Bonus points for topic branches.
84
+
85
+ ## Author(s)
86
+
87
+ * [Nikos Dimitrakopoulos](http://github.com/nikosd)
88
+ * [Eric Cohen](http://github.com/eirc)
89
+
90
+ ## Copyright
91
+
92
+ * Copyrignt (c) 2011 [E-Travel S.A.](http://www.airtickets24.com)
93
+ * Copyrignt (c) 2011 [Fraudpointer.com](http://www.fraudpointer.com)
94
+
95
+ ## License
96
+
97
+ Evelpidon Validators are released under the MIT license.
98
+ See [LICENSE](/e-travel/evelpidon_validators/blob/master/LICENSE) for more details.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "evelpidon_validators/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "evelpidon_validators"
7
+ s.version = EvelpidonValidators::VERSION
8
+ s.authors = ["Nikos Dimitrakopoulos", "Eric Cohen"]
9
+ s.email = ["n.dimitrakopoulos@pamediakopes.gr", "e.koen@pamediakopes.gr"]
10
+ s.homepage = ""
11
+ s.summary = %q{Useful ActiveModel validators}
12
+ s.description = %q{Useful ActiveModel validators with ClientSideValidations support.}
13
+
14
+ s.rubyforge_project = "evelpidon_validators"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "activesupport"
22
+ s.add_development_dependency "evelpidon_test_helpers"
23
+
24
+ s.add_runtime_dependency "activemodel"
25
+ end
@@ -0,0 +1,9 @@
1
+ //= require_self
2
+ //= require_tree ./evelpidon_validators
3
+
4
+ clientSideValidations.helpers = clientSideValidations.helpers || {};
5
+
6
+ clientSideValidations.helpers.getSiblingField = function(field, siblingName) {
7
+ var thanName = field.attr('name').replace(/\[[^\]]*\]/, '[' + siblingName + ']');
8
+ return field.parents('form').find("input[name='" + thanName + "']");
9
+ };
@@ -0,0 +1,7 @@
1
+ clientSideValidations.validators.remote['different'] = function(element, options) {
2
+ if ($.ajax({
3
+ url: '/validators/different.json',
4
+ data: { value: element.val(), than: clientSideValidations.helpers.getSiblingField(element, options.than).val() },
5
+ async: false
6
+ }).status == 404) { return options.message; }
7
+ };
@@ -0,0 +1,7 @@
1
+ clientSideValidations.validators.remote['greater'] = function(element, options) {
2
+ if ($.ajax({
3
+ url: '/validators/greater.json',
4
+ data: { value: element.val(), than: clientSideValidations.helpers.getSiblingField(element, options.than).val() },
5
+ async: false
6
+ }).status == 404) { return options.message; }
7
+ };
@@ -0,0 +1,7 @@
1
+ clientSideValidations.validators.remote['less'] = function(element, options) {
2
+ if ($.ajax({
3
+ url: '/validators/less.json',
4
+ data: { value: element.val(), than: clientSideValidations.helpers.getSiblingField(element, options.than).val() },
5
+ async: false
6
+ }).status == 404) { return options.message; }
7
+ };
@@ -0,0 +1,6 @@
1
+ require "evelpidon_validators/version"
2
+ require 'active_model'
3
+ require "evelpidon_validators/rails"
4
+
5
+ module EvelpidonValidators
6
+ end
@@ -0,0 +1,3 @@
1
+ require "evelpidon_validators/different"
2
+ require "evelpidon_validators/greater"
3
+ require "evelpidon_validators/less"
@@ -0,0 +1,24 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class DifferentValidator < EachValidator
4
+ def self.different?(value, than)
5
+ not value.eql? than
6
+ end
7
+
8
+ def validate_each(record, attribute, value)
9
+ unless self.class.different? value, record.send(options[:than])
10
+ record.errors.add(attribute, :different, options)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClientSideValidations::Middleware
18
+ class Different < Base
19
+ def response
20
+ self.status = ActiveModel::Validations::DifferentValidator.different?(request.params[:value], request.params[:than]) ? 200 : 404
21
+ super
22
+ end
23
+ end
24
+ end if defined?(ClientSideValidations)
@@ -0,0 +1,24 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class GreaterValidator < EachValidator
4
+ def self.greater?(value, than)
5
+ value.present? and than.present? and value > than
6
+ end
7
+
8
+ def validate_each(record, attribute, value)
9
+ unless self.class.greater? value, record.send(options[:than])
10
+ record.errors.add(attribute, :greater, options)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClientSideValidations::Middleware
18
+ class Greater < Base
19
+ def response
20
+ self.status = ActiveModel::Validations::GreaterValidator.greater?(request.params[:value], request.params[:than]) ? 200 : 404
21
+ super
22
+ end
23
+ end
24
+ end if defined?(ClientSideValidations)
@@ -0,0 +1,24 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class LessValidator < EachValidator
4
+ def self.less?(value, than)
5
+ value.present? and than.present? and value < than
6
+ end
7
+
8
+ def validate_each(record, attribute, value)
9
+ unless self.class.less? value, record.send(options[:than])
10
+ record.errors.add(attribute, :less, options)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module ClientSideValidations::Middleware
18
+ class Less < Base
19
+ def response
20
+ self.status = ActiveModel::Validations::LessValidator.less?(request.params[:value], request.params[:than]) ? 200 : 404
21
+ super
22
+ end
23
+ end
24
+ end if defined?(ClientSideValidations)
@@ -0,0 +1,6 @@
1
+ module EvelpidonValidators
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end if defined?(Rails)
@@ -0,0 +1,3 @@
1
+ module EvelpidonValidators
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+ require 'evelpidon_validators/different'
3
+
4
+ class DifferentValidatorTest < ActiveSupport::TestCase
5
+
6
+ class ModelWithDifferentValidation
7
+ include ActiveModel::Validations
8
+ attr_accessor :attr1, :attr2
9
+ validates :attr1, :different => { :than => :attr2 }
10
+ end
11
+
12
+ setup do
13
+ @model = ModelWithDifferentValidation.new
14
+ end
15
+
16
+ test "different integers" do
17
+ @model.attr2 = 2
18
+ assert_valid_attr1 1
19
+ assert_valid_attr1 3
20
+ assert_invalid_attr1 2
21
+ end
22
+
23
+ test "different dates" do
24
+ today = Date.today
25
+ @model.attr2 = today
26
+ assert_valid_attr1 today - 1
27
+ assert_valid_attr1 today + 1
28
+ assert_invalid_attr1 today
29
+ end
30
+
31
+ test "different strings" do
32
+ @model.attr2 = 'a string'
33
+ assert_valid_attr1 'another string'
34
+ assert_valid_attr1 'a third string'
35
+ assert_invalid_attr1 'a string'
36
+ end
37
+
38
+ #########
39
+ protected
40
+ #########
41
+
42
+ def assert_valid_attr1(value)
43
+ @model.attr1 = value
44
+ assert_valid_attribute(@model, :attr1)
45
+ end
46
+
47
+ def assert_invalid_attr1(value)
48
+ @model.attr1 = value
49
+ assert_invalid_attribute @model, :attr1
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+ require 'evelpidon_validators/greater'
3
+
4
+ class GreaterValidatorTest < ActiveSupport::TestCase
5
+
6
+ class ModelWithGreaterValidation
7
+ include ActiveModel::Validations
8
+ attr_accessor :attr1, :attr2
9
+ validates :attr1, :greater => { :than => :attr2 }
10
+ end
11
+
12
+ setup do
13
+ @model = ModelWithGreaterValidation.new
14
+ end
15
+
16
+ test "greater integer" do
17
+ @model.attr2 = 2
18
+ assert_valid_attr1 3
19
+ assert_valid_attr1 4
20
+ assert_invalid_attr1 1
21
+ assert_invalid_attr1 2
22
+ end
23
+
24
+ test "greater date" do
25
+ today = Date.today
26
+ @model.attr2 = today
27
+ assert_valid_attr1 today + 1
28
+ assert_valid_attr1 today + 2
29
+ assert_invalid_attr1 today - 1
30
+ assert_invalid_attr1 today
31
+ end
32
+
33
+ test "greater string" do
34
+ @model.attr2 = 'c'
35
+ assert_valid_attr1 'd'
36
+ assert_valid_attr1 'e'
37
+ assert_invalid_attr1 'b'
38
+ assert_invalid_attr1 'c'
39
+ end
40
+
41
+ #########
42
+ protected
43
+ #########
44
+
45
+ def assert_valid_attr1(value)
46
+ @model.attr1 = value
47
+ assert_valid_attribute(@model, :attr1)
48
+ end
49
+
50
+ def assert_invalid_attr1(value)
51
+ @model.attr1 = value
52
+ assert_invalid_attribute @model, :attr1
53
+ end
54
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+ require 'evelpidon_validators/less'
3
+
4
+ class LessValidatorTest < ActiveSupport::TestCase
5
+
6
+ class ModelWithLessValidation
7
+ include ActiveModel::Validations
8
+ attr_accessor :attr1, :attr2
9
+ validates :attr1, :less => { :than => :attr2 }
10
+ end
11
+
12
+ setup do
13
+ @model = ModelWithLessValidation.new
14
+ end
15
+
16
+ test "less integer" do
17
+ @model.attr2 = 2
18
+ assert_valid_attr1 0
19
+ assert_valid_attr1 1
20
+ assert_invalid_attr1 2
21
+ assert_invalid_attr1 3
22
+ end
23
+
24
+ test "less date" do
25
+ today = Date.today
26
+ @model.attr2 = today
27
+ assert_valid_attr1 today - 2
28
+ assert_valid_attr1 today - 1
29
+ assert_invalid_attr1 today
30
+ assert_invalid_attr1 today + 1
31
+ end
32
+
33
+ test "less string" do
34
+ @model.attr2 = 'c'
35
+ assert_valid_attr1 'a'
36
+ assert_valid_attr1 'b'
37
+ assert_invalid_attr1 'c'
38
+ assert_invalid_attr1 'd'
39
+ end
40
+
41
+ #########
42
+ protected
43
+ #########
44
+
45
+ def assert_valid_attr1(value)
46
+ @model.attr1 = value
47
+ assert_valid_attribute(@model, :attr1)
48
+ end
49
+
50
+ def assert_invalid_attr1(value)
51
+ @model.attr1 = value
52
+ assert_invalid_attribute @model, :attr1
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+
3
+ require 'evelpidon_validators'
4
+
5
+ require 'test/unit'
6
+ require 'active_support/test_case'
7
+ require 'evelpidon_test_helpers'
8
+
9
+
10
+
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evelpidon_validators
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
+ - Nikos Dimitrakopoulos
14
+ - Eric Cohen
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-11-01 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: activesupport
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: evelpidon_test_helpers
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: activemodel
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :runtime
63
+ version_requirements: *id003
64
+ description: Useful ActiveModel validators with ClientSideValidations support.
65
+ email:
66
+ - n.dimitrakopoulos@pamediakopes.gr
67
+ - e.koen@pamediakopes.gr
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - CHANGELOG.md
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - evelpidon_validators.gemspec
82
+ - lib/assets/javascripts/evelpidon_validators.js
83
+ - lib/assets/javascripts/evelpidon_validators/different.js
84
+ - lib/assets/javascripts/evelpidon_validators/greater.js
85
+ - lib/assets/javascripts/evelpidon_validators/less.js
86
+ - lib/evelpidon_validators.rb
87
+ - lib/evelpidon_validators/all.rb
88
+ - lib/evelpidon_validators/different.rb
89
+ - lib/evelpidon_validators/greater.rb
90
+ - lib/evelpidon_validators/less.rb
91
+ - lib/evelpidon_validators/rails.rb
92
+ - lib/evelpidon_validators/version.rb
93
+ - test/different_test.rb
94
+ - test/greater_test.rb
95
+ - test/less_test.rb
96
+ - test/test_helper.rb
97
+ has_rdoc: true
98
+ homepage: ""
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project: evelpidon_validators
127
+ rubygems_version: 1.6.2
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Useful ActiveModel validators
131
+ test_files:
132
+ - test/different_test.rb
133
+ - test/greater_test.rb
134
+ - test/less_test.rb
135
+ - test/test_helper.rb