Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Alterados blocos compose_of no cpf e cnpj

Signed-off-by: Marcos Tapajós <tapajos@improveit.com.br>
  • Loading branch information...
commit dfd56f32abcb35b8094e947c7c21a518dab629ae 1 parent 664189b
Cássio Marques cassiomarques authored Marcos Tapajós committed
2  .gitignore
View
@@ -4,3 +4,5 @@
pkg
doc
*~
+*swp
+*swo
BIN  brcpfcnpj/lib/brcpfcnpj/.cpf_cnpj_activerecord.rb.swp
View
Binary file not shown
11 brcpfcnpj/lib/brcpfcnpj/cpf_cnpj_activerecord.rb
View
@@ -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?
BIN  brcpfcnpj/spec/.spec_helper.rb.swp
View
Binary file not shown
25 brcpfcnpj/spec/cpf_active_record_spec.rb
View
@@ -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
BIN  brcpfcnpj/spec/db/.create_testing_structure.rb.swp
View
Binary file not shown
18 brcpfcnpj/spec/db/create_testing_structure.rb
View
@@ -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
+
6 brcpfcnpj/spec/spec_helper.rb
View
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.