Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created March 1, 2018 08:19
Show Gist options
  • Save StevenJL/d2e90a33d06d5946996201e6b6c4b9b8 to your computer and use it in GitHub Desktop.
Save StevenJL/d2e90a33d06d5946996201e6b6c4b9b8 to your computer and use it in GitHub Desktop.
class Account < ActiveRecord::Base
def withdraw(amount)
# ...
end
def deposit(amount)
# ...
end
def close!
update_attribute!(closed_at, Time.current)
end
end
# These three types of accounts inherit from a common base class
# so they can all share a common `#close` method. However, each
# class has its own class-specific methods as well as their own
# tables.
class CorporateAccount < Account
set_table_name "corporate_accounts"
def close!
inform_share_holders!
super
end
end
class SmallBusinessAccount < Account
set_table_name "small_business_accounts"
def close!
inform_mom_and_pop!
super
end
end
class PersonalAccount < Account
set_table_name "personal_accounts"
def close!
inform_dude_or_dudette!
super
end
end
# As such, we need to create three tables. A very un-DRY approach.
class CreateCorporateAccounts < ActiveRecord::Migration
def change
create_table :corporate_accounts do |t|
t.float :balance
t.string :tax_identifier
t.date_time :closed_at
# ... more column fields #
t.timestamps
end
end
end
class CreateSBAccounts < ActiveRecord::Migration
def change
create_table :small_business_accounts do |t|
t.float :balance
t.string :tax_identifier
t.date_time :closed_at
# ... more column fields #
t.timestamps
end
end
end
class CreatePersonalAccounts < ActiveRecord::Migration
def change
create_table :personal_accounts do |t|
t.float :balance
t.string :tax_identifier
t.date_time :closed_at
# ... more column fields #
t.timestamps
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment