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
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 QuoterecordRule < ActiveRecord::Base
17 belongs_to :User, :foreign_key=>'id_user'
20 validates :id_user, :name, :rule, :related_columns, :last_modified, presence: true
21 validates :last_modified, numericality: { greater_than: 0 }
22 validate :related_columns_valid
25 # Wrapper to convert integer date_* column to Time and vice versa.
26 # If :integer is given, the date_* field is updated first. The Time
27 # value is always returned.
28 def time_last_modified=(string)
29 write_attribute(:last_modified, string.to_time)
31 def time_last_modified
32 Time.at read_attribute(:last_modified)
35 # returns :related_columns field as array
36 def get_related_columns
37 if self.related_columns then self.related_columns.split('|')
41 # compact given column names into :related_columns field
42 def set_related_columns(array_of_column_names)
43 self.related_columns = array_of_column_names.uniq.join('|')
49 # custom validator to make sure :related_columns field only talks about
50 # columns a Quoterecord object knows of.
51 def related_columns_valid
52 valid_cols = Quoterecord.get_columns
53 self.get_related_columns.each do |c|
54 if not valid_cols.include?(c) then errors.add(:related_columns, 'a column named "%s" is not accepted by Quoterecord class' % c) end