Skip to content

Commit 4bd4300

Browse files
lazyatomparndt
authored andcommitted
Defer loading of ActiveRecord to avoid config issues
If FriendlyId references `ActiveRecord::Base` while it is loading as part of a Rails application, it triggers the `on_load` event for ActiveRecord, which as part of the loading process sets the current configuration values of ActiveRecord. If any initializers try to influence that configuration (e.g. changing defaults in `new_framework_defaults.rb` for an app that's been upgraded), these changes will have no effect because the configuration has already been set in place. To avoid this, we need to avoid triggering the `on_load` event by wrapping the reference to `ActiveRecord::Base` in an initializer block. The simplest way of doing this is with a `Railtie`, which will be automatically loaded if FriendlyId is required as part of Rails. Fixes #823
1 parent a29e7d7 commit 4bd4300

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

lib/friendly_id.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "friendly_id/object_utils"
55
require "friendly_id/configuration"
66
require "friendly_id/finder_methods"
7+
require 'friendly_id/railtie' if defined?(Rails)
78

89
=begin
910

lib/friendly_id/object_utils.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module FriendlyId
22
# Instances of these classes will never be considered a friendly id.
33
# @see FriendlyId::ObjectUtils#friendly_id
44
UNFRIENDLY_CLASSES = [
5-
ActiveRecord::Base,
65
Array,
76
FalseClass,
87
Hash,
@@ -59,11 +58,15 @@ def unfriendly_id?
5958
true
6059
end
6160
end
61+
62+
def self.mark_as_unfriendly(klass)
63+
klass.send(:include, FriendlyId::UnfriendlyUtils)
64+
end
6265
end
6366

6467
Object.send :include, FriendlyId::ObjectUtils
6568

6669
# Considered unfriendly if object is an instance of an unfriendly class or
6770
# one of its descendants.
6871

69-
FriendlyId::UNFRIENDLY_CLASSES.each { |klass| klass.send(:include, FriendlyId::UnfriendlyUtils) }
72+
FriendlyId::UNFRIENDLY_CLASSES.each { |klass| FriendlyId.mark_as_unfriendly(klass) }

lib/friendly_id/railtie.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module FriendlyId
2+
class Railtie < Rails::Railtie
3+
initializer 'friendly_id.setup' do
4+
ActiveSupport.on_load(:active_record) do
5+
FriendlyId.mark_as_unfriendly(ActiveRecord::Base)
6+
end
7+
end
8+
end
9+
end

test/object_utils_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class ObjectUtilsTest < TestCaseClass
1919
end
2020

2121
test "ActiveRecord::Base instances should be unfriendly_ids" do
22+
FriendlyId.mark_as_unfriendly(ActiveRecord::Base)
23+
2224
model_class = Class.new(ActiveRecord::Base) do
2325
self.table_name = "authors"
2426
end

0 commit comments

Comments
 (0)