1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #---
- # Excerpted from "Agile Web Development with Rails",
- # published by The Pragmatic Bookshelf.
- # Copyrights apply to this code. It may not be used to create training material,
- # courses, books, articles, and the like. Contact us if you are in doubt.
- # We make no guarantees that this code is fit for any purpose.
- # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
- #---
- $: << File.dirname(__FILE__)
- require "connect"
- require "rubygems"
- require "active_record"
- ActiveRecord::Schema.define do
-
- create_table :orders, :force => true do |t|
- t.integer :user_id
- t.string :name
- t.string :address
- t.string :email
- end
-
- create_table :users, :force => :true do |t|
- t.string :name
- end
- end
- class ActiveRecord::Base
- def self.encrypt(*attr_names)
- encrypter = Encrypter.new(attr_names)
-
- before_save encrypter
- after_save encrypter
- after_find encrypter
- define_method(:after_find) { }
- end
- end
- class Encrypter
- # We're passed a list of attributes that should
- # be stored encrypted in the database
- def initialize(attrs_to_manage)
- @attrs_to_manage = attrs_to_manage
- end
- # Before saving or updating, encrypt the fields using the NSA and
- # DHS approved Shift Cipher
- def before_save(model)
- @attrs_to_manage.each do |field|
- model[field].tr!("a-z", "b-za")
- end
- end
- # After saving, decrypt them back
- def after_save(model)
- @attrs_to_manage.each do |field|
- model[field].tr!("b-za", "a-z")
- end
- end
- # Do the same after finding an existing record
- alias_method :after_find, :after_save
- end
- class Order < ActiveRecord::Base
- encrypt(:name, :email)
- end
- o = Order.new
- o.name = "Dave Thomas"
- o.address = "123 The Street"
- o.email = "dave@example.com"
- o.save
- puts o.name
- o = Order.find(o.id)
- puts o.name
|