manage bookmarks, bugfixes
[smr.git] / gui / app / models / security_metal.rb
blobd0d7773847ca8bb690136996911a3b0507c1f28e
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/>.
17 class SecurityMetal < ActiveRecord::Base
18     include Smr::Extensions::HelperMethods
19     include Smr::Extensions::SecurityTypemodelMandatoryMethods
21     self.inheritance_column = :none
22         has_one :Security, :foreign_key=>:id_security_metal, :inverse_of=>:SecurityMetal
24     ##
25     # types of metals supported (may alter behaviour of some methods)
26     # NOTE: do NOT change the relations! just ADD!
27     enum type: { :base=>0, :precious=>1, :strategic=>2  }
29     # data validations
30     #validates :id_security, numericality: { greater_than: 0 }
32     ##
33     # Returns human readable String describing :type. It may be given as
34     # symbol, as string or as numerical index.
35     def SecurityMetal.describe_type(lookup)
36         descriptions = {
37             :base        => 'Base Metal',
38             :precious    => 'Precious Metal',
39             :strategic   => 'Strategic Metal',
40         }
42         if lookup.is_a?(Numeric) then
43             lookup = self.types.key(lookup) || ''
44         end
45         descriptions[lookup.to_sym]
46     end
48     ##
49     # Describes type of the instance object.
50     def describe_type
51         SecurityMetal.describe_type(type)
52     end
54     ##
55     # Returns humanreadable String describing the model itself.
56     def SecurityMetal.describe
57         'Industrialized Metal'
58     end
59     def describe
60         SecurityMetal.describe
61     end
63     ##
64     # Returns a collection of all types in their described form.
65     #
66     # Useful for Select boxes in forms, also see SecurityMetal#describe_type. The
67     # :as_number option toggles whether symbols or integers are retured as
68     # index.
69     def self.types_for_form(options={ :as_number=>false })
70         SecurityMetal.types.map { |t|
71             [
72                 SecurityMetal.describe_type(t.first),
73                 options[:as_number] ? SecurityMetal.types[t.first] : t.first
74             ]
75         }
76     end
78     ##
79     # Human readable String composed of essential properties known by a
80     # SecurityMetal on its own. Useful as part to compose the name of a
81     # Security.
82     def to_s
83         [
84           (self.Security.id_organization != Smr::ID_UNIVERSE_ORGANIZATION ? self.Security.brief : nil),
85           SecurityMetal.describe_type(type),
86         ].compact.join(' ')
87     end
89 end