2014-03-11 08:58:36 +00:00
|
|
|
module TimeLiving
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
|
|
|
|
validates :time_living, numericality: { only_integer: true }, presence: true
|
|
|
|
|
|
|
|
validate -> {
|
|
|
|
# MIN_TIME_LIVING <= time_living <= MAX_TIME_LIVING or
|
|
|
|
# 2 min <= time_living <= 12 hours
|
|
|
|
# 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', min: (min / 60), max: (max / 60))
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
before_validation :convert_time_living
|
2015-04-27 22:16:35 +01:00
|
|
|
# attr_accessible :time_living
|
2014-03-11 08:58:36 +00:00
|
|
|
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
|