organized code into Smr module
[smr.git] / gui / app / controllers / objects / portfolio_controller.rb
blob9142b171e868510a2ead50e57be6ae67a1c4492b
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
18 # handles CRUD on Portfolio records
19 class Objects::PortfolioController < ObjectsController
20     def index
21         super
22         @portfolios, @total_pages = smr_paginate(
23             @page = smr_page,
24             Portfolio.where(:id_user=>current_user.id).order(:name).to_a
25         )
26     end
28     ##
29     # defines empty Portfolio to create() a new one
30     def new
31         self.index
32         @portfolio = Portfolio.new(:date_created => Time.now.to_i)
33         render :index
34     end
36     ##
37     # edit Portfolio
38     def edit
39         self.index
41         @portfolio = Portfolio.find(params[:id])
42         render :index
43     end
45     ##
46     # handles creates and updates
47     def create
48         if params[:portfolio][:id].to_i > 0 then
49             p = Portfolio.where(:id=>params[:portfolio][:id], :id_user=>current_user.id).first
50         else p = Portfolio.new(portfolio_params) end
52         # hard-wired so noone spoofes it in here
53         p.id_user = current_user.id
54         p.date_created = Time.parse(params[:time_created]).to_i if params[:time_created]
56         # FIXME: not used any longer but required by schema, drop it?
57         p.type = ''
58         if params[:portfolio][:id].to_i > 0 then
59             p.update(portfolio_params)
60         else p.save end
62         redirect_to objects_portfolio_index_path, :notice=>p.errors.full_messages.uniq.join(', ')
63     end
65  protected
67     ##
68     # internal helper defining parameters acceptable for Portfolio create/update
69     def portfolio_params
70         params.require(:portfolio).permit(
71           :id, :name, :date_created, :taxallowance, :order, :comment
72         )
73     end
74 end