Permalink
Browse files

fix validation error on encrypted field in embedded document.

  • Loading branch information...
1 parent 815bd8a commit 44e1d4402aa808637f2bfa093adbbc18b552f75f @juno committed Feb 25, 2011
Showing with 44 additions and 1 deletion.
  1. +1 −1 lib/mongoid/encryptor.rb
  2. +23 −0 spec/embedded_document_spec.rb
  3. +10 −0 spec/models/credit_card.rb
  4. +10 −0 spec/models/person.rb
View
@@ -19,7 +19,7 @@ def encrypts(*attrs)
mode = options.delete(:mode) || :sha
cipher_class = EncryptedStrings.const_get("#{mode.to_s.classify}Cipher")
- send(:before_validation) do |doc|
+ send(:after_validation) do |doc|
doc.send(:write_encrypted_attribute, attr_name, cipher_class, options)
true
end
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+require 'spec_helper'
+
+describe "Symmetric encryption on embedded document" do
+ let(:credit_card) { CreditCard.new(:number => '0000111122224444') }
+ let(:person) { Person.new(:first_name => 'John', :last_name => 'Smith', :credit_card => credit_card) }
+
+ context "Has valid attributes" do
+ context "Before save" do
+ subject { credit_card }
+ its(:number) { should eq('0000111122224444') }
+ it { should be_valid }
+ end
+
+ context "after save" do
+ before { credit_card.save! }
+ subject { credit_card }
+ its(:number) { should be_encrypted }
+ its(:number) { should eq('0000111122224444') }
+ end
+ end
+
+end
View
@@ -0,0 +1,10 @@
+class CreditCard
+ include Mongoid::Document
+ include Mongoid::Encryptor
+
+ field :number
+
+ validates_format_of :number, :with => /^\A\d{16}\z/
+
+ encrypts :number, :mode => :symmetric, :password => 'secret'
+end
View
@@ -0,0 +1,10 @@
+class Person
+ include Mongoid::Document
+
+ field :first_name
+ field :last_name
+
+ embeds_one :credit_card
+
+ validates_presence_of :first_name, :last_name
+end

0 comments on commit 44e1d44

Please sign in to comment.