Skip to content

Commit

Permalink
Support setting retires_on and retirement_warn for services from dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcculloug committed Feb 15, 2018
1 parent a68a310 commit 2291f38
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions app/models/service/dialog_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ module Service::DialogProperties
def apply_dialog_settings
dialog_options = options[:dialog] || {}

%w(dialog_service_name dialog_service_description).each do |field_name|
send(field_name, dialog_options[field_name]) if dialog_options.key?(field_name)
%w(
name
description
retires_on
retires_in_days
retires_in_hours
).each do |field_name|
dialog_field_name = "dialog_service_#{field_name}"
send(dialog_field_name, dialog_options[dialog_field_name]) if dialog_options.key?(dialog_field_name)
end
end

Expand All @@ -22,4 +29,39 @@ def dialog_service_name(value)
def dialog_service_description(value)
self.description = value if value.present?
end

def dialog_service_retires_on(value)
dialog_service_retire_on_date(Time.zone.parse(value))
end

def dialog_service_retires_in_hours(value)
dialog_service_retire_on_date(Time.zone.now + value.to_i.hours)
end

def dialog_service_retires_in_days(value)
dialog_service_retire_on_date(Time.zone.now + value.to_i.days)
end

def dialog_service_retire_on_date(value)
self.retires_on = value
dialog_service_retirement_warning
end

def dialog_service_retirement_warning
return if retires_on.blank?
dialog_options = options[:dialog] || {}

self.retirement_warn =
if dialog_options.key?('dialog_service_retirement_warn_on')
Time.zone.parse(dialog_options['dialog_service_retirement_warn_on'])
elsif dialog_options.key?('dialog_service_retirement_warn_in_days')
Time.zone.now + dialog_options['dialog_service_retirement_warn_in_days'].to_i.days
elsif dialog_options.key?('dialog_service_retirement_warn_in_hours')
Time.zone.now + dialog_options['dialog_service_retirement_warn_in_hours'].to_i.hours
elsif dialog_options.key?('dialog_service_retirement_warn_offset_days')
retires_on - dialog_options['dialog_service_retirement_warn_offset_days'].to_i.days
elsif dialog_options.key?('dialog_service_retirement_warn_offset_hours')
retires_on - dialog_options['dialog_service_retirement_warn_offset_hours'].to_i.hours
end
end
end

0 comments on commit 2291f38

Please sign in to comment.