Updated Gems, improved quote retrieval
[smr.git] / gui / app / controllers / quoterecords_controller.rb
blob39499b1b70b0bfda2aa44ff630de9879961fc7ea
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/>.
16 require 'quoterecords'
19 # Quote Records are a way of ordering market quotes into trends and putting
20 # personal notes on them. Interpretation of events that is.
21 class QuoterecordsController < ApplicationController
23     ##
24     # Obtains SmrQuoterecords collection.
25     def index
26         smr_menu_addsubitem('quote records', {
27             'rules'=>:quoterecord_rules,
28             '+ quote'=>:new_quoterecord,
29         })
31         @securities = smr_securities_list
32         if params[:id_security] and params[:id_security].to_i>1 then
33             session[:quoterecords_security_index] = @securities.index{|s|s.id==params[:id_security].to_i}
34         else i = 0 end
36         case params[:show]
37             when 'previous' then session[:quoterecords_security_index] -= 1
38             when 'next' then session[:quoterecords_security_index] += 1
39         end
40         session[:quoterecords_security_index]  = 0 if not (0..@securities.count-1) === session[:quoterecords_security_index]
41         @selected_security = @securities[ session[:quoterecords_security_index]  ]
43         @intraday_quotes = Quote.where(:id_security=>@selected_security.id, :date=>smr_browse_date.beginning_of_day.to_i..smr_browse_date.end_of_day.to_i).order(date: :desc)
44         @quoterecords, @total_pages = smr_paginate(
45             @page = smr_page,
46             Smr::Quoterecords.new(current_user.id, @selected_security, smr_browse_date)
47         )
48         @sensitivity = QuoterecordThreshold.where(:id_user=>current_user.id, :id_security=>@selected_security.id).first || (QuoterecordThreshold.new(:id_security=>@selected_security.id))
50         @possible_columns = Hash.new
51         Quoterecord.get_columns.collect do |c|
52             @possible_columns[Quoterecord.translate_column(c)] = c
53         end
54     end
56     ##
57     # handle new Quote data
58     def new
59         self.index
60         @quote = Quote.new(:date=>Time.now.to_i)
61         render :index
62     end
64     ##
65     # handles creates and updates
66     def create
67         if params[:quoterecord][:id].to_i > 0 then
68             qr = Quoterecord.where(:id=>params[:quoterecord][:id], :id_user=>current_user.id).first
69             qr.update(quoterecord_params)
70         else
71             qr = Quoterecord.new(quoterecord_params)
72             qr.id_user = current_user.id
73             qr.created = Time.now.to_i
74             qr.save!
75         end
77         index
78         render :index
79     end
81     ##
82     # present exiting Quoterecord for editing.
83     def show
84         index
85         @quoterecord = Quoterecord.where(:id=>params[:id], :id_user=>current_user.id).first
86         render :index
87     end
89     ##
90     # View QuoterecordRule.
91     def view_rules
92         redirect_to :back
93     end
96     protected
98     ##
99     # internal helper defining parameters acceptable for quoterecord
100     def quoterecord_params
101       params.require(:quoterecord).permit(
102         :id_security, :id_quote, :column, :comment, :is_pivotal_point, :is_uphit,
103         :is_downhit, :is_signal
104       )
105     end