Permalink
Browse files

PaymentType モデルとサブクラス実装 / 7.2 節まで

  • Loading branch information...
1 parent 74cdf98 commit 59629493e5b7bfcc59fe66d230240e2f5aefb36e @labocho committed Mar 30, 2012
@@ -0,0 +1,9 @@
+class AmericanExpress < PaymentType
+ def validate_card_number (card_number, expiry)
+ p "AMEX card number validation"
+ end
+
+ def process_order(name, address, amount)
+ p "AMEX process order"
+ end
+end
@@ -0,0 +1,2 @@
+class MasterCard < PaymentType
+end
View
@@ -1,4 +1,18 @@
class Order < ActiveRecord::Base
acts_as_address
+ belongs_to :movie_showtimes
+ belongs_to :payment_type
has_many :purchased_tickets
+
+ validate do
+ validate_credit_card_number
+ end
+
+ private
+ def validate_credit_card_number
+ return unless payment_type
+ unless payment_type.validate_card_number(credit_card_number, expiration_date)
+ errors.add(:credit_card_number, :is_valid)
+ end
+ end
end
View
@@ -0,0 +1,14 @@
+class PaymentType < ActiveRecord::Base
+ has_many :orders
+ VISA = find_by_type("Visa")
+ MASTER_CARD = find_by_type("MasterCard")
+ AMEX = find_by_type("AmericanExpress")
+
+ def validate_card_number (card_number, expiry)
+ raise "This method must be redefined in the subclass"
+ end
+
+ def process_order(name, address, amount)
+ raise "This method must be redefined in the subclass"
+ end
+end
View
@@ -0,0 +1,2 @@
+class Visa < PaymentType
+end
@@ -0,0 +1,23 @@
+class CreatePaymentTypes < ActiveRecord::Migration
+ def up
+ execute <<-SQL
+ create sequence payment_types_id_seq;
+ create table payment_types (
+ id integer not null
+ default nextval('payment_types_id_seq'),
+ type varchar(128) not null unique
+ check (length(type) > 0),
+ primary key (id)
+ );
+ alter table orders add foreign key (payment_type_id) references payment_types (id);
+ SQL
+ end
+
+ def down
+ execute <<-SQL
+ alter table orders drop constraint orders_payment_type_id_fkey;
+ drop table payment_types;
+ drop sequence payment_types_id_seq;
+ SQL
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120328131331) do
+ActiveRecord::Schema.define(:version => 20120330171931) do
create_table "addresses", :id => false, :force => true do |t|
t.string "line_1", :null => false
@@ -66,6 +66,13 @@
t.date "expiration_date", :null => false
end
+ create_table "payment_types", :id => false, :force => true do |t|
+ t.integer "id", :null => false
+ t.string "type", :limit => 128, :null => false
+ end
+
+ add_index "payment_types", ["type"], :name => "payment_types_type_key", :unique => true
+
create_table "purchased_tickets", :id => false, :force => true do |t|
t.integer "id", :null => false
t.integer "order_id", :null => false
View
@@ -34,3 +34,9 @@
zip_code.save!
end
+["MasterCard", "Visa", "AmericanExpress"].each do |type|
+ next if PaymentType.exists?(type: type)
+ payment_type = PaymentType.new
+ payment_type.type = type
+ payment_type.save!
+end

0 comments on commit 5962949

Please sign in to comment.