Sha256: b8a5c77942979eb0a47ab7416054b8e7104a80249daf389abcca8777250bf0c8

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'adeia/controller_resource'
require 'adeia/exceptions'

module Adeia

  module ControllerMethods

    module ClassMethods

      def load_and_authorize(**args)
        ControllerResource.add_before_filter(self, :load_resource_or_records_and_authorize, **args)
      end

      def require_login(**args)
        ControllerResource.add_before_filter(self, :require_login, **args)
      end

    end

    def self.included(base)
      base.extend ClassMethods
      base.helper_method :can?
    end

    def load_and_authorize!(**args)
      controller_resource = ControllerResource.new(self, **args)
      resource = controller_resource.load_resource
      controller_resource.authorize!
      return resource
    end

    def authorize_and_load_records!(**args)
      controller_resource = ControllerResource.new(self, **args)
      controller_resource.check_permissions!
      return controller_resource.load_records
    end

    def authorize!(**args)
      ControllerResource.new(self, **args).authorize!
    end

    def require_login!
      raise LoginRequired unless signed_id?
    end

    def can?(action, controller=nil, resource=nil)
      args = { action: action, controller: controller, resource: resource }
      ControllerResource.new(self, **args).can?
    end

    # Redirect the user to the stored url or the default one provided
    # 
    # * *Args*    :
    #   - default path to redirect to
    # * *Returns* :
    #
    def redirect_back_or(default, message = nil)
      redirect_to(cookies[:return_to] || default, message)
      cookies.delete(:return_to)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adeia-0.6.0 lib/adeia/controller_methods.rb
adeia-0.5.6 lib/adeia/controller_methods.rb