format_for_extensions 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +14 -0
- data/Gemfile.lock +42 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +86 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/config/format_for_extensions.yml +9 -0
- data/format_for_extensions.gemspec +69 -0
- data/lib/format_for_extensions/config.rb +24 -0
- data/lib/format_for_extensions.rb +59 -0
- data/test/helper.rb +27 -0
- data/test/lib/format_for_extensions/test_config.rb +12 -0
- data/test/models/abstract_model.rb +12 -0
- data/test/models/person.rb +9 -0
- data/test/test_format_for_extensions.rb +59 -0
- metadata +173 -0
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "rails", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.6.2"
|
11
|
+
gem "mocha", "0.9.5"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
gem "shoulda", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (2.3.11)
|
5
|
+
actionpack (= 2.3.11)
|
6
|
+
actionpack (2.3.11)
|
7
|
+
activesupport (= 2.3.11)
|
8
|
+
rack (~> 1.1.0)
|
9
|
+
activerecord (2.3.11)
|
10
|
+
activesupport (= 2.3.11)
|
11
|
+
activeresource (2.3.11)
|
12
|
+
activesupport (= 2.3.11)
|
13
|
+
activesupport (2.3.11)
|
14
|
+
git (1.2.5)
|
15
|
+
jeweler (1.6.2)
|
16
|
+
bundler (~> 1.0)
|
17
|
+
git (>= 1.2.5)
|
18
|
+
rake
|
19
|
+
mocha (0.9.5)
|
20
|
+
rake
|
21
|
+
rack (1.1.1)
|
22
|
+
rails (2.3.11)
|
23
|
+
actionmailer (= 2.3.11)
|
24
|
+
actionpack (= 2.3.11)
|
25
|
+
activerecord (= 2.3.11)
|
26
|
+
activeresource (= 2.3.11)
|
27
|
+
activesupport (= 2.3.11)
|
28
|
+
rake (>= 0.8.3)
|
29
|
+
rake (0.8.7)
|
30
|
+
rcov (0.9.9)
|
31
|
+
shoulda (2.11.3)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
bundler (~> 1.0.0)
|
38
|
+
jeweler (~> 1.6.2)
|
39
|
+
mocha (= 0.9.5)
|
40
|
+
rails (>= 2.3.5)
|
41
|
+
rcov
|
42
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Karle Durante
|
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,86 @@
|
|
1
|
+
= format_for
|
2
|
+
|
3
|
+
Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use format_for to dynamically define reusable formats for any field you want.
|
4
|
+
|
5
|
+
By default, format_for comes with smart formats for email addresses and postal codes. Even better, no code repetition! Use constructs that make sense such as "validates_email_for :account_owner" and "validates_postal_code_for :address_postal_code".
|
6
|
+
|
7
|
+
|
8
|
+
== System Requirements
|
9
|
+
format_for should work with Rails 2.3.5 and above. However, I have not yet tested this with Rails 3.1.
|
10
|
+
|
11
|
+
|
12
|
+
== Installation
|
13
|
+
Modify your Gemfile to include format_for:
|
14
|
+
|
15
|
+
gem 'format_for'
|
16
|
+
|
17
|
+
Run `bundle install`. You thought it would be harder?
|
18
|
+
|
19
|
+
|
20
|
+
== Usage
|
21
|
+
By default, ArValidationExtensions supports email and postal code formats. These can be leveraged by including code such as:
|
22
|
+
|
23
|
+
class Person < ActiveRecord::Base
|
24
|
+
# Notice how the normal ActiveRecord Validation options can still be used...
|
25
|
+
validates_email_for :email_address
|
26
|
+
validates_email_for :home_email_address, :if => :full_validation_required
|
27
|
+
validates_postal_code_for :postal_code, :allow_blank => true
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
=== Configuration
|
32
|
+
|
33
|
+
Default configurations are specified in the gem's config/ar_validation_extensions.yml file. If you wish to modify the default regular expressions or messages reported when validation fails, simply copy this file to the config directory in your Rails root folder. You can also manually create [rails_root]/config/ar_validation_extensions.yml if you'd like to.
|
34
|
+
|
35
|
+
The format of this file is as follows:
|
36
|
+
|
37
|
+
# ar_validation_extensions.yml
|
38
|
+
en:
|
39
|
+
email:
|
40
|
+
regex: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/
|
41
|
+
message: "is invalid. The address should be in a format similar to 'user@example.com'."
|
42
|
+
|
43
|
+
PLEASE NOTE that localization is not really supported at this time. By default we only support english mappings, the 'en' locale exists to allow us to more easily support localization in the future.
|
44
|
+
|
45
|
+
If you modify this file, you will need to bounce your application server (or restart your rails console) as this gem caches configurations at load time.
|
46
|
+
|
47
|
+
|
48
|
+
=== The Hotness
|
49
|
+
You can dynamically create your own format validators! For instance, add this to your [rails_root]/config/ar_validation_extensions.yml file:
|
50
|
+
|
51
|
+
# ar_validation_extensions.yml
|
52
|
+
en:
|
53
|
+
hotness:
|
54
|
+
regex: /^hot/
|
55
|
+
message: "is not hot!"
|
56
|
+
|
57
|
+
And you now have a completely useless method to validate that fields begin with 'hot':
|
58
|
+
|
59
|
+
class Person < ActiveRecord::Base
|
60
|
+
# Validation will fail if your first_name does not begin with 'hot'
|
61
|
+
validates_hotness_for :first_name
|
62
|
+
end
|
63
|
+
|
64
|
+
As you might have guessed, the gem is driven off of the configuration file. The same way you can add validators, you can remove them by simply deleting lines from the configuration file.
|
65
|
+
|
66
|
+
|
67
|
+
== TODO
|
68
|
+
- We intend to support more locales other then english. The idea being that we'd check the current local, and load the regex/message accordingly.
|
69
|
+
- Support some other highly repetitious fields, such as phone number.
|
70
|
+
|
71
|
+
|
72
|
+
== Contributing to format_for
|
73
|
+
|
74
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
75
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
76
|
+
* Fork the project
|
77
|
+
* Start a feature/bugfix branch
|
78
|
+
* Commit and push until you are happy with your contribution
|
79
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
80
|
+
* 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.
|
81
|
+
|
82
|
+
== Copyright
|
83
|
+
|
84
|
+
Copyright (c) 2011 Karle Durante. See LICENSE.txt for
|
85
|
+
further details.
|
86
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
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 = "format_for_extensions"
|
18
|
+
gem.homepage = "http://github.com/customink/format_for_extensions"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Extends ActiveRecord validates_format_of validations with reusable and customizable validation methods.}
|
21
|
+
gem.description = %Q{Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use format_for_extensions to dynamically define reusable formats for any ActiveRecord model attribute you want.}
|
22
|
+
gem.email = "kdurante@customink.com"
|
23
|
+
gem.authors = ["Karle Durante"]
|
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
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "ar_validation_extensions #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# Default validation configurations...
|
2
|
+
en:
|
3
|
+
postal_code:
|
4
|
+
regex: /^[0-9]{5}(-[0-9]{4})?|[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d?$/
|
5
|
+
message: "is invalid. Valid formats: 94105-0011 or 94105 or T2X 1V4 or T2X1V4"
|
6
|
+
|
7
|
+
email:
|
8
|
+
regex: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/
|
9
|
+
message: "is invalid. The address should be in a format similar to 'user@example.com'."
|
@@ -0,0 +1,69 @@
|
|
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 = %q{format_for_extensions}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Karle Durante"]
|
12
|
+
s.date = %q{2011-06-02}
|
13
|
+
s.description = %q{Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use format_for_extensions to dynamically define reusable formats for any ActiveRecord model attribute you want.}
|
14
|
+
s.email = %q{kdurante@customink.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"config/format_for_extensions.yml",
|
27
|
+
"format_for_extensions.gemspec",
|
28
|
+
"lib/format_for_extensions.rb",
|
29
|
+
"lib/format_for_extensions/config.rb",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/lib/format_for_extensions/test_config.rb",
|
32
|
+
"test/models/abstract_model.rb",
|
33
|
+
"test/models/person.rb",
|
34
|
+
"test/test_format_for_extensions.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/customink/format_for_extensions}
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.4.2}
|
40
|
+
s.summary = %q{Extends ActiveRecord validates_format_of validations with reusable and customizable validation methods.}
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<rails>, [">= 2.3.5"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
49
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.5"])
|
50
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rails>, [">= 2.3.5"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
56
|
+
s.add_dependency(%q<mocha>, ["= 0.9.5"])
|
57
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
58
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rails>, [">= 2.3.5"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
64
|
+
s.add_dependency(%q<mocha>, ["= 0.9.5"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module FormatForExtensions
|
2
|
+
class Config
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
# TODO: Bake in support for multiple locales...
|
6
|
+
DEFAULT_LOCALE = 'en'
|
7
|
+
|
8
|
+
def self.values(locale = DEFAULT_LOCALE)
|
9
|
+
@@config ||= nil
|
10
|
+
|
11
|
+
# First, we want to see if the consumer of this gem has defined a config file
|
12
|
+
return @@config[locale] unless @@config.nil?
|
13
|
+
|
14
|
+
begin
|
15
|
+
@@config = YAML.load_file(File.join(Rails.root, 'config', 'format_for_extensions.yml'))
|
16
|
+
rescue
|
17
|
+
# fall back to our smart defaults
|
18
|
+
@@config = YAML.load_file(File.join(File.dirname(__FILE__), '../../config', 'format_for_extensions.yml'))
|
19
|
+
end
|
20
|
+
|
21
|
+
@@config[locale]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module FormatForExtensions
|
4
|
+
module ActiveRecord
|
5
|
+
module Validations
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
require 'format_for_extensions/config'
|
12
|
+
|
13
|
+
begin
|
14
|
+
FIELDS = FormatForExtensions::Config.values.keys
|
15
|
+
rescue Exception => e
|
16
|
+
puts "Exception loading config: #{e.inspect}"
|
17
|
+
raise(RuntimeError, "Your format_for_extensions.yml file seems to be invalid, perhaps you've mis-keyed something?")
|
18
|
+
end
|
19
|
+
|
20
|
+
FIELDS.each do |field|
|
21
|
+
define_method("validates_#{field}_for") do |*attr_names|
|
22
|
+
configuration = {:with => self.send("#{field}_regex"),
|
23
|
+
:message => self.send("#{field}_message")
|
24
|
+
}
|
25
|
+
|
26
|
+
# Add in user supplied options (yes, users can still override the regex and
|
27
|
+
# message with custom options)
|
28
|
+
configuration.update(attr_names.extract_options!)
|
29
|
+
|
30
|
+
# Leverage existing ActiveRecord::Validations...
|
31
|
+
validates_format_of(attr_names, configuration)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Compose accessor methods for each of the specified field types
|
36
|
+
# e.g. postal_code_regex, postal_code_message, etc...
|
37
|
+
['postal_code', 'email'].each do |field|
|
38
|
+
define_method("#{field}_regex".to_sym) do
|
39
|
+
regex = FormatForExtensions::Config.values[field]['regex']
|
40
|
+
raise(ArgumentError, "Your format_for_extensions.yml is missing the mapping '#{field}: regex:'") if regex.blank?
|
41
|
+
|
42
|
+
return eval(regex)
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method("#{field}_message".to_sym) do
|
46
|
+
message = FormatForExtensions::Config.values[field]['message']
|
47
|
+
raise(ArgumentError, "Your format_for_extensions.yml is missing the mapping '#{field}: message:'") if message.blank?
|
48
|
+
|
49
|
+
return message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Base.class_eval do
|
58
|
+
include FormatForExtensions::ActiveRecord::Validations
|
59
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
require 'active_record'
|
13
|
+
require 'active_record/test_case'
|
14
|
+
require 'mocha'
|
15
|
+
|
16
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
17
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
18
|
+
require 'format_for_extensions'
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
# These have to load AFTER our gem...
|
23
|
+
require 'models/abstract_model'
|
24
|
+
require 'models/person'
|
25
|
+
|
26
|
+
class Test::Unit::TestCase
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'format_for_extensions/config'
|
3
|
+
|
4
|
+
class TestConfig < Test::Unit::TestCase
|
5
|
+
should "load the default yml file if an override has not been specified" do
|
6
|
+
config = FormatForExtensions::Config.values
|
7
|
+
|
8
|
+
assert_equal "is invalid. Valid formats: 94105-0011 or 94105 or T2X 1V4 or T2X1V4", config['postal_code']['message']
|
9
|
+
assert_equal "is invalid. The address should be in a format similar to 'user@example.com'.", config['email']['message']
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
# require 'active_record/base'
|
3
|
+
|
4
|
+
class AbstractModel < ActiveRecord::Base
|
5
|
+
self.abstract_class = true
|
6
|
+
|
7
|
+
def self.columns() @columns ||= []; end
|
8
|
+
|
9
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
10
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFormatForExtensions < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "When loading FormatFor" do
|
6
|
+
context "with valid configurations" do
|
7
|
+
setup { @fields = FormatForExtensions::Config.values.keys }
|
8
|
+
|
9
|
+
should "define extended validation methods for each configured field" do
|
10
|
+
@fields.each do |field|
|
11
|
+
assert ActiveRecord::Base.respond_to?("validates_#{field}_for")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
should "define data accessor methods for each configured field" do
|
16
|
+
@fields.each do |field|
|
17
|
+
assert ActiveRecord::Base.respond_to?("#{field}_regex")
|
18
|
+
assert ActiveRecord::Base.respond_to?("#{field}_message")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
should "consider the postal code to be valid" do
|
23
|
+
person = Person.new
|
24
|
+
['94105-0011', '94105', 'T2X 1V4', 'T2X1V4'].each do |postal_code|
|
25
|
+
person.postal_code = postal_code
|
26
|
+
assert person.valid?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
should "consider the postal code to be invalid" do
|
31
|
+
person = Person.new
|
32
|
+
['9410', 'abc', '123-123'].each do |postal_code|
|
33
|
+
person.postal_code = postal_code
|
34
|
+
assert !person.valid?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
should "consider the email to be valid" do
|
39
|
+
person = Person.new
|
40
|
+
['user@example.org', 'some.other.guy@yahoo.us.ca', 'a@b.com'].each do |email|
|
41
|
+
person.email = email
|
42
|
+
assert person.valid?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
should "consider the email to be valid" do
|
47
|
+
person = Person.new
|
48
|
+
['user@example', 'yahoo.us.ca', 'a.com'].each do |email|
|
49
|
+
person.email = email
|
50
|
+
assert !person.valid?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with invalid configurations " do
|
56
|
+
# TODO: How to test this?? Once active record and the gem are loaded, how do we unload/reload them with bad params?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: format_for_extensions
|
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
|
+
- Karle Durante
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-02 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 9
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 3
|
32
|
+
- 5
|
33
|
+
version: 2.3.5
|
34
|
+
requirement: *id001
|
35
|
+
prerelease: false
|
36
|
+
name: rails
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :development
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
requirement: *id002
|
51
|
+
prerelease: false
|
52
|
+
name: bundler
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
type: :development
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 11
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 6
|
64
|
+
- 2
|
65
|
+
version: 1.6.2
|
66
|
+
requirement: *id003
|
67
|
+
prerelease: false
|
68
|
+
name: jeweler
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
type: :development
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - "="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 49
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
- 9
|
80
|
+
- 5
|
81
|
+
version: 0.9.5
|
82
|
+
requirement: *id004
|
83
|
+
prerelease: false
|
84
|
+
name: mocha
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
type: :development
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirement: *id005
|
97
|
+
prerelease: false
|
98
|
+
name: rcov
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
type: :development
|
101
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
requirement: *id006
|
111
|
+
prerelease: false
|
112
|
+
name: shoulda
|
113
|
+
description: Tired of repeating 'validates_format_of' with the same regex expression across your models only to validate the same email address and postal code fields? So am I. Use format_for_extensions to dynamically define reusable formats for any ActiveRecord model attribute you want.
|
114
|
+
email: kdurante@customink.com
|
115
|
+
executables: []
|
116
|
+
|
117
|
+
extensions: []
|
118
|
+
|
119
|
+
extra_rdoc_files:
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.rdoc
|
122
|
+
files:
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.rdoc
|
127
|
+
- Rakefile
|
128
|
+
- VERSION
|
129
|
+
- config/format_for_extensions.yml
|
130
|
+
- format_for_extensions.gemspec
|
131
|
+
- lib/format_for_extensions.rb
|
132
|
+
- lib/format_for_extensions/config.rb
|
133
|
+
- test/helper.rb
|
134
|
+
- test/lib/format_for_extensions/test_config.rb
|
135
|
+
- test/models/abstract_model.rb
|
136
|
+
- test/models/person.rb
|
137
|
+
- test/test_format_for_extensions.rb
|
138
|
+
has_rdoc: true
|
139
|
+
homepage: http://github.com/customink/format_for_extensions
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
hash: 3
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
version: "0"
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
hash: 3
|
162
|
+
segments:
|
163
|
+
- 0
|
164
|
+
version: "0"
|
165
|
+
requirements: []
|
166
|
+
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 1.4.2
|
169
|
+
signing_key:
|
170
|
+
specification_version: 3
|
171
|
+
summary: Extends ActiveRecord validates_format_of validations with reusable and customizable validation methods.
|
172
|
+
test_files: []
|
173
|
+
|