Hi,
I have a table containing system account types (“Income”, “Ordered”,
“Available” etc) which are created as seed data during application
installation.
class InventoryAccountType < ActiveRecord::Base
has_many :inventory_accounts
enumerable_constant :normal_balance, :constants => [:debit, :credit]
validates_uniqueness_of :name
system account types
def self.income
find_by_name(“Income”)
end
def self.ordered
find_by_name(“Ordered”)
end
def self.available
find_by_name(“Available”)
end
end
These account types represent system concepts that are referenced in
code elsewhere in application, for example:
class InventoryTransaction < ActiveRecord::Base
has_many :inventory_account_entries
validate :account_entries_balance
locate new inventory
def self.create_inventory(product_sku, storage_location, quantity,
inventory_batch = nil)
# if no batch specified, create a new batch
if inventory_batch.nil?
inventory_batch = InventoryBatch.create(:product_sku =>
product_sku)
end
product_income_account = InventoryAccount.retrieve_by_type
(product_sku, InventoryAccountType.income)
available_product_location_account =
InventoryAccount.retrieve_by_location(inventory_batch,
storage_location, InventoryAccountType.available)
account_transfer(product_income_account,
available_product_location_account, quantity)
end
end
Do you think this approach to hardcoding record lookups into the
InventoryAccountType model in such a way is good practice?
There may be other InventoryAccountTypes created by the user during
application usage but these would be only be managed and allocated to
by the user as general account transfers. Not by system core system
use cases such as creating new inventory as addressed by it own
methods in the model.
I have also added a “system” boolean column to the
InventoryAccountType table so as to know which types cannot be deleted
or edited.
I was also thinking of replacing the:
def self.available
find_by_name(“Available”)
end
…style methods for each system type with some of dynamic ruby method
that automatically creates a method for any database record that has
system == 1. This would save having to clutter the
InventoryAccountType model with multiple method definitions doing the
same thing.
Thanks in advance, Andrew.