wedding-planner/app/controllers/application_controller.rb

54 lines
1.4 KiB
Ruby
Raw Normal View History

2024-10-27 21:42:45 +00:00
# Copyright (C) 2024 Manuel Bustillo
2024-07-11 18:42:31 +02:00
class ApplicationController < ActionController::Base
before_action :set_tenant
2024-11-30 11:03:29 +01:00
before_action :authenticate_user!
after_action :set_csrf_cookie
skip_before_action :verify_authenticity_token, if: :development_swagger?
rescue_from ActiveRecord::RecordInvalid do |exception|
render json: {
message: 'Record invalid',
errors: exception.record.errors.full_messages
}, status: :unprocessable_entity
end
rescue_from ActionController::ParameterMissing do |exception|
render json: {
message: 'Parameter missing',
errors: [exception.message]
}, status: :bad_request
end
rescue_from ActiveRecord::RecordNotFound do |exception|
render json: {
message: 'Record not found',
errors: [exception.message]
}, status: :not_found
end
private
def default_url_options(options = {})
options.merge(path_params: { slug: ActsAsTenant.current_tenant&.slug })
end
def set_tenant
ActsAsTenant.current_tenant = Wedding.find_by(slug: params[:slug])
end
def development_swagger?
Rails.env.test? ||
Rails.env.development? && request.headers['referer']&.include?('/api-docs/index.html')
end
def set_csrf_cookie
cookies['csrf-token'] = {
value: form_authenticity_token,
secure: Rails.env.production?,
same_site: :strict
}
end
2024-07-11 18:42:31 +02:00
end