Files
astuto/app/controllers/tenants_controller.rb

63 lines
1.4 KiB
Ruby
Raw Normal View History

2022-07-18 10:47:54 +02:00
class TenantsController < ApplicationController
include ApplicationHelper
before_action :authenticate_admin, only: [:show, :update]
def new
@page_title = t('signup.page_title')
end
def show
render json: Current.tenant_or_raise!
end
def create
@tenant = Tenant.new
@tenant.assign_attributes(tenant_create_params)
authorize @tenant
ActiveRecord::Base.transaction do
@tenant.save!
Current.tenant = @tenant
@user = User.create!(
full_name: params[:user][:full_name],
email: params[:user][:email],
password: params[:user][:password],
role: "admin"
)
render json: @tenant, status: :created
rescue ActiveRecord::RecordInvalid => exception
render json: { error: exception }, status: :unprocessable_entity
end
end
def update
@tenant = Current.tenant_or_raise!
authorize @tenant
if @tenant.update(tenant_update_params)
render json: @tenant
else
render json: {
error: @tenant.errors.full_messages
}, status: :unprocessable_entity
end
end
private
def tenant_create_params
params
.require(:tenant)
.permit(policy(@tenant).permitted_attributes_for_create)
end
def tenant_update_params
params
.require(:tenant)
.permit(policy(@tenant).permitted_attributes_for_update)
end
end