organized code into Smr module
[smr.git] / gui / app / controllers / application_controller.rb
blob9c9b3c48716d0097d96735698b5b954916afdab2
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
39         @smr_menu_items = {
40             'assets'=>:root,
41             'cashflow'=>:cashflow_index,
42             'quote records'=>:quoterecords,
43             'figures'=>:figures,
44             'documents'=>:documents,
45             'blog'=>:blog,
46             'objects'=>:objects,
47         }
48     end
50  private
52     ##
53     # redirect to login unless we have a authenticated current_user
54     def force_login 
55       unless current_user or params[:controller].eql?'sessions'
56         redirect_to :controller=>'sessions', :action=>'new'
57       end
58     end
59     
60     ##
61     # find current_user by :auth_token cookie
62     def current_user
63       @current_user ||= User.find_by_password_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
64     end
66     ##
67     # print whatever is given as parameter to the servers output 
68     def debug(*parameters)
69       parameters.each { |p| print 'DEBUG> ', p.inspect }
70     end
72  public   
74     ##
75     # set session parameters that are of global use
76     # - these can be modified through POST from anywhere
77     # 
78     # you may POST these params:
79     #
80     # - smr_browse_date = {"day"=>"23","month"=>"5","year"=>"2010"}
81     #
82     def set_session_params
84         if params[:smr_browse_date] then
85             d=params[:smr_browse_date]
86             session[:smr_browse_date] = Time.new(d['year'],d['month'],d['day']).end_of_day.to_i
87         end
89         if params[:smr_step_date] then
90             case params[:smr_step_date]
91                 when 'day_back'    then session[:smr_browse_date] -= 1.days
92                 when 'day_forward' then session[:smr_browse_date] += 1.days
93             end
94         end
96         redirect_to :back
97     end
99     ##
100     # return date the user is currently browsing, as Time
101     def smr_browse_date
102         if session[:smr_browse_date].nil? then Time.new
103         else Time.at(session[:smr_browse_date]) end
104     end
106     ##
107     # extends @smr_menu_items by adding sub elements to a top level
108     # entry. Note that you can only extend exising entries but not
109     # add your own top level entry.
110     def smr_menu_addsubitem(name, subitems)
111         raise 'subitems must be a Hash' if not subitems.is_a?(Hash)
112         raise 'name must exist in @smr_menu_items' if not @smr_menu_items[name]
113       
114         toplink=@smr_menu_items[name] 
115         @smr_menu_items[name] = subitems
116         @smr_menu_items[name]['_toplink'] = toplink
117     end
119     ##
120     # Collection of securities (of Stock) to display or select from.
121     #
122     # The list is ordered by Stock.name while open positions are prefered
123     # and put in front, see AssetsController.
124     def smr_securities_list
125         securities = Stock.all.order(:name).to_a
126        
127         # put securities held in open positions in front of the list 
128         if session[:open_positions_id_stock].is_a?(Array) then
129             open = Array.new
130             securities.delete_if do |s|
131                 if session[:open_positions_id_stock].include?(s.id) then
132                     open << s
133                     true
134                 else false end
135             end
136             open.sort_by!{|s| s.name}
137             securities = open + securities
138         end
139         securities
140     end
142     ##
143     # Returns sanitized page number as set by +params[:page]+.
144     def smr_page
145         JustPaginate.page_value(params[:page])
146     end
148     ##
149     # Paginate a collection if things.
150     def smr_paginate(current_page, collection, items_per_page=20)
151         JustPaginate.paginate(current_page, items_per_page, collection.count) do |range|
152             collection.slice(range)
153         end
154     end