Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 73c59a531e
...
compare: 41256b378f
  • 4 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 20, 2012
@MarceloCajueiro The real sense of this gem is validate the value before the type cast.
So i18n is out of our mind and unnecessary for us.

Will be use the active record method "*attribute*_before_type_cast" to get the string and validate it in according of the regexp;
995383d
@MarceloCajueiro Active model was removed from Gemfile because is already loaded as de…
…pendecy;
c9e70ce
@MarceloCajueiro Added more explanation for this gem; d64438e
@MarceloCajueiro Release the gem as the version 0.2; 41256b3
View
2  Gemfile
@@ -4,5 +4,5 @@ gemspec
group :test do
gem "rspec"
- gem "activemodel"
+ gem "sqlite3"
end
View
4 README.markdown
@@ -2,7 +2,9 @@
This gem was inspired in the Sobrinho's gems to validate simple things inside of ActiveModel.
-Use mask in inputs and validate then in the models.
+Use mask in inputs and validate in the models.
+
+The gem works getting the value before type cast and comparing with a regexp from the mask pattern.
## Installation
View
23 lib/mask_validator.rb
@@ -14,7 +14,7 @@ class MaskValidator < ActiveModel::EachValidator
# * - to alphanumerics (A-Z, a-z, 0-9)
#
def validate_each(record, attribute, value)
- value = converted_value(value)
+ value = record.send("#{attribute.to_s}_before_type_cast")
if value.nil?
record.errors.add(attribute, :blank) unless allow_nil?
@@ -43,27 +43,6 @@ def character_map
{ "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" }
end
- def converted_value(value)
- if [Date, DateTime, Time].include?(value.class)
- date_or_time_value(value)
- else
- value
- end
- end
-
- #
- # Use the value with i18n to validate
- #
- def date_or_time_value(value)
- type = (value.class == Date) ? 'date' : 'time'
-
- if I18n.t("#{type}.formats.mask") =~ /missing/
- I18n.l(value)
- else
- I18n.l(value, :format => :mask)
- end
- end
-
private
def message
View
4 mask_validator.gemspec
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = "mask_validator"
- s.version = "0.1"
+ s.version = "0.2"
s.platform = Gem::Platform::RUBY
s.authors = ["Marcelo Cajueiro"]
s.email = ["marcelocajueiro@gmail.com"]
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency 'activemodel', ">= 3.0"
+ s.add_dependency 'activerecord', ">= 3.0"
s.add_development_dependency 'rake', '>= 0.8.7'
-
end
View
27 spec/mask_validator_spec.rb
@@ -1,7 +1,6 @@
require 'spec_helper'
describe MaskValidator do
-
subject do
Person.new :phone => '(12) 3456-7890',
:fax => '(12) 3456-7890',
@@ -9,7 +8,9 @@
:alphanumeric => 'AAA666',
:zip_code => '88900-000',
:birth_date => '13/10/1989',
- :birth_time => '10:20'
+ :birth_time => '10:20',
+ :birth_year => '2011',
+ :body_fat => '23,44'
end
it "Person should be valid" do
@@ -134,4 +135,26 @@
subject.should be_valid
end
end
+
+ context "when the attributes type is not a string" do
+ it "should not be valid with a wrong date format" do
+ subject.birth_date = "15-12-2009"
+ subject.should be_invalid
+ end
+
+ it "should not be valid with a wrong birth year" do
+ subject.birth_year = "20110"
+ subject.should be_invalid
+ end
+
+ it "should not be valid with a wrong birth time" do
+ subject.birth_time = "333:20"
+ subject.should be_invalid
+ end
+
+ it "should not be valid with a wrong body fat" do
+ subject.birth_time = "333,00%"
+ subject.should be_invalid
+ end
+ end
end
View
11 spec/models/person.rb
@@ -0,0 +1,11 @@
+class Person < ActiveRecord::Base
+ validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
+ validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
+ validates :acronym, :mask => "***", :allow_nil => true
+ validates :alphanumeric, :mask => "aaa999"
+ validates :zip_code, :mask => "99999-999", :allow_blank => false
+ validates :birth_date, :mask => '99/99/9999'
+ validates :birth_time, :mask => '99:99'
+ validates :birth_year, :mask => '9999'
+ validates :body_fat, :mask => "99,99"
+end
View
18 spec/spec_helper.rb
@@ -1,19 +1,7 @@
require 'rspec'
require 'active_model'
-require 'ostruct'
+require 'active_record'
require 'mask_validator'
-class Person < OpenStruct
- include ActiveModel::Validations
-
- validates :birth_date, :mask => '99/99/9999'
- validates :birth_time, :mask => '99:99'
- validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
- validates :acronym, :mask => "***", :allow_nil => true
- validates :alphanumeric, :mask => "aaa999"
- validates :zip_code, :mask => "99999-999", :allow_blank => false
- validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
-end
-
-I18n.locale = :pt
-I18n.load_path << Dir[File.expand_path("../support/locales/*.yml", __FILE__)]
+require "support/db/schema"
+require "support/models/person"
View
20 spec/support/db/schema.rb
@@ -0,0 +1,20 @@
+ActiveRecord::Base.establish_connection(
+ :adapter => "sqlite3",
+ :database => ":memory:"
+)
+
+ActiveRecord::Schema.define do
+ self.verbose = false
+
+ create_table :people do |t|
+ t.string :phone
+ t.string :acronym
+ t.string :alphanumeric
+ t.string :zip_code
+ t.string :fax
+ t.decimal :body_fat
+ t.integer :birth_year
+ t.date :birth_date
+ t.datetime :birth_time
+ end
+end
View
9 spec/support/locales/pt.yml
@@ -1,9 +0,0 @@
-pt:
- date:
- formats:
- default: "%d/%m/%Y"
-
- time:
- formats:
- default: "%d/%m/%Y %H:%M:%S"
- mask: "%H:%M"
View
11 spec/support/models/person.rb
@@ -0,0 +1,11 @@
+class Person < ActiveRecord::Base
+ validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
+ validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
+ validates :acronym, :mask => "***", :allow_nil => true
+ validates :alphanumeric, :mask => "aaa999"
+ validates :zip_code, :mask => "99999-999", :allow_blank => false
+ validates :birth_date, :mask => '99/99/9999'
+ validates :birth_time, :mask => '99:99'
+ validates :birth_year, :mask => '9999'
+ validates :body_fat, :mask => "99,99"
+end

No commit comments for this range

Something went wrong with that request. Please try again.