class Authenticatable::Scope
Attributes
You can also specify another controller than the default one in case you want to override some controller actions.
The resource name added by the authenticatable-route (for example :users)
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') }
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') } }
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
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?
You can also specify controllers you want to skip. For example if you want to disable registrations for an Admin resource.
Public Class Methods
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
# File lib/authenticatable/scope.rb, line 58 def classify ActiveSupport::Inflector.classify @singular_name end
# File lib/authenticatable/scope.rb, line 62 def klass classify.constantize end
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
# 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
# File lib/authenticatable/scope.rb, line 101 def default_controllers { sessions: "authenticatable/sessions", registrations: "authenticatable/registrations", passwords: "authenticatable/passwords" } end
rubocop:enable Style/MissingRespondToMissing
# File lib/authenticatable/scope.rb, line 87 def default_path @plural_name end
# 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