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
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/>.
20 class AssetsController < ApplicationController
23 # Generates lists of currently open and closed positions where "currently"
24 # is defined by #smr_browse_date.
26 # It also stores some of these lists in the Session. Other controllers
27 # might check for and make use if this to avoid expensive database queries. There is:
28 # * :open_positions_id_stock => collection of Stock#id values of the
29 # positions currently open
32 smr_menu_addsubitem('assets', {
33 '+ position'=>:new_position,
34 '+ quote'=>:new_asset,
37 @assets = Smr::Asset.new(
40 params.fetch(:asset_limit_portfolio, false)
42 @open_positions = @assets.open_positions
44 # exports for use by other controllers
45 session[:open_positions_id_stock] = @assets.open_positions.collect{ |p| p.stock.id }
47 @securities = smr_securities_list
48 @portfolios = smr_portfolios_list
52 # handle new/updated Quote
56 @quote = Quote.where(:id=>params[:id]).first
57 else @quote = Quote.new(:date=>Time.now.to_i) end
63 # handles creates and updates for Quote
65 if params[:quote][:id].to_i > 0 then
66 q = Quote.where(:id=>params[:quote][:id]).first
67 else q = Quote.new(quote_params) end
69 q.date = Time.parse(params[:time]).to_i if params[:time]
70 q.exchange = 'Quotation by SMR User' if q.exchange.empty?
72 if params[:quote][:id].to_i > 0 then
73 q.update(quote_params)
76 # FIXME: bug in rails?
77 # - this :notice flash message it lost when using redirect_to
78 # - it is not lost if the default template is rendered: app/views/assets/create.html.erb
79 #redirect_to assets_path, :notice=>q.errors.full_messages.uniq.join(', ')
80 flash.notice = 'Adding Quote failed: ' + q.errors.full_messages.uniq.join(', ') if not q.errors.empty?
82 if URI(request.referer).path == new_asset_path then
85 else redirect_to :back end
92 # internal helper defining parameters acceptable for Quote create/update
94 params.require(:quote).permit(
95 :id, :id_stock, :date, :quote, :low,
96 :high, :volume, :exchange