added pagination gem, many fixes
[smr.git] / gui / app / controllers / objects / stock_controller.rb
blob7be87db98c70630944469aa1edc1dc75bc30af1a
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 Stock records
20 # NOTE:
21 # * this controller is called "Stock" since the model is still Stock. It
22 #   will become "Security" when support for different types of paper is
23 #   implemented (Bonds, Funds, Currency, ...).
24 # * until then we talk to the user about "Securities" already
26 class Objects::StockController < ObjectsController
27     def index
28         super
29         @stocks = Stock.order(:name).paginate(:page => params[:page])
30         @stock = Stock.new
32         @symbolextensions = StockSymbolextension.all
33         @quotesources = StockQuotesource.all
34     end
36     ##
37     # retrieve Stock for editing
38     def edit
39         self.index
40         @stock = Stock.find(params[:id])
41         render :index
42     end
44     ##
45     # handles creates and updates
46     # FIXME: :annual_meeting is broken and hardwired, better drop it?
47     def create
48         if params[:stock][:id].to_i > 0 then
49             s = Stock.find(params[:stock][:id])
50         else s = Stock.new(stock_params) end
52         s.annual_meeting = "1970-01-01"
54         if params[:stock][:id].to_i > 0 then
55             s.update(stock_params)
56         else s.save end
58         redirect_to objects_stock_index_path, :notice=>s.errors.full_messages.uniq.join(', ')
59     end
61  protected
63     ##
64     # internal helper defining parameters acceptable for Stock create/update
65     def stock_params
66         params.require(:stock).permit(
67           :name, :symbol, :id_stock_symbolextension, :id_stock_quotesource,
68           :url, :comment
69         )
70     end
71 end