75 lines
2.1 KiB
Ruby
75 lines
2.1 KiB
Ruby
class InstanceController < ApplicationController
|
|
def create
|
|
zone = create_instance_params
|
|
public_id = Nanoid.generate(size: 6)
|
|
name = Spicy::Proton.pair(" ")
|
|
password = Nanoid.generate(size: 4, alphabet: "0123456789")
|
|
instance = Instance.new(zone: zone, public_id: public_id, name: name, password: password)
|
|
if instance.save
|
|
@id = instance.public_id
|
|
@password = instance.password
|
|
render "set_password"
|
|
end
|
|
end
|
|
|
|
def show
|
|
@instance = Instance.includes(:pops, :fairies).find_by(public_id: show_instance_params)
|
|
if @instance
|
|
@forecast = Weather.forecast(@instance.zone.to_sym)
|
|
else
|
|
redirect_to root_path
|
|
end
|
|
end
|
|
|
|
def pop
|
|
instance_id, nm, pwd = pop_instance_params
|
|
parent_instance = Instance.find_by(public_id: instance_id)
|
|
if parent_instance.password == pwd
|
|
pop = Pop.new(instance_id: parent_instance.id, name: nm)
|
|
if pop.save
|
|
@instance = Instance.includes(:pops, :fairies).find_by(public_id: instance_id)
|
|
@forecast = Weather.forecast(@instance.zone.to_sym)
|
|
render partial: "list", locals: { instance: @instance, forecast: @forecast }
|
|
end
|
|
end
|
|
end
|
|
|
|
def reset
|
|
instance_id, nm, pwd = pop_instance_params
|
|
parent_instance = Instance.find_by(public_id: instance_id)
|
|
if parent_instance.password == pwd
|
|
Pop.delete_by(instance_id: parent_instance.id, name: nm)
|
|
@instance = Instance.includes(:pops, :fairies).find_by(public_id: instance_id)
|
|
@forecast = Weather.forecast(@instance.zone.to_sym)
|
|
render partial: "list", locals: { instance: @instance, forecast: @forecast }
|
|
end
|
|
end
|
|
|
|
def authenticate
|
|
password, instance = auth_params
|
|
inst = Instance.find_by(public_id: instance)
|
|
if password == inst.password
|
|
@id = inst.public_id
|
|
@password = inst.password
|
|
render "set_password"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def create_instance_params
|
|
params.expect(:zone)
|
|
end
|
|
|
|
def show_instance_params
|
|
params.expect(:public_id)
|
|
end
|
|
|
|
def pop_instance_params
|
|
params.expect(:instance, :nm, :pwd)
|
|
end
|
|
|
|
def auth_params
|
|
params.expect(:password, :instance)
|
|
end
|
|
end
|