Sha256: 6ed25bdcc7a48b1bb0daad811f5368884bce7d5b5d6796bca3800703930b58e8

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Janus
  # A collection of abstraction helper methods used in Janus controllers and views.
  # This should be of no particular outside of abstract controllers for Janus that
  # must be working for all scopes at once.
  module InternalHelpers
    extend ActiveSupport::Concern

    included do
      helper_method :janus_scope, :resource, :resource_class, :resource_name
    end

    # Abstract method for the authenticate_scope! before filter, with scope
    # as detected by janus_scope.
    def authenticate!
      send("authenticate_#{janus_scope}!")
    end

    # Detects the scope from the controller name.
    def janus_scope
      @janus_scope ||= self.class.name.split('::', 2).first.underscore.singularize
    end

    # Returns the `@user` instance variable (or `@admin` or whatever),
    # as detected by janus_scope.
    def resource
      instance_variable_get(:"@#{janus_scope}")
    end

    # Sets the `@user` instance variable (or `@admin` or whatever),
    # as detected by janus_scope.
    def resource=(value)
      instance_variable_set(:"@#{janus_scope}", value)
    end

    # Returns the `User` class (or `Admin` or whatever) as detected by
    # janus_scope.
    def resource_class
      @resource_class ||= janus_scope.camelize.constantize
    end

    # Alias for janus_scope.
    def resource_name
      janus_scope
    end

    # Returns the `UserMailer` class (or `AdminMailer` or whatever) as detected
    # by janus_scope.
    def mailer_class
      @mailer_class ||= (janus_scope.camelize + 'Mailer').constantize
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
janus-0.9.0 lib/janus/controllers/internal_helpers.rb
janus-0.8.1 lib/janus/controllers/internal_helpers.rb
janus-0.8.0 lib/janus/controllers/internal_helpers.rb