Skip to content

Instantly share code, notes, and snippets.

@nogtini
Forked from StevenJL/mti.rb
Created August 15, 2019 17:38
Show Gist options
  • Save nogtini/50cc3c6e488cb0f70c4616bf807d8a9d to your computer and use it in GitHub Desktop.
Save nogtini/50cc3c6e488cb0f70c4616bf807d8a9d 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