Sha256: 9b37d4828ccba2b8d0befeeba2fb89107c61185436629646dec5f296f1f0e76b

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 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?, :rights?
    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!
      unless signed_in?
        store_location
        raise LoginRequired
      end
    end

    def can?(action, element, resource=nil)
      ControllerResource.new(self, action: action).authorized?(:can?, element, resource)
    end

    def rights?(action, element, resource=nil)
      ControllerResource.new(self, action: action).authorized?(:rights?, element, resource)
    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

    # Store the current url in a cookie
    # 
    # * *Args*    :
    # 
    # * *Returns* :
    #
    def store_location
      cookies[:return_to] = request.fullpath if request.get?
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
adeia-0.10.5 lib/adeia/controller_methods.rb
adeia-0.10.4 lib/adeia/controller_methods.rb
adeia-0.10.3 lib/adeia/controller_methods.rb
adeia-0.10.2 lib/adeia/controller_methods.rb
adeia-0.10.1 lib/adeia/controller_methods.rb