Sha256: 7980a592b4d20015dc450c4ae6b64960d985edb03655fd4d01c8ce849c33e85b

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 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

Maglev::ApiController = Maglev::APIController

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
maglevcms-1.1.3 app/controllers/maglev/api_controller.rb
maglevcms-1.1.2 app/controllers/maglev/api_controller.rb
maglevcms-1.1.1 app/controllers/maglev/api_controller.rb
maglevcms-1.1.0 app/controllers/maglev/api_controller.rb
maglevcms-1.0.0 app/controllers/maglev/api_controller.rb
maglevcms-1.0.0.rc3 app/controllers/maglev/api_controller.rb
maglevcms-1.0.0.rc2 app/controllers/maglev/api_controller.rb
maglevcms-1.0.0.rc1 app/controllers/maglev/api_controller.rb