internetee-registry/app/models/concerns/domain/releasable.rb
2021-05-18 11:26:23 +03:00

53 lines
1.6 KiB
Ruby

module Domain::Releasable
extend ActiveSupport::Concern
class_methods do
def release_domains
releasable_domains.each do |domain|
domain.release
yield domain if block_given?
end
end
private
def releasable_domains
if release_to_auction
where('(delete_date <= ? OR force_delete_date <= ?)' \
' AND ? != ALL(coalesce(statuses, array[]::varchar[]))',
Time.zone.today,
Time.zone.today,
DomainStatus::SERVER_RELEASE_PROHIBITED)
else
where('(delete_date <= ? OR force_delete_date <= ?)' \
' AND ? != ALL(coalesce(statuses, array[]::varchar[])) AND' \
' ? != ALL(COALESCE(statuses, array[]::varchar[]))',
Time.zone.today,
Time.zone.today,
DomainStatus::SERVER_RELEASE_PROHIBITED,
DomainStatus::DELETE_CANDIDATE)
end
end
end
included do
class_attribute :release_to_auction
self.release_to_auction = ENV['release_domains_to_auction'] == 'true'
end
def release
if release_to_auction
ToStdout.msg 'Destroying domain'
destroy!
ToStdout.msg "Checking if domain_name is auctionable: #{domain_name.auctionable?}"
domain_name.sell_at_auction if domain_name.auctionable?
ToStdout.msg 'Sending registrar notification'
registrar.notifications.create!(text: "#{I18n.t(:domain_deleted)}: #{name}",
attached_obj_id: id,
attached_obj_type: self.class)
else
discard
end
end
end