Skip to content

Instantly share code, notes, and snippets.

@aderyabin
Created August 18, 2013 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aderyabin/6263944 to your computer and use it in GitHub Desktop.
Save aderyabin/6263944 to your computer and use it in GitHub Desktop.
class Merchant < ActiveRecord::Base
# Include default devise modules
devise :database_authenticatable, :registerable, :token_authenticatable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :timeoutable, :two_factor_authenticatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :callback_url,
:name, :surname, :phone_number, :address_zip, :address_city, :address,
:balance_btc, :two_factor_authentication
attr_accessor :enabled_wallet_ids
#attr_accessible :company_account_attributes,:bank_account_attributes
has_attached_file :proof_of_id
has_attached_file :proof_of_address
monetize :balance_cents
attr_protected :api_key
# attr_accessible :title, :body
#validates_presence_of :api_key
validates :api_key, uniqueness: true
has_many :wallets
has_one :bank_account
has_one :company_account
accepts_nested_attributes_for :company_account
accepts_nested_attributes_for :bank_account
before_create :build_default_accounts
before_create :build_default_wallet
after_save :update_enabled_wallets
def enabled_wallets=(wallets_list=[])
#create_wallet if not exist
wallets_list.each do |enabled_wallet|
unless wallets.find_by_balance_currency enabled_wallet
wallets.create balance_currency: enabled_wallet, enabled: true
end
end
wallets.each do |wallet|
wallet.enabled = wallet.balance_currency.in? wallets_list
wallet.save
end
end
private
def build_default_accounts
build_bank_account
build_company_account
true
end
def build_default_wallet
wallets.build balance_currency: 'USD'
end
def update_enabled_wallets
# write logic to update enabled wallets
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment