activeadmin_friendly_id_disabler 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +69 -0
- data/lib/activeadmin/friendly_id_disabler.rb +32 -0
- data/lib/activeadmin_friendly_id_disabler/version.rb +5 -0
- data/lib/activeadmin_friendly_id_disabler.rb +11 -0
- data/lib/friendly_id/disabler.rb +47 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bde44090af3a0c7f166d64dc3b4fbedab9754367cc7b9e3ebcc7349117adfa07
|
4
|
+
data.tar.gz: 92c723789faba3b5cf6dea6a472425a1bed7048ebc29d0384d45d8009bd48e27
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89a5af8bd0d6bc09ea13d866deb0e623e6e186e8a82013a6de379770c2148e18b2fc10d95aa457fb04d9a0524b52678fc7ccb64cc9f43d23d8e478196e7521c4
|
7
|
+
data.tar.gz: 8d73421e4e80c36a3cfbf69a8ccba6b9362c6e1351f63287ad2a066754f1fd61935df8499b7a10d2a7ab5d4bcddb5a9ee8f94451ee370652f9806abf2b3b1054
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Alexey Gordienko
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# ActiveAdmin FriendlyId Disabler
|
2
|
+
|
3
|
+
`activeadmin_friendly_id_disabler` is a Ruby gem designed to selectively disable the `FriendlyId` behavior for ActiveAdmin environments.
|
4
|
+
This allows applications to use standard IDs in URLs instead of slugs, which can help avoid potential conflicts and improve compatibility with certain frameworks.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
- Temporarily disables `FriendlyId` functionality in ActiveAdmin controllers.
|
9
|
+
- Automatically reverts to standard ID usage within the specified context.
|
10
|
+
- Integrated using thread-local variables to ensure thread safety.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
1. **Add the gem to your Gemfile:**
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'activeadmin_friendly_id_disabler'
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
2. **Bundle Install:**
|
22
|
+
|
23
|
+
Run the following command to install the gem:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
bundle install
|
27
|
+
|
28
|
+
```
|
29
|
+
|
30
|
+
3. **Create an initializer:**
|
31
|
+
|
32
|
+
You need to configure your Rails application to use this module in ActiveAdmin controllers by creating an initializer. Create a file `config/initializers/activeadmin_friendly_id_disabler.rb`:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Rails.application.config.after_initialize do
|
36
|
+
ActiveAdmin::BaseController.include(ActiveAdmin::FriendlyIdDisabler)
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
This code ensures that the `FriendlyId` is disabled for ActiveAdmin actions after the application initializes.
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
Once you have installed the gem and set up the initializer, `FriendlyId` will be disabled in the context of ActiveAdmin controllers. This means any URLs generated will use the standard record IDs instead of slugs, preventing issues where slugs may not be necessary or desirable.
|
46
|
+
|
47
|
+
## Example
|
48
|
+
|
49
|
+
Here's a simple example of a `to_param` override within your models:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Post < ApplicationRecord
|
53
|
+
extend FriendlyId
|
54
|
+
friendly_id :title, use: :slugged
|
55
|
+
end
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
## Configuration
|
60
|
+
|
61
|
+
By default, the gem will automatically disable `FriendlyId` only for the duration of an action within ActiveAdmin controllers. If you need more granular control or need to disable `FriendlyId` in other parts of your application, consider implementing additional custom logic.
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/gordienko/activeadmin_friendly_id_disabler](https://github.com/gordienko/activeadmin_friendly_id_disabler).
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
The gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The FriendlyIdDisabler module is designed to be used in ActiveAdmin controllers
|
4
|
+
# to temporarily disable FriendlyId's functionality during specific actions.
|
5
|
+
# This is especially useful when dealing with ActiveAdmin, which may not require
|
6
|
+
# the friendly slugs that FriendlyId generates.
|
7
|
+
#
|
8
|
+
# To use this module, include it in the ActiveAdmin controller context where you
|
9
|
+
# want to disable FriendlyId.
|
10
|
+
|
11
|
+
module ActiveAdmin
|
12
|
+
# This module disables FriendlyId for the duration of the specified action.
|
13
|
+
module FriendlyIdDisabler
|
14
|
+
extend ActiveSupport::Concern
|
15
|
+
|
16
|
+
included do
|
17
|
+
# Prepend an around_action filter to disable FriendlyId when the action is run.
|
18
|
+
prepend_around_action :disable_friendly_id
|
19
|
+
end
|
20
|
+
|
21
|
+
# Temporarily disables FriendlyId functionality within the given block.
|
22
|
+
#
|
23
|
+
# @param action [Proc] the block of code (typically a controller action) during which
|
24
|
+
# FriendlyId should be disabled.
|
25
|
+
#
|
26
|
+
# @note This method leverages a thread-local variable to manage the disabled state,
|
27
|
+
# ensuring that the disabling is scoped to the current thread only.
|
28
|
+
def disable_friendly_id(&action)
|
29
|
+
::FriendlyId::Disabler.disable_friendly_id(&action)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/concern'
|
5
|
+
require_relative 'activeadmin_friendly_id_disabler/version'
|
6
|
+
require 'activeadmin/friendly_id_disabler'
|
7
|
+
require 'friendly_id/disabler'
|
8
|
+
|
9
|
+
module ActiveadminFriendlyIdDisabler
|
10
|
+
class Error < StandardError; end
|
11
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FriendlyId
|
4
|
+
# The Disabler module manages the temporary disabling of FriendlyId functionality
|
5
|
+
# by utilizing a thread-local variable. This module provides methods to check
|
6
|
+
# the current state of FriendlyId (enabled/disabled) and to execute blocks with
|
7
|
+
# FriendlyId disabled.
|
8
|
+
module Disabler
|
9
|
+
# A key used in `Thread.current` to store the state of FriendlyId (enabled/disabled).
|
10
|
+
THREAD_LOCAL_KEY = :__friendly_id_enabler_disabled
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# Checks if FriendlyId is currently disabled.
|
14
|
+
#
|
15
|
+
# @return [Boolean] returns true if FriendlyId is currently disabled in the thread.
|
16
|
+
def disabled?
|
17
|
+
!!Thread.current[THREAD_LOCAL_KEY]
|
18
|
+
end
|
19
|
+
|
20
|
+
# Temporarily disables FriendlyId for the duration of the given block.
|
21
|
+
#
|
22
|
+
# This method sets a thread-local flag that is checked by the `FriendlyId::Slugged#to_param`
|
23
|
+
# method. It ensures that the disabling is scoped only to the current thread of execution.
|
24
|
+
#
|
25
|
+
# @yield the block of code during which FriendlyId should be disabled.
|
26
|
+
def disable_friendly_id
|
27
|
+
old_value = Thread.current[THREAD_LOCAL_KEY]
|
28
|
+
Thread.current[THREAD_LOCAL_KEY] = true
|
29
|
+
yield
|
30
|
+
ensure
|
31
|
+
Thread.current[THREAD_LOCAL_KEY] = old_value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# The Slugged module is an extension of FriendlyId''s functionality,
|
37
|
+
# providing a way to customize how object URLs are generated with slugs.
|
38
|
+
module Slugged
|
39
|
+
# Overrides the default `to_param` method to conditionally return the record''s ID
|
40
|
+
# when FriendlyId is disabled, otherwise leveraging the slug functionality.
|
41
|
+
#
|
42
|
+
# @return [String] the URL-safe parameter string representing the object.
|
43
|
+
def to_param
|
44
|
+
::FriendlyId::Disabler.disabled? ? id&.to_s : super
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeadmin_friendly_id_disabler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Gordienko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activeadmin
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: friendly_id
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.3.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.0.0
|
69
|
+
description: |-
|
70
|
+
This gem helps disable FriendlyId''s default behavior in ActiveAdmin to allow usage of standard
|
71
|
+
ID instead of friendly slugs in URLs, eliminating possible conflicts.
|
72
|
+
email:
|
73
|
+
- alx@anadyr.org
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- lib/activeadmin/friendly_id_disabler.rb
|
81
|
+
- lib/activeadmin_friendly_id_disabler.rb
|
82
|
+
- lib/activeadmin_friendly_id_disabler/version.rb
|
83
|
+
- lib/friendly_id/disabler.rb
|
84
|
+
homepage: https://github.com/gordienko/activeadmin_friendly_id_disabler
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata:
|
88
|
+
allowed_push_host: https://rubygems.org
|
89
|
+
homepage_uri: https://github.com/gordienko/activeadmin_friendly_id_disabler
|
90
|
+
source_code_uri: https://github.com/gordienko/activeadmin_friendly_id_disabler
|
91
|
+
changelog_uri: https://github.com/gordienko/activeadmin_friendly_id_disabler/CHANGELOG.md
|
92
|
+
rubygems_mfa_required: 'false'
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 3.0.0
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.5.16
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Disables FriendlyId in ActiveAdmin environments where it''s not needed.
|
112
|
+
test_files: []
|