Permalink
Browse files

Alterados blocos compose_of no cpf e cnpj

Signed-off-by: Marcos Tapajós <tapajos@improveit.com.br>
  • Loading branch information...
1 parent 664189b commit dfd56f32abcb35b8094e947c7c21a518dab629ae @cassiomarques cassiomarques committed with Marcos Tapajós Jan 13, 2009
View
@@ -4,3 +4,5 @@
pkg
doc
*~
+*swp
+*swo
Binary file not shown.
@@ -6,9 +6,8 @@ module ClassMethods #:nodoc:
def usar_como_cpf(*args) #:nodoc:
unless args.size.zero?
args.each do |name|
- composed_of name, :class_name => 'Cpf', :mapping => [name.to_s, "numero"], :allow_nil => true do
- Cpf.new(name[:numero])
- end
+ composed_of name, :class_name => 'Cpf', :mapping => [name.to_s, "numero"], :allow_nil => true,
+ :constructor => Proc.new { |numero| Cpf.new(numero) }
name = name.to_s
module_eval <<-ADICIONANDO_METODOS_PARA_CPF
validate :#{name}_valido?
@@ -41,10 +40,8 @@ def #{name}=(value)
def usar_como_cnpj(*args) #:nodoc:
unless args.size.zero?
args.each do |name|
- composed_of name, :class_name => 'Cnpj', :mapping => [name.to_s, "numero"], :allow_nil => true do
- Cnpj.new(name[:numero])
- end
-
+ composed_of name, :class_name => 'Cnpj', :mapping => [name.to_s, "numero"], :allow_nil => true,
+ :constructor => Proc.new { |numero| Cnpj.new(numero) }
name = name.to_s
module_eval <<-ADICIONANDO_METODOS_PARA_CNPJ
validate :#{name}_valido?
Binary file not shown.
@@ -1,14 +1,15 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/active_record/base_without_table'
-class Person < ActiveRecord::BaseWithoutTable
+class Pessoa < ActiveRecord::Base
usar_como_cpf :cpf
+ validates_uniqueness_of :cpf
end
describe "Using a model attribute as Cpf" do
before(:each) do
- @person = Person.new
+ @person = Pessoa.new(:nome => "Fulano")
end
it "should format the received number" do
@@ -62,7 +63,7 @@ class Person < ActiveRecord::BaseWithoutTable
end
it "should be able to receive parameters at initialization" do
- @person = Person.new(:cpf => "111.44477735")
+ @person = Pessoa.new(:cpf => "111.44477735")
@person.cpf.numero.should == "111.444.777-35"
end
@@ -74,4 +75,22 @@ class Person < ActiveRecord::BaseWithoutTable
end
end
+describe "using validations" do
+ it "should validate presence of cpf" do
+ Pessoa.validates_presence_of :cpf
+ p = Pessoa.new(:nome => "Fulano")
+ p.should_not be_valid
+ p.errors.on(:cpf).should eql("can't be blank")
+ end
+ it "should validate uniqueness of cpf" do
+ p1 = Pessoa.new(:nome => "Fulano", :cpf => "11144477735")
+ p1.save
+ puts p1.inspect
+ p2 = Pessoa.new(:nome => "Beltrano", :cpf => "11144477735")
+ p2.valid?
+ p p2.errors
+ p2.should_not be_valid
+ p2.errors.on(:cpf).should_not be_nil
+ end
+end
Binary file not shown.
@@ -0,0 +1,18 @@
+class CreateTestingStructure < ActiveRecord::Migration
+ def self.up
+ create_table :empresas do |t|
+ t.string :nome
+ t.string :cnpj
+ end
+ create_table :pessoas do |t|
+ t.string :nome
+ t.string :cpf
+ end
+ end
+
+ def self.down
+ drop_table :pessoas
+ drop_table :empresas
+ end
+end
+
@@ -1,3 +1,9 @@
require 'rubygems'
require 'spec'
require File.expand_path(File.dirname(__FILE__) + "/../lib/brcpfcnpj")
+require 'active_record'
+
+ActiveRecord::Base.establish_connection(:adapter=>"sqlite3", :database => ":memory:")
+require File.dirname(__FILE__) + "/db/create_testing_structure"
+
+CreateTestingStructure.migrate(:up)

0 comments on commit dfd56f3

Please sign in to comment.