Module: FriendlyId::Disabler

Defined in:
lib/friendly_id/disabler.rb

Overview

The Disabler module manages the temporary disabling of FriendlyId functionality by utilizing a thread-local variable. This module provides methods to check the current state of FriendlyId (enabled/disabled) and to execute blocks with FriendlyId disabled.

Constant Summary collapse

THREAD_LOCAL_KEY =

A key used in Thread.current to store the state of FriendlyId (enabled/disabled).

:__friendly_id_enabler_disabled

Class Method Summary collapse

Class Method Details

.disable_friendly_id { ... } ⇒ Object

Temporarily disables FriendlyId for the duration of the given block.

This method sets a thread-local flag that is checked by the FriendlyId::Slugged#to_param method. It ensures that the disabling is scoped only to the current thread of execution.

Yields:

  • the block of code during which FriendlyId should be disabled.



26
27
28
29
30
31
32
# File 'lib/friendly_id/disabler.rb', line 26

def disable_friendly_id
  old_value = Thread.current[THREAD_LOCAL_KEY]
  Thread.current[THREAD_LOCAL_KEY] = true
  yield
ensure
  Thread.current[THREAD_LOCAL_KEY] = old_value
end

.disabled? ⇒ Boolean

Checks if FriendlyId is currently disabled.



16
17
18
# File 'lib/friendly_id/disabler.rb', line 16

def disabled?
  !!Thread.current[THREAD_LOCAL_KEY]
end