crazy_validators 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.travis.yml +4 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +21 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/crazy_validators.gemspec +87 -0
- data/lib/crazy_validators/.DS_Store +0 -0
- data/lib/crazy_validators/alpha_numericality_validator.rb +14 -0
- data/lib/crazy_validators/blacklist/default.yml +3 -0
- data/lib/crazy_validators/blacklist_validator.rb +65 -0
- data/lib/crazy_validators/boolean_validator.rb +10 -0
- data/lib/crazy_validators/o_embed_url_validator.rb +40 -0
- data/lib/crazy_validators/slug_validator.rb +15 -0
- data/lib/crazy_validators/word_count_validator.rb +34 -0
- data/lib/crazy_validators.rb +18 -0
- data/locales/en.yml +13 -0
- data/locales/fr.yml +13 -0
- data/test/fixtures/boolean_model.rb +4 -0
- data/test/fixtures/simple_model.rb +51 -0
- data/test/fixtures/string_model.rb +4 -0
- data/test/helper.rb +29 -0
- data/test/test_boolean_validator.rb +19 -0
- data/test/test_crazy_validators.rb +21 -0
- data/test/test_slug_validator.rb +16 -0
- data/test/test_word_count_validator.rb +0 -0
- metadata +206 -0
data/.document
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem 'activemodel'
|
4
|
+
gem 'activesupport'
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem "shoulda", ">= 0"
|
8
|
+
gem "bundler", "~> 1.1.0"
|
9
|
+
gem "jeweler", "~> 1.8.0"
|
10
|
+
gem 'simplecov', :require => false
|
11
|
+
gem 'to_slug'
|
12
|
+
gem "ruby-oembed"
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Cyril Picard
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= CrazyValidators
|
2
|
+
|
3
|
+
{<img src="https://secure.travis-ci.org/cyrilpic/crazy_validators.png?branch=master" alt="Build Status" />}[http://travis-ci.org/cyrilpic/crazy_validators]
|
4
|
+
|
5
|
+
Description goes here.
|
6
|
+
|
7
|
+
== Contributing to crazy_validators
|
8
|
+
|
9
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
10
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
11
|
+
* Fork the project
|
12
|
+
* Start a feature/bugfix branch
|
13
|
+
* Commit and push until you are happy with your contribution
|
14
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
15
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2011 Cyril Picard. See LICENSE.txt for
|
20
|
+
further details.
|
21
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "crazy_validators"
|
18
|
+
gem.homepage = "http://github.com/cyrilpic/crazy_validators"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{ Adds some validation methods for ActiveModel models }
|
21
|
+
gem.description = %Q{ CrazyValidators enables easy validation of attributs of an ActiveModel class, such as alphanumericality, presence in a Blacklist. }
|
22
|
+
gem.email = "Cyril@picard.ch"
|
23
|
+
gem.authors = ["Cyril Picard"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Run Test Unit with code coverage"
|
36
|
+
task :test_coverage do
|
37
|
+
ENV['COVERAGE'] = 'true'
|
38
|
+
Rake::Task["test"].execute
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rdoc/task'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "crazy_validators #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "crazy_validators"
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Cyril Picard"]
|
12
|
+
s.date = "2012-05-28"
|
13
|
+
s.description = " CrazyValidators enables easy validation of attributs of an ActiveModel class, such as alphanumericality, presence in a Blacklist. "
|
14
|
+
s.email = "Cyril@picard.ch"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".travis.yml",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"crazy_validators.gemspec",
|
28
|
+
"lib/crazy_validators.rb",
|
29
|
+
"lib/crazy_validators/.DS_Store",
|
30
|
+
"lib/crazy_validators/alpha_numericality_validator.rb",
|
31
|
+
"lib/crazy_validators/blacklist/default.yml",
|
32
|
+
"lib/crazy_validators/blacklist_validator.rb",
|
33
|
+
"lib/crazy_validators/boolean_validator.rb",
|
34
|
+
"lib/crazy_validators/o_embed_url_validator.rb",
|
35
|
+
"lib/crazy_validators/slug_validator.rb",
|
36
|
+
"lib/crazy_validators/word_count_validator.rb",
|
37
|
+
"locales/en.yml",
|
38
|
+
"locales/fr.yml",
|
39
|
+
"test/fixtures/boolean_model.rb",
|
40
|
+
"test/fixtures/simple_model.rb",
|
41
|
+
"test/fixtures/string_model.rb",
|
42
|
+
"test/helper.rb",
|
43
|
+
"test/test_boolean_validator.rb",
|
44
|
+
"test/test_crazy_validators.rb",
|
45
|
+
"test/test_slug_validator.rb",
|
46
|
+
"test/test_word_count_validator.rb"
|
47
|
+
]
|
48
|
+
s.homepage = "http://github.com/cyrilpic/crazy_validators"
|
49
|
+
s.licenses = ["MIT"]
|
50
|
+
s.require_paths = ["lib"]
|
51
|
+
s.rubygems_version = "1.8.19"
|
52
|
+
s.summary = "Adds some validation methods for ActiveModel models"
|
53
|
+
|
54
|
+
if s.respond_to? :specification_version then
|
55
|
+
s.specification_version = 3
|
56
|
+
|
57
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
58
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
59
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
61
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
|
62
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.0"])
|
63
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
64
|
+
s.add_development_dependency(%q<to_slug>, [">= 0"])
|
65
|
+
s.add_development_dependency(%q<ruby-oembed>, [">= 0"])
|
66
|
+
else
|
67
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
68
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
69
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
+
s.add_dependency(%q<bundler>, ["~> 1.1.0"])
|
71
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
|
72
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
73
|
+
s.add_dependency(%q<to_slug>, [">= 0"])
|
74
|
+
s.add_dependency(%q<ruby-oembed>, [">= 0"])
|
75
|
+
end
|
76
|
+
else
|
77
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
78
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
79
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
80
|
+
s.add_dependency(%q<bundler>, ["~> 1.1.0"])
|
81
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
|
82
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
83
|
+
s.add_dependency(%q<to_slug>, [">= 0"])
|
84
|
+
s.add_dependency(%q<ruby-oembed>, [">= 0"])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
Binary file
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module CrazyValidators
|
4
|
+
class AlphaNumericalityValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
|
7
|
+
return if value.nil?
|
8
|
+
|
9
|
+
unless value.to_s.match(/^\w+$/)
|
10
|
+
record.errors.add(attribute, :not_alpha_numeric, options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module CrazyValidators
|
4
|
+
class BlacklistValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
RESERVED_OPTIONS = [:with_array, :with_model, :with_file, :type]
|
7
|
+
|
8
|
+
DEFAULT_FILE = File.join(File.dirname(__FILE__), 'blacklist', 'default.yml')
|
9
|
+
DEFAULT_TYPE = :equals
|
10
|
+
|
11
|
+
def initialize(options)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_each(record, attribute, value)
|
16
|
+
@blacklist = load_blacklist
|
17
|
+
type = (options[:type] || DEFAULT_TYPE)
|
18
|
+
error_options = options.except(RESERVED_OPTIONS)
|
19
|
+
if type == :contains
|
20
|
+
@blacklist.each do |el|
|
21
|
+
if value.to_s.include? el
|
22
|
+
error_options[:word] = value.to_s
|
23
|
+
record.errors.add(attribute, :contains_blacklist, error_options)
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end
|
27
|
+
else
|
28
|
+
value.to_s.split(/'| /).each do |el|
|
29
|
+
if @blacklist.include? el
|
30
|
+
error_options[:word] = el.to_s
|
31
|
+
record.errors.add(attribute, :in_blacklist, error_options)
|
32
|
+
break
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
private
|
38
|
+
def load_blacklist()
|
39
|
+
if options.key? :with_array
|
40
|
+
raise ArgumentError, ":with_array must contain an array" unless options[:with_array].is_a?(Array)
|
41
|
+
return options[:with_array]
|
42
|
+
elsif options.key? :with_model
|
43
|
+
model = options[:with_model]
|
44
|
+
if model.is_a?(String) || model.is_a?(Symbol)
|
45
|
+
model = model.to_s.camelize.constantize
|
46
|
+
end
|
47
|
+
raise ArgumentError, ":with_model must be a model (does not respond to 'all')" unless model.respond_to? :all
|
48
|
+
array = []
|
49
|
+
list = model.all
|
50
|
+
list.each do |record|
|
51
|
+
array << record.name
|
52
|
+
end
|
53
|
+
return array
|
54
|
+
elsif options.key? :with_file
|
55
|
+
raise ArgumentError, ":with_file does not point to an existing file" unless File.exists?(options[:with_file])
|
56
|
+
hash = YAML::load_file(options[:with_file])
|
57
|
+
raise ArgumentError, ":with_file file does not contain an array called blacklist" unless hash.key?(:blacklist) && hash.is_a?(Array)
|
58
|
+
return hash["blacklist"]
|
59
|
+
else
|
60
|
+
hash = YAML::load_file(DEFAULT_FILE)
|
61
|
+
return hash["blacklist"]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
module CrazyValidators
|
3
|
+
class BooleanValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
unless value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
6
|
+
record.errors.add(attribute, :not_boolean, options)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'oembed'
|
3
|
+
module CrazyValidators
|
4
|
+
class OEmbedUrlValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
RESERVED_OPTIONS = [:type, :success]
|
7
|
+
AUTHORIZED_TYPES = ['video', 'link', 'photo', 'rich']
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_each(record, attribute, value)
|
14
|
+
error_options = options.except(RESERVED_OPTIONS)
|
15
|
+
type = [options[:type]].flatten.map(&:to_s).keep_if do |e|
|
16
|
+
AUTHORIZED_TYPES.include? e
|
17
|
+
end
|
18
|
+
if record.send(attribute.to_s + "_changed?")
|
19
|
+
OEmbed::Providers.register_all
|
20
|
+
begin
|
21
|
+
res = OEmbed::Providers.get(value)
|
22
|
+
if type.any? { |t| res.send(t+'?') }
|
23
|
+
unless options[:success].nil?
|
24
|
+
if options[:success].is_a? Proc
|
25
|
+
options[:success].call(res)
|
26
|
+
else
|
27
|
+
record.send(options[:success].to_sym, res)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
else
|
31
|
+
error_options[:required_type] = type.join(", ")
|
32
|
+
record.errors.add(attribute, :oembed_not_right_type, error_options)
|
33
|
+
end
|
34
|
+
rescue OEmbed::Error => e
|
35
|
+
record.errors.add(attribute, :oembed_error, error_options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'to_slug'
|
3
|
+
|
4
|
+
module CrazyValidators
|
5
|
+
class SlugValidator < ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
|
8
|
+
return if value.nil?
|
9
|
+
|
10
|
+
unless value.to_slug == value
|
11
|
+
record.errors.add(attribute, :not_a_slug, options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module CrazyValidators
|
4
|
+
class WordCountValidator < ActiveModel::Validations::LengthValidator
|
5
|
+
MESSAGES = { :is => :word_wrong_length, :minimum => :word_too_short, :maximum => :word_too_long }.freeze
|
6
|
+
|
7
|
+
# Copy of ActiveModel::Validations::LengthValidator
|
8
|
+
def validate_each(record, attribute, value)
|
9
|
+
value = word_tokenizer(value) if value.kind_of?(String)
|
10
|
+
|
11
|
+
CHECKS.each do |key, validity_check|
|
12
|
+
next unless check_value = options[key]
|
13
|
+
|
14
|
+
value ||= [] if key == :maximum
|
15
|
+
|
16
|
+
value_length = value.respond_to?(:length) ? value.length : value.to_s.length
|
17
|
+
next if value_length.send(validity_check, check_value)
|
18
|
+
|
19
|
+
errors_options = options.except(*RESERVED_OPTIONS)
|
20
|
+
errors_options[:count] = check_value
|
21
|
+
|
22
|
+
default_message = options[MESSAGES[key]]
|
23
|
+
errors_options[:message] ||= default_message if default_message
|
24
|
+
|
25
|
+
record.errors.add(attribute, MESSAGES[key], errors_options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def word_tokenizer(value)
|
31
|
+
value.split(' ')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module CrazyValidators
|
4
|
+
extend ActiveSupport::Autoload
|
5
|
+
|
6
|
+
autoload :SlugValidator
|
7
|
+
autoload :OEmbedUrlValidator
|
8
|
+
end
|
9
|
+
require 'crazy_validators/boolean_validator'
|
10
|
+
require 'crazy_validators/blacklist_validator'
|
11
|
+
require 'crazy_validators/alpha_numericality_validator'
|
12
|
+
require 'crazy_validators/word_count_validator'
|
13
|
+
|
14
|
+
|
15
|
+
require 'active_support/i18n'
|
16
|
+
I18n.load_path += Dir[File.expand_path(File.join(File.dirname(__FILE__), '../locales', '*.yml')).to_s]
|
17
|
+
|
18
|
+
ActiveModel::Validations.__send__(:include, CrazyValidators)
|
data/locales/en.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
not_alpha_numeric: "can only contain alphanumeric characters"
|
5
|
+
not_a_slug: "contains illegal caracters"
|
6
|
+
word_too_long: "is too long (maximum is %{count} words)"
|
7
|
+
word_too_short: "is too short (minimum is %{count} words)"
|
8
|
+
word_wrong_length: "is the wrong length (should be %{count} words)"
|
9
|
+
in_blacklist: "is a reserved name (%{word} is not allowed)"
|
10
|
+
contains_blacklist: "is a reserved name (%{word} is not allowed)"
|
11
|
+
oembed_error: "could not be embedded"
|
12
|
+
oembed_not_right_type: "is not of the correct type (allowed is : %{required_types})"
|
13
|
+
not_boolean: "should be yes or no"
|
data/locales/fr.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
fr:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
not_alpha_numeric: "ne peut contenir que des caractères alpha-numérique"
|
5
|
+
not_a_slug: "contient des caractères interdits"
|
6
|
+
word_too_long: "est trop long (maximum %{count} mots)"
|
7
|
+
word_too_short: "est trop court (minimum %{count} mots)"
|
8
|
+
word_wrong_length: "n'a pas la bonne longueur (devrait être de %{count} mots)"
|
9
|
+
in_blacklist: "est un nom réservé (%{word} n'est pas autorisé)"
|
10
|
+
contains_blacklist: "est un nom réservé (%{word} n'est pas autorisé)"
|
11
|
+
oembed_error: "n'a pu être intégré"
|
12
|
+
oembed_not_right_type: "ne correspond pas au format attendu (autorisé : %{required_format})"
|
13
|
+
not_boolean: "devrait être oui ou non"
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class SimpleModel
|
2
|
+
include ActiveModel::Conversion
|
3
|
+
extend ActiveModel::Naming
|
4
|
+
extend ActiveModel::Translation
|
5
|
+
include ActiveModel::Validations
|
6
|
+
include ActiveModel::AttributeMethods
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each do |attr, value|
|
10
|
+
self.send("#{attr}=", value)
|
11
|
+
end unless attributes.blank?
|
12
|
+
end
|
13
|
+
|
14
|
+
def persisted?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
# 1) Define a class inheritable attribute named _attributes
|
19
|
+
# Let's use underscore to mark this method as internal to our gem
|
20
|
+
class_attribute :_attributes
|
21
|
+
self._attributes = []
|
22
|
+
|
23
|
+
attribute_method_prefix 'clear_'
|
24
|
+
attribute_method_suffix '?'
|
25
|
+
|
26
|
+
def self.attributes(*names)
|
27
|
+
attr_accessor *names
|
28
|
+
define_attribute_methods names
|
29
|
+
|
30
|
+
# 2) Add declared attributes to the list
|
31
|
+
self._attributes += names
|
32
|
+
end
|
33
|
+
|
34
|
+
# 3) Create the attributes hash by iterating the list
|
35
|
+
def attributes
|
36
|
+
self._attributes.inject({}) do |hash, attr|
|
37
|
+
hash[attr.to_s] = send(attr)
|
38
|
+
hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def clear_attribute(attribute)
|
45
|
+
send("#{attribute}=", nil)
|
46
|
+
end
|
47
|
+
|
48
|
+
def attribute?(attribute)
|
49
|
+
send(attribute).present?
|
50
|
+
end
|
51
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
if ENV["COVERAGE"]
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter '/gems/'
|
6
|
+
add_filter '/test/'
|
7
|
+
add_filter '/spec/'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'bundler'
|
13
|
+
begin
|
14
|
+
Bundler.setup(:default, :development)
|
15
|
+
rescue Bundler::BundlerError => e
|
16
|
+
$stderr.puts e.message
|
17
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
18
|
+
exit e.status_code
|
19
|
+
end
|
20
|
+
require 'test/unit'
|
21
|
+
require 'shoulda'
|
22
|
+
require 'active_model'
|
23
|
+
|
24
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
25
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
26
|
+
require 'crazy_validators'
|
27
|
+
|
28
|
+
class Test::Unit::TestCase
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'fixtures/boolean_model'
|
3
|
+
|
4
|
+
class TestBooleanValidator < Test::Unit::TestCase
|
5
|
+
include ActiveModel::Lint::Tests
|
6
|
+
def setup
|
7
|
+
BooleanModel.validates :boolean, boolean: true
|
8
|
+
@model = BooleanModel.new
|
9
|
+
end
|
10
|
+
should "only accept boolean values" do
|
11
|
+
model.boolean = false
|
12
|
+
assert model.valid?
|
13
|
+
model.boolean = true
|
14
|
+
assert model.valid?
|
15
|
+
model.boolean = nil
|
16
|
+
assert !model.valid?
|
17
|
+
assert_equal model.errors[:boolean], ["should be yes or no"]
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'fixtures/simple_model'
|
3
|
+
|
4
|
+
class TestCrazyValidators < Test::Unit::TestCase
|
5
|
+
should "be included in active_model validators" do
|
6
|
+
assert ActiveModel::Validations::constants.include?(:AlphaNumericalityValidator)
|
7
|
+
assert ActiveModel::Validations::constants.include?(:BlacklistValidator)
|
8
|
+
assert ActiveModel::Validations::constants.include?(:BooleanValidator)
|
9
|
+
assert ActiveModel::Validations::constants.include?(:OEmbedUrlValidator)
|
10
|
+
assert ActiveModel::Validations::constants.include?(:SlugValidator)
|
11
|
+
assert ActiveModel::Validations::constants.include?(:WordCountValidator)
|
12
|
+
end
|
13
|
+
should "be available in model" do
|
14
|
+
assert SimpleModel.constants.include?(:AlphaNumericalityValidator)
|
15
|
+
assert SimpleModel.constants.include?(:BlacklistValidator)
|
16
|
+
assert SimpleModel.constants.include?(:BooleanValidator)
|
17
|
+
assert SimpleModel.constants.include?(:OEmbedUrlValidator)
|
18
|
+
assert SimpleModel.constants.include?(:SlugValidator)
|
19
|
+
assert SimpleModel.constants.include?(:WordCountValidator)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'fixtures/string_model'
|
3
|
+
|
4
|
+
class TestSlugValidator < Test::Unit::TestCase
|
5
|
+
include ActiveModel::Lint::Tests
|
6
|
+
def setup
|
7
|
+
StringModel.validates :string, slug: true
|
8
|
+
@model = StringModel.new
|
9
|
+
end
|
10
|
+
should "only accept valid slug" do
|
11
|
+
model.string = "This is @ regular sentance !"
|
12
|
+
assert !model.valid?, "Provided a sentance instead of a slug"
|
13
|
+
model.string = "This is @ regular sentance !".to_slug
|
14
|
+
assert model.valid?
|
15
|
+
end
|
16
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crazy_validators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cyril Picard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activesupport
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: shoulda
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.1.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.1.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: jeweler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.8.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.8.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: to_slug
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: ruby-oembed
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: ! ' CrazyValidators enables easy validation of attributs of an ActiveModel
|
143
|
+
class, such as alphanumericality, presence in a Blacklist. '
|
144
|
+
email: Cyril@picard.ch
|
145
|
+
executables: []
|
146
|
+
extensions: []
|
147
|
+
extra_rdoc_files:
|
148
|
+
- LICENSE.txt
|
149
|
+
- README.rdoc
|
150
|
+
files:
|
151
|
+
- .document
|
152
|
+
- .travis.yml
|
153
|
+
- Gemfile
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.rdoc
|
156
|
+
- Rakefile
|
157
|
+
- VERSION
|
158
|
+
- crazy_validators.gemspec
|
159
|
+
- lib/crazy_validators.rb
|
160
|
+
- lib/crazy_validators/.DS_Store
|
161
|
+
- lib/crazy_validators/alpha_numericality_validator.rb
|
162
|
+
- lib/crazy_validators/blacklist/default.yml
|
163
|
+
- lib/crazy_validators/blacklist_validator.rb
|
164
|
+
- lib/crazy_validators/boolean_validator.rb
|
165
|
+
- lib/crazy_validators/o_embed_url_validator.rb
|
166
|
+
- lib/crazy_validators/slug_validator.rb
|
167
|
+
- lib/crazy_validators/word_count_validator.rb
|
168
|
+
- locales/en.yml
|
169
|
+
- locales/fr.yml
|
170
|
+
- test/fixtures/boolean_model.rb
|
171
|
+
- test/fixtures/simple_model.rb
|
172
|
+
- test/fixtures/string_model.rb
|
173
|
+
- test/helper.rb
|
174
|
+
- test/test_boolean_validator.rb
|
175
|
+
- test/test_crazy_validators.rb
|
176
|
+
- test/test_slug_validator.rb
|
177
|
+
- test/test_word_count_validator.rb
|
178
|
+
homepage: http://github.com/cyrilpic/crazy_validators
|
179
|
+
licenses:
|
180
|
+
- MIT
|
181
|
+
post_install_message:
|
182
|
+
rdoc_options: []
|
183
|
+
require_paths:
|
184
|
+
- lib
|
185
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
186
|
+
none: false
|
187
|
+
requirements:
|
188
|
+
- - ! '>='
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
segments:
|
192
|
+
- 0
|
193
|
+
hash: 2631497578937373577
|
194
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
+
none: false
|
196
|
+
requirements:
|
197
|
+
- - ! '>='
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: '0'
|
200
|
+
requirements: []
|
201
|
+
rubyforge_project:
|
202
|
+
rubygems_version: 1.8.19
|
203
|
+
signing_key:
|
204
|
+
specification_version: 3
|
205
|
+
summary: Adds some validation methods for ActiveModel models
|
206
|
+
test_files: []
|