Class: StandardId::ScopeConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_id/scope_config.rb

Constant Summary collapse

DEPRECATOR =

Shared deprecator instance. Creating a new ActiveSupport::Deprecation on every extract_profile_types call bypasses the host app’s configured deprecation behaviour (Rails 7.1+ routes through deprecation registries) and allocates for every scope init. One instance is enough.

ActiveSupport::Deprecation.new("2.0", "StandardId")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config = {}) ⇒ ScopeConfig

Returns a new instance of ScopeConfig.



56
57
58
59
60
61
62
63
64
# File 'lib/standard_id/scope_config.rb', line 56

def initialize(name, config = {})
  @name = name.to_sym
  @profile_types = self.class.extract_profile_types(config)
  @after_sign_in_path = config[:after_sign_in_path]
  @no_profile_message = config[:no_profile_message] || default_no_profile_message
  @label = config[:label] || name.to_s.humanize
  @allow_registration = config.fetch(:allow_registration, true)
  @authorizer = config[:authorizer]
end

Instance Attribute Details

#after_sign_in_pathObject (readonly)

Returns the value of attribute after_sign_in_path.



15
16
17
# File 'lib/standard_id/scope_config.rb', line 15

def 
  @after_sign_in_path
end

#allow_registrationObject (readonly)

Reserved for future use — controls whether new accounts can register under this scope.



15
16
17
18
19
20
21
# File 'lib/standard_id/scope_config.rb', line 15

attr_reader :name,
:profile_types,
:after_sign_in_path,
:no_profile_message,
:label,
:allow_registration,
:authorizer

#authorizerObject (readonly)

Returns the value of attribute authorizer.



15
16
17
18
19
20
21
# File 'lib/standard_id/scope_config.rb', line 15

attr_reader :name,
:profile_types,
:after_sign_in_path,
:no_profile_message,
:label,
:allow_registration,
:authorizer

#labelObject (readonly)

Returns the value of attribute label.



15
16
17
# File 'lib/standard_id/scope_config.rb', line 15

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/standard_id/scope_config.rb', line 15

def name
  @name
end

#no_profile_messageObject (readonly)

Returns the value of attribute no_profile_message.



15
16
17
# File 'lib/standard_id/scope_config.rb', line 15

def no_profile_message
  @no_profile_message
end

#profile_typesObject (readonly)

Array of profile-type class names accepted by this scope. Any profile matching any of these types satisfies the built-in profile check.



15
16
17
18
19
20
21
# File 'lib/standard_id/scope_config.rb', line 15

attr_reader :name,
:profile_types,
:after_sign_in_path,
:no_profile_message,
:label,
:allow_registration,
:authorizer

Class Method Details

.extract_profile_types(config) ⇒ Object

Normalize profile-type inputs from config.

Accepts:

- :profile_types (plural) — array of strings (preferred).
- :profile_type  (singular) — single string, retained for back-compat. Emits a
  deprecation warning when present.

Returns an Array<String> (possibly empty).



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/standard_id/scope_config.rb', line 37

def self.extract_profile_types(config)
  plural = config[:profile_types]
  singular = config[:profile_type]

  if singular && plural
    raise ArgumentError, "Scope config cannot set both :profile_type and :profile_types — use :profile_types"
  end

  if singular
    DEPRECATOR.warn(
      "StandardId scope config key :profile_type is deprecated and will be removed in v2.0. " \
        "Use :profile_types (an Array of profile-type strings) instead."
    )
    return Array(singular).map(&:to_s).reject(&:blank?)
  end

  Array(plural).map(&:to_s).reject(&:blank?)
end

Instance Method Details

#accepts_profile_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/standard_id/scope_config.rb', line 76

def accepts_profile_type?(type)
  return false if type.blank?
  @profile_types.include?(type.to_s)
end

#authorizer?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/standard_id/scope_config.rb', line 81

def authorizer?
  authorizer.respond_to?(:call)
end

#profile_typeObject

Back-compat accessor. Returns the first configured profile type (or nil). Prefer #profile_types for new code — a scope may accept more than one type.



68
69
70
# File 'lib/standard_id/scope_config.rb', line 68

def profile_type
  @profile_types.first
end

#requires_profile?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/standard_id/scope_config.rb', line 72

def requires_profile?
  @profile_types.any?
end