portrait Security and Organization records
[smr.git] / gui / app / models / quoterecord.rb
blob8ff59634090da27e7e1f3b9c9a43f58e18e1a60e
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 class Quoterecord < ActiveRecord::Base
17         belongs_to :User, :foreign_key=>:id_user
18         belongs_to :Security, :foreign_key=>:id_security
19         belongs_to :Quote, :foreign_key=>:id_quote
21     ##
22     # possible values for 'column' field
23     #
24     # keys of this hash are the actual column values while the hash values are
25     # a human readable representation
26     # <b>ATTENTION</b>: the order matters for viewing
27     POSSIBLE_COLUMNS = {
28         'secondary_rally'    => 'Secondary Rally',
29         'natural_rally'      => 'Natural Rally',
30         'upward'             => 'Upward Trend',
31         'downward'           => 'Downward Trend',
32         'natural_reaction'   => 'Natural Reaction',
33         'secondary_reaction' => 'Secondary Reaction',
34     }
36     # data validations
37     validates :id_user, :id_security, :id_quote, presence: true
38     validates :id_user, :id_security, :id_quote, :created, numericality: { greater_than: 0 }
39     validates :column, inclusion: { in: POSSIBLE_COLUMNS.keys, message: "%{value} is not a valid column name" }
41     ##
42     # Wrapper to convert integer date_* column to Time and vice versa.
43     # If :integer is given, the date_* field is updated first. The Time
44     # value is always returned.
45     def time_created=(string)
46         write_attribute(:created, string.to_time)
47     end
48     def time_created
49         Time.at read_attribute(:created)
50     end
52     ##
53     # returns array of possible values for :column field
54     def self.get_columns
55         POSSIBLE_COLUMNS.keys
56     end
58     ##
59     # returns human readable +String+ for given column name, see get_columns()
60     def self.translate_column(column=self.column)
61         POSSIBLE_COLUMNS[column]
62     end
64     ##
65     # tell whether this record has been sorted in to column
66     def is_column?(column_name)
67         self.column == column_name
68     end
70     ##
71     # tell a human what happened
72     def to_s
73         msg = Array.new
74         msg << 'UP hit' if self.is_uphit
75         msg << 'DOWN hit' if self.is_downhit 
76         msg << 'classified quote' if msg.empty?
78         msg << 'on %s' % self.Security.to_s
80         msg << 'into %s column' % self.class.translate_column(self.column)
82         msg << 'which is a pivotal point' if self.is_pivotal_point
83         msg << 'and triggered SIGNAL' if self.is_signal
85         msg.join(' ')
86     end
87 end