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
|
|
|
|
|
|
|
|
# TODO convertir el Pathname en un helper
|
|
|
|
def site_dirs
|
|
|
|
return [] unless Dir.exists? path
|
|
|
|
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,
|
|
|
|
:no_acepta_politicas_de_privacidad)
|
|
|
|
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
|
|
|
|
|
|
|
|
unless File.exists? dir
|
|
|
|
FileUtils.ln_s File.join('..', '..', '_sites', site.name), dir
|
|
|
|
end
|
|
|
|
|
|
|
|
if File.exists? file
|
|
|
|
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
|