class Authenticatable::Scope

Attributes

controllers[RW]

You can also specify another controller than the default one in case you want to override some controller actions.

name[RW]

The resource name added by the authenticatable-route (for example :users)

path[RW]

You can customize a resource path to something other than the default, including the possibility to change path for I18n:

authenticatable :users, { path: I18n.translate('routes.account') }
path_names[RW]

You can customize a resource’s path_names to something other than the default, including the possibility to change path names for I18n:

authenticatable :users, { path_names: { sign_in: I18n.translate('routes.login') } }
plural_name[RW]

The resource name converted to plural (used to generate controllers etc). This is used as a fallback in case the user defines a resource in plural like:

authenticatable :user
singular_name[RW]

The resource name converted to singluar (used by for example url helpers)

authenticatable :users generates helper methods like:
new_user_session_path and user_signed_in?
skip[RW]

You can also specify controllers you want to skip. For example if you want to disable registrations for an Admin resource.

Public Class Methods

new(resource_name, options = {}) click to toggle source

Create a new Scope resource that can be added to Authenticatable.scopes

# File lib/authenticatable/scope.rb, line 47
def initialize(resource_name, options = {})
  @name = resource_name.to_s
  @singular_name = ActiveSupport::Inflector.singularize(resource_name)
  @plural_name = ActiveSupport::Inflector.pluralize(resource_name)
  @path = options[:path] || default_path
  @path_names = default_path_names.merge(options[:path_names] || {})
  @controllers = default_controllers.merge(options[:controllers] || {})
  @skip = *options[:skip]
  @skip = @skip.map(&:to_sym)
end

Public Instance Methods

classify() click to toggle source
# File lib/authenticatable/scope.rb, line 58
def classify
  ActiveSupport::Inflector.classify @singular_name
end
klass() click to toggle source
# File lib/authenticatable/scope.rb, line 62
def klass
  classify.constantize
end
method_missing(method_name) click to toggle source

Create magic predicates for verifying that a controller exists and hasn’t been skipped for the current scope in routes.rb.

Example

current_scope.registrations? => false

when registrations are skipped in routes:

authenticatable :admins, skip: [:registrations]

rubocop:disable Style/MissingRespondToMissing

Calls superclass method
# File lib/authenticatable/scope.rb, line 76
def method_missing(method_name)
  @controllers.each do |controller, _routes|
    return @skip.exclude?(controller) if method_name.to_s == "#{controller}?"
  end

  super # return NoMethodError
end

Private Instance Methods

default_controllers() click to toggle source
# File lib/authenticatable/scope.rb, line 101
def default_controllers
  {
    sessions: "authenticatable/sessions",
    registrations: "authenticatable/registrations",
    passwords: "authenticatable/passwords"
  }
end
default_path() click to toggle source

rubocop:enable Style/MissingRespondToMissing

# File lib/authenticatable/scope.rb, line 87
def default_path
  @plural_name
end
default_path_names() click to toggle source
# File lib/authenticatable/scope.rb, line 91
def default_path_names
  {
    sign_in: "sign_in",
    sign_up: "sign_up",
    sign_out: "sign_out",
    forgot_password: "forgot_password",
    reset_password: "reset_password"
  }
end