Skip to content

Commit 548e403

Browse files
committed
ActiveValidators 1.1.0
1 parent 2146692 commit 548e403

File tree

10 files changed

+67
-10
lines changed

10 files changed

+67
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.swp
2+
*.gem

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
activevalidators (1.0.2)
4+
activevalidators (1.1.0)
55
activemodel (>= 3.0.0)
66
activerecord (>= 3.0.0)
77
mail

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,32 @@ Collection of ActiveModel/ActiveRecord validations
66
Installation (Rails 3)
77
----------------------
88

9-
In your Gemfile:
9+
In your Gemfile ( >>= 1.1.0 ):
1010

11-
gem 'activevalidators', :require => 'active_validators'
11+
gem 'activevalidators'
12+
13+
**N.B: if you use version <= 1.0.2, use this instead:**
1214

15+
gem 'activevalidators', :require => 'active_validators'
1316

1417
In your models, the gem provides new validators like `email`, or `url`:
1518

1619
class User
1720
validates :email_address, :email => true
1821
validates :link_url, :url => true
22+
validates :user_phone, :phone => true
23+
end
24+
25+
class Article
26+
validates :slug, :slug => true
1927
end
2028

21-
Exhaustive list of supported validators:
29+
Exhaustive list of supported validators and their implementation:
2230

23-
* `email` : checks the email based on the `mail` gem
24-
* `url` : checks the url based on a regular expression
25-
* `phone` : checks the phone number based on a regular expression
31+
* `email` : based on the `mail` gem
32+
* `url` : based on a regular expression
33+
* `phone` : based on a regular expression
34+
* `slug` : based on `ActiveSupport::String#parameterize`
2635

2736
Todo
2837
----

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $:.unshift File.expand_path("../lib", __FILE__)
33

44
require 'rubygems'
55
require 'rubygems/specification'
6-
require 'active_validators'
6+
require 'activevalidators'
77

88
def gemspec
99
@gemspec ||= begin

activevalidators.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $:.unshift lib unless $:.include?(lib)
44

55
Gem::Specification.new do |s|
66
s.name = "activevalidators"
7-
s.version = '1.0.2'
7+
s.version = '1.1.0'
88
s.platform = Gem::Platform::RUBY
99
s.authors = ["Franck Verrot"]
1010
s.email = ["[email protected]"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module ActiveModel
2+
module Validations
3+
class SlugValidator < EachValidator
4+
def validate_each(record, attribute, value)
5+
unless value == value.parameterize
6+
record.errors.add(attribute)
7+
end
8+
end
9+
end
10+
end
11+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module Validations
1111
autoload :UrlValidator
1212
autoload :RespondToValidator
1313
autoload :PhoneValidator
14+
autoload :SlugValidator
1415
end
1516
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Models
2+
class SlugValidatorModel
3+
include ActiveModel::Validations
4+
attr_accessor :slug
5+
validates :slug, :slug => true
6+
end
7+
end

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'active_validators'
1+
require 'activevalidators'
22

33
%w(models).each do |directory|
44
Dir["#{File.dirname(__FILE__)}/#{directory}/*.rb"].each {|f| require f}

spec/specs/slug_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2+
3+
describe "Slug Validation" do
4+
it "accepts valid slugs" do
5+
model = Models::SlugValidatorModel.new
6+
model.slug = '1234567890-foo-bar-bar'
7+
model.valid?.should be(true)
8+
model.should have(0).errors
9+
end
10+
11+
describe "for invalid slugs" do
12+
let(:model) do
13+
Models::SlugValidatorModel.new.tap do |m|
14+
m.slug = '@#$%^'
15+
end
16+
end
17+
18+
it "rejects invalid slugs" do
19+
model.valid?.should be(false)
20+
model.should have(1).errors
21+
end
22+
23+
it "generates an error message of type invalid" do
24+
model.valid?.should be(false)
25+
model.errors[:slug].should == [model.errors.generate_message(:slug, :invalid)]
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)