implemented Cash positions
[smr.git] / gui / app / controllers / application_controller.rb
blob646bd0b76768bc574f894a5f945b1f6a97b35bdb
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 # provides fundamental means to use SMR
19 class ApplicationController < ActionController::Base
20     before_filter :force_login
21     helper_method :current_user
22     helper_method :smr_menu_addsubitem
24     ##
25     # extend parent constructor with @smr_menu_items, see
26     # smr_menu_addsubitem()
27     #
28     # = NOTE
29     # I don not understand where these symbols come from? However, 
30     # when smr_menu() builds the menu it happens that +:root+
31     # refers to +root_path+ and +:cashflow+ refers to +cashflow_path+
32     # etc...
33     # If one specifies a symbol for the actual method name, like
34     # +:root_path+ that will become translated into +:root_path_path+
35     # and trigger an error 'unknown method'.
36     def initialize
37         super
38         @smr_menu_items = {
39             'assets'=>:root,
40             'cashflow'=>:cashflow_index,
41             'quote records'=>:quoterecords,
42             'figures'=>:figures,
43             'documents'=>:documents,
44             'blog'=>:blog,
45             'objects'=>:objects,
46         }
47     end
49  private
51     ##
52     # redirect to login unless we have a authenticated current_user
53     def force_login 
54       unless current_user or params[:controller].eql?'sessions'
55         redirect_to :controller=>'sessions', :action=>'new'
56       end
57     end
58     
59     ##
60     # find current_user by :auth_token cookie
61     def current_user
62       @current_user ||= User.find_by_password_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
63     end
65     ##
66     # print whatever is given as parameter to the servers output 
67     def debug(*parameters)
68       parameters.each { |p| print 'DEBUG> ', p.inspect }
69     end
71  public   
73     ##
74     # set session parameters that are of global use
75     # - these can be modified through POST from anywhere
76     # 
77     # you may POST these params:
78     #
79     # - smr_browse_date = {"day"=>"23","month"=>"5","year"=>"2010"}
80     #
81     def set_session_params
83         if params[:smr_browse_date] then
84             d=params[:smr_browse_date]
85             session[:smr_browse_date] = Time.new(d['year'],d['month'],d['day']).end_of_day.to_i
86         end
88         if params[:smr_step_date] then
89             case params[:smr_step_date]
90                 when 'day_back'    then session[:smr_browse_date] -= 1.days
91                 when 'day_forward' then session[:smr_browse_date] += 1.days
92             end
93         end
95         redirect_to :back
96     end
98     ##
99     # return date the user is currently browsing, as Time
100     def smr_browse_date
101         session[:smr_browse_date] = Time.now.end_of_day.to_i if session[:smr_browse_date].nil?
102         Time.at(session[:smr_browse_date])
103     end
105     ##
106     # extends @smr_menu_items by adding sub elements to a top level
107     # entry. Note that you can only extend exising entries but not
108     # add your own top level entry.
109     def smr_menu_addsubitem(name, subitems)
110         raise 'subitems must be a Hash' if not subitems.is_a?(Hash)
111         raise 'name must exist in @smr_menu_items' if not @smr_menu_items[name]
112       
113         toplink=@smr_menu_items[name] 
114         @smr_menu_items[name] = subitems
115         @smr_menu_items[name]['_toplink'] = toplink
116     end
118     ##
119     # Collection of securities (of Stock) to display or select from.
120     #
121     # The list is ordered by Stock.name while open positions are prefered
122     # and put in front, see AssetsController.
123     def smr_securities_list
124         securities = Stock.all.order(:name).to_a
125        
126         # put securities held in open positions in front of the list 
127         if session[:open_positions_id_stock].is_a?(Array) then
128             open = Array.new
129             securities.delete_if do |s|
130                 if session[:open_positions_id_stock].include?(s.id) then
131                     open << s
132                     true
133                 else false end
134             end
135             open.sort_by!{|s| s.name}
136             securities = open + securities
137         end
138         securities
139     end
141     ##
142     # Collection of Portfolio objects of current user to display or select
143     # from.
144     def smr_portfolios_list
145         Portfolio.with_open_positions(current_user.id).order(:order).to_a
146     end
148     ##
149     # Returns sanitized page number as set by +params[:page]+.
150     def smr_page
151         JustPaginate.page_value(params[:page])
152     end
154     ##
155     # Paginate a collection if things.
156     def smr_paginate(current_page, collection, items_per_page=20)
157         JustPaginate.paginate(current_page, items_per_page, collection.count) do |range|
158             collection.slice(range)
159         end
160     end