manage bookmarks, bugfixes
[smr.git] / gui / app / controllers / application_controller.rb
blob96b4b65cb672a712d0fef6e07f498c8962c611cf
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             'workdesk'      => :workdesk_index,
42             'watchlist'     => :watchlist_index,
43             'report'        => :report_index,
44             'quote records' => :quoterecords,
45             'figures'       => :figures,
46             'blog'          => :blog_index,
47             'documents'     => :documents,
48             'objects'       => :objects,
49         }
50     end
52  private
54     ##
55     # redirect to login unless we have a authenticated current_user
56     def force_login 
57       unless current_user or params[:controller].eql?'sessions'
58         redirect_to new_session_path
59       end
60     end
62     ##
63     # find current_user by :auth_token cookie
64     def current_user
65       @current_user ||= User.find_by_password_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
66     end
68  public 
70     ##
71     # set session parameters that are of global use
72     # - these can be modified through POST from anywhere
73     # 
74     # you may POST these params:
75     #
76     # - smr_browse_date = "YYYY-MM-DD"
77     # - smr_step_date   = "day_back" or "day_forward"
78     #
79     def set_session_params
81         if params[:smr_browse_date] then
82             session[:smr_browse_date] = Time.parse(params[:smr_browse_date]).end_of_day.to_i
83         end
85         if params[:smr_step_date] then
86             case params[:smr_step_date]
87                 when 'day_back'    then session[:smr_browse_date] -= 1.days
88                 when 'day_forward' then session[:smr_browse_date] += 1.days
89             end
90         end
92         redirect_to :back
93     end
95     ##
96     # return date the user is currently browsing, as Time
97     def smr_browse_date
98         session[:smr_browse_date] = Time.now.end_of_day.to_i if session[:smr_browse_date].nil?
99         Time.at(session[:smr_browse_date])
100     end
102     ##
103     # extends @smr_menu_items by adding sub elements to a top level
104     # entry. Note that you can only extend exising entries but not
105     # add your own top level entry.
106     def smr_menu_addsubitem(name, subitems)
107         raise 'subitems must be a Hash' if not subitems.is_a?(Hash)
108         raise 'name must exist in @smr_menu_items' if not @smr_menu_items[name]
109       
110         toplink=@smr_menu_items[name] 
111         @smr_menu_items[name] = subitems
112         @smr_menu_items[name]['_toplink'] = toplink
113     end
115     ##
116     # Collection of Securitiy names and ids for display or select forms.
117     # Note: the Smr::ID_CASH_SECURITY is dropped from here.
118     def smr_securities_list
119         securities = Security.is_actively_traded(smr_browse_date).to_a
120         securities.sort!
121     end
123     ##
124     # Collection of Organization names and ids for display or select forms.
125     #
126     # Note: the Smr::ID_UNIVERSE_ORGANIZATION is dropped by default as its as
127     # it counts as 'not yet organized'. Use the :drop_universe option to alter
128     # that.
129     def smr_organizations_list(options={:drop_universe=>true})
130         collection = if options[:drop_universe] then
131             Organization.where.not(:id=>Smr::ID_UNIVERSE_ORGANIZATION)
132         else Organization end
133         collection.order(:name=>:desc).to_a
134     end
136     ##
137     # Collection of Portfolio objects of current user to display or select
138     # from.
139     #
140     # By default, only those having open positions in it will be shown. Use the
141     # options to change that behaviour.
142     def smr_portfolios_list(options={:show_all=>false})
143         if options[:show_all] then
144             Portfolio.where(:id_user=>current_user.id).order(:order).to_a
145         else
146             Portfolio.with_open_positions(current_user.id).order(:order).to_a
147         end
148     end
150     ##
151     # Collection of Security.id numbers held in open positions by the current
152     # user.
153     #
154     # This is cached in the session. Either some code fills the cache by giving
155     # :fill_initially or this method takes care of filling it (expensive).
156     #
157     # :fill_initially should be a collection of Smr::AssetsPosition.
158     def smr_securities_in_open_positions_list(fill_initially=false)
159         if fill_initially then
160            session[:smr_securities_in_open_positions_cache] = fill_initially.collect{ |p| p.security.id }.uniq
161         end        
163         if not session[:smr_securities_in_open_positions_cache].is_a?(Array) then
164             assets = Smr::Asset.new(current_user.id, smr_browse_date)
165             session[:smr_securities_in_open_positions_cache] = assets.open_positions.collect{ |p| p.security.id }.uniq
166         end
167         session[:smr_securities_in_open_positions_cache]
168     end
170     ##
171     # Returns sanitized page number as set by +params[:page]+.
172     def smr_page
173         JustPaginate.page_value(params[:page])
174     end
176     ##
177     # Paginate a collection if things.
178     def smr_paginate(current_page, collection, items_per_page=20)
179         JustPaginate.paginate(current_page, items_per_page, collection.count) do |range|
180             collection.slice(range)
181         end
182     end