Sha256: 83d413da1524a5132bf492e364d01b5c4e80833208dff42fae8623c7e2b27b8d

Contents?: true

Size: 1.47 KB

Versions: 15

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Maglev
  class ApiController < ::Maglev::ApplicationController
    include Maglev::JsonConcern
    include Maglev::UiLocaleConcern
    include Maglev::ContentLocaleConcern

    before_action :authenticate
    before_action :fetch_maglev_site
    before_action :set_content_locale

    rescue_from ActionController::ParameterMissing, with: :exception_message
    rescue_from ActiveRecord::RecordInvalid, with: :record_errors
    rescue_from ActiveRecord::RecordNotFound, with: :not_found
    rescue_from ActiveRecord::StaleObjectError, with: :stale_record
    rescue_from Maglev::Errors::NotAuthorized, with: :unauthorized

    helper_method :maglev_site, :maglev_theme

    private

    def authenticate
      raise Maglev::Errors::NotAuthorized if session[:maglev_site_id] != maglev_site&.id
    end

    def fetch_maglev_site
      maglev_site # simply force the fetching of the current site
    end

    def maglev_site
      @maglev_site ||= services.fetch_site.call
    end

    def maglev_theme
      @maglev_theme ||= maglev_services.fetch_theme.call
    end

    def record_errors(exception)
      render(json: { errors: exception.record.errors }, status: :bad_request)
    end

    def exception_message(exception)
      render(json: { errors: [exception.message] }, status: :bad_request)
    end

    def not_found
      head :not_found
    end

    def stale_record
      head :conflict
    end

    def unauthorized
      head :unauthorized
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
maglevcms-1.7.2 app/controllers/maglev/api_controller.rb
maglevcms-1.7.1 app/controllers/maglev/api_controller.rb
maglevcms-1.7.0 app/controllers/maglev/api_controller.rb
maglevcms-1.6.1 app/controllers/maglev/api_controller.rb
maglevcms-1.6.0 app/controllers/maglev/api_controller.rb
maglevcms-1.5.1 app/controllers/maglev/api_controller.rb
maglevcms-1.4.0 app/controllers/maglev/api_controller.rb
maglevcms-1.3.0 app/controllers/maglev/api_controller.rb
maglevcms-1.2.2 app/controllers/maglev/api_controller.rb
maglevcms-1.2.1 app/controllers/maglev/api_controller.rb
maglevcms-1.2.0 app/controllers/maglev/api_controller.rb
maglevcms-1.1.7 app/controllers/maglev/api_controller.rb
maglevcms-1.1.6 app/controllers/maglev/api_controller.rb
maglevcms-1.1.5 app/controllers/maglev/api_controller.rb
maglevcms-1.1.4 app/controllers/maglev/api_controller.rb