manage bookmarks, bugfixes
[smr.git] / gui / app / models / quoterecord.rb
blob8cf28fc2f4d97f8ae42128982c0df70323f93c2f
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     include Smr::Extensions::DateTimeWrapper
19         belongs_to :User, :foreign_key=>:id_user
20         belongs_to :Quote, :foreign_key=>:id_quote
21         belongs_to :Security, :foreign_key=>:id_security
22         has_one    :Organization, :through => :Security
24     ##
25     # possible values for 'column' field
26     #
27     # keys of this hash are the actual column values while the hash values are
28     # a human readable representation
29     # <b>ATTENTION</b>: the order matters for viewing
30     POSSIBLE_COLUMNS = {
31         'secondary_rally'    => 'Secondary Rally',
32         'natural_rally'      => 'Natural Rally',
33         'upward'             => 'Upward Trend',
34         'downward'           => 'Downward Trend',
35         'natural_reaction'   => 'Natural Reaction',
36         'secondary_reaction' => 'Secondary Reaction',
37     }
39     # data validations
40     validates :id_user, :id_security, :id_quote, presence: true
41     validates :id_user, :id_security, :id_quote, :date_created, numericality: { greater_than: 0 }
42     validates :column, inclusion: { in: POSSIBLE_COLUMNS.keys, message: "%{value} is not a valid column name" }
44     # define scopes and ransack aliases for finding things
45     ransack_alias :ransackcolumns, :comment_or_Security_brief_or_Security_symbol_or_Organization_name
47     ##
48     # returns array of possible values for :column field
49     def self.get_columns
50         POSSIBLE_COLUMNS.keys
51     end
53     ##
54     # returns human readable +String+ for given column name, see get_columns()
55     def self.translate_column(column=self.column)
56         POSSIBLE_COLUMNS[column]
57     end
59     ##
60     # tell whether this record has been sorted in to column
61     def is_column?(column_name)
62         self.column == column_name
63     end
65     ##
66     # tell a human what happened
67     def to_s
68         msg = Array.new
69         msg << 'UP hit' if self.is_uphit
70         msg << 'DOWN hit' if self.is_downhit 
71         msg << 'classified quote' if msg.empty?
73         msg << 'on %s' % self.Security.to_s
75         msg << 'into %s column' % self.class.translate_column(self.column)
77         msg << 'which is a pivotal point' if self.is_pivotal_point
78         msg << 'and triggered SIGNAL' if self.is_signal
80         msg.join(' ')
81     end
82 end