Permalink
Browse files

added gift card model and associations to order model

  • Loading branch information...
1 parent 144e964 commit 9658c7b71f685c8fcd26850ec1bc9e217e8b72f8 @ericcolon committed Jan 3, 2012
@@ -0,0 +1,22 @@
+module Spree
+ class Order < ActiveRecord::Base
+ belongs_to :order
+
+ before_create :generate_gift_code
+
+ def gift_code_valid?(g_code)
+ (self.validation_code_hash == encode_gift_code(g_code)) && self.redeemed == false
+ end
+
+ protected
+ def generate_validation_code
+ self.validation_code = 1000000001+Random.rand(8999999998) # Code provided by Omar Ramos
+ self.validation_code_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
+ self.validation_code_hash = encode_validation_code(validation_code)
+ end
+
+ def encode_validation_code(g_code)
+ Digest::SHA1.hexdigest([g_code, validation_code_salt].join)
+ end
+ end
+end
@@ -16,6 +16,7 @@ class Order < ActiveRecord::Base
has_many :shipments, :dependent => :destroy
has_many :return_authorizations, :dependent => :destroy
has_many :adjustments, :as => :adjustable, :dependent => :destroy
+ has_many :gift_card_codes # added association to gift card codes model
accepts_nested_attributes_for :line_items
accepts_nested_attributes_for :bill_address
@@ -193,6 +193,16 @@ def change
t.timestamps
end
+ # Creating Gift Card Codes table, referencing an order
+ create_table :gift_card_codes, :force => true do |t|
+ t.string :validation_code
+ t.string :validation_hash
+ t.string :validation_salt
+ t.boolean :is_redeemed, :default => false
+ t.references :order
+ t.timestamps
+ end
+
create_table :preferences, :force => true do |t|
t.string :attribute, :null => false, :limit => 100
t.integer :owner_id, :null => false, :limit => 30

0 comments on commit 9658c7b

Please sign in to comment.