2012-12-07 11:11:12 +00:00
|
|
|
module Modules
|
|
|
|
module Models
|
|
|
|
module TimeLiving
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
validates :time_living, numericality: {
|
|
|
|
only_integer: true
|
|
|
|
}, presence: true
|
2012-12-07 11:11:12 +00:00
|
|
|
|
|
|
|
validate lambda {
|
2013-07-24 15:43:41 +01:00
|
|
|
# MIN_TIME_LIVING <= time_living <= MAX_TIME_LIVING or
|
2012-12-07 11:11:12 +00:00
|
|
|
# 2 min <= time_living <= 12 hours
|
2013-07-24 15:43:41 +01:00
|
|
|
# time_living in seconds
|
|
|
|
min = self.class.const_defined?(:MIN_TIME_LIVING) ? self.class::MIN_TIME_LIVING : 120
|
|
|
|
max = self.class.const_defined?(:MAX_TIME_LIVING) ? self.class::MAX_TIME_LIVING : 43200
|
|
|
|
if min > time_living.to_i || time_living.to_i > max
|
|
|
|
errors.add :time_living, I18n.t('flash.time_living.numericality_error',
|
2014-01-21 04:51:49 +00:00
|
|
|
min: (min / 60),
|
|
|
|
max: (max / 60)
|
2013-07-24 15:43:41 +01:00
|
|
|
)
|
2012-12-07 11:11:12 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
before_validation :convert_time_living
|
|
|
|
|
|
|
|
attr_accessible :time_living
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def convert_time_living
|
|
|
|
self.time_living = time_living.to_i * 60 if time_living_was.to_i != time_living.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|