2019-03-26 15:32:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-28 14:34:37 +00:00
|
|
|
class Invitadx < ApplicationRecord
|
|
|
|
has_secure_password
|
|
|
|
validates_uniqueness_of :email
|
2019-02-11 19:14:27 +00:00
|
|
|
validate :acepto_politicas_de_privacidad
|
2018-09-28 14:34:37 +00:00
|
|
|
|
|
|
|
after_create :create_invitadx_directory!
|
|
|
|
after_save :add_sites!
|
|
|
|
|
|
|
|
# Para facilitar la serialización de Warden
|
|
|
|
def username
|
|
|
|
email
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
File.join(Rails.root, '_invitadxs', email)
|
|
|
|
end
|
|
|
|
|
2019-03-26 15:32:20 +00:00
|
|
|
# TODO: convertir el Pathname en un helper
|
2018-09-28 14:34:37 +00:00
|
|
|
def site_dirs
|
2019-03-26 15:32:20 +00:00
|
|
|
return [] unless Dir.exist? path
|
|
|
|
|
2018-09-28 14:34:37 +00:00
|
|
|
Pathname.new(path).children.map(&:expand_path).map(&:to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sites
|
|
|
|
@sites ||= site_dirs.map do |site|
|
|
|
|
Site.find(File.basename(site))
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-11 19:14:27 +00:00
|
|
|
def acepto_politicas_de_privacidad
|
|
|
|
unless acepta_politicas_de_privacidad?
|
|
|
|
errors.add(:acepta_politicas_de_privacidad,
|
2019-03-26 15:32:20 +00:00
|
|
|
:no_acepta_politicas_de_privacidad)
|
2019-02-11 19:14:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 14:34:37 +00:00
|
|
|
# Crea el directorio en _sites
|
|
|
|
def create_invitadx_directory!
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Agrega el sitio a las invitadxs y viceversa
|
|
|
|
def add_sites!
|
|
|
|
sites.each do |site|
|
|
|
|
dir = File.join(path, site.name)
|
|
|
|
file = site.invitadxs_file
|
|
|
|
|
2019-03-26 15:32:20 +00:00
|
|
|
unless File.exist? dir
|
2018-09-28 14:34:37 +00:00
|
|
|
FileUtils.ln_s File.join('..', '..', '_sites', site.name), dir
|
|
|
|
end
|
|
|
|
|
2019-03-26 15:32:20 +00:00
|
|
|
if File.exist? file
|
2018-09-28 14:34:37 +00:00
|
|
|
invitadxs = File.read(file).split("\n")
|
|
|
|
invitadxs_orig = invitadxs.dup
|
|
|
|
invitadxs << email unless invitadxs.include? email
|
|
|
|
else
|
|
|
|
invitadxs_orig = []
|
|
|
|
invitadxs = [email]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Solo escribir los cambios si hubo cambios en la lista
|
|
|
|
return if invitadxs_orig == invitadxs
|
|
|
|
|
|
|
|
r = File.open(file, File::RDWR | File::CREAT, 0o640) do |f|
|
|
|
|
# Bloquear el archivo para que no sea accedido por otro
|
|
|
|
# proceso u otra editora
|
|
|
|
f.flock(File::LOCK_EX)
|
|
|
|
|
|
|
|
# Empezar por el principio
|
|
|
|
f.rewind
|
|
|
|
|
|
|
|
# Escribir la fecha de creación
|
|
|
|
f.write(invitadxs.join("\n"))
|
|
|
|
|
|
|
|
# Eliminar el resto
|
|
|
|
f.flush
|
|
|
|
f.truncate(f.pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|