I read everywhere that business logic belongs to models, not to the controller, but where is the limit? I play with a personal account.
Account
Entry
Operation
When creating an operation, it is valid only if the corresponding entries are created and linked to accounts, so that the operation is balanced, for example, buy a 6-pack:
o=Operation.new({:description=>"b33r", :user=>current_user, :date=>"2008/09/15"})
o.entries.build({:account_id=>1, :amount=>15})
o.valid?
o.entries.build({:account_id=>2, :amount=>-15})
o.valid?
Now the form displayed to the user in the case of basic operations is simplified to hide the data of records, accounts are selected from 5 by default according to the type of operation requested by the user (intialise account → equity for accout, spend assets → expenses, earn income → assets, take on liabilities themselves → assets, pay debt assets → liabilities ...) I want the records to be created from the default values.
( 2 ). , . Entry.
? SimpleOperationController, , Operation, Operation.new_simple_operation (params [: operation])
, Entry Operation?
:)
edit - , .
. , :
, , : , , . , .
def spend
amount=params[:operation].delete(:amount)
op=Operation.new(params[:operation])
...
op.entries.build(...)
op.entries.build(...)
op.save
end
,
def spend
op=Operation.new_simple_operation(params)
op.save
end
, , .