Skip to content

Commit 5b1fc43

Browse files
committed
Refactor: Replace ConfigSingleton with direct instance variables
Remove custom ConfigSingleton implementation that was causing ActiveSupport::Configurable deprecation warnings in Rails 8.2+. Changes: - Remove ConfigSingleton base class and its metaprogramming - Implement config_accessor using direct instance variable access - Convert config_group pattern to explicit nested classes - Maintain all existing validators and validation logic - Preserve public API compatibility - Update method syntax from blocks to keyword arguments All configuration behavior remains the same, but without relying on deprecated ActiveSupport::Configurable patterns.
1 parent 1fe37fa commit 5b1fc43

File tree

1 file changed

+82
-85
lines changed

1 file changed

+82
-85
lines changed

lib/intercom-rails/config.rb

Lines changed: 82 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,8 @@
22

33
module IntercomRails
44

5-
class ConfigSingleton
6-
7-
def self.config_accessor(*args, &block)
8-
config_reader(*args)
9-
config_writer(*args, &block)
10-
end
11-
12-
def self.meta_class
13-
class << self; self end
14-
end
15-
16-
def self.config_reader(name)
17-
meta_class.send(:define_method, name) do
18-
instance_variable_get("@#{name}")
19-
end
20-
end
21-
22-
def self.config_writer(name, &block)
23-
meta_class.send(:define_method, "#{name}=") do |value|
24-
validate(name, value, block)
25-
instance_variable_set("@#{name}", value)
26-
end
27-
end
28-
29-
def self.config_group(name, &block)
30-
camelized_name = name.to_s.classify
31-
group = self.const_set(camelized_name, Class.new(self))
32-
33-
meta_class.send(:define_method, name) do
34-
group
35-
end
36-
37-
group.send(:instance_variable_set, :@underscored_class_name, name)
38-
group.instance_eval(&block)
39-
end
40-
41-
private
42-
43-
def self.validate(name, value, block)
44-
return unless block
45-
args = [value]
46-
if block.arity > 1
47-
field_name = underscored_class_name ? "#{underscored_class_name}.#{name}" : name
48-
args << field_name
49-
end
50-
block.call(*args)
51-
end
52-
53-
def self.underscored_class_name
54-
@underscored_class_name
55-
end
56-
57-
end
58-
59-
class Config < ConfigSingleton
60-
5+
class Config
6+
# Validators (moved outside of class << self for accessibility)
617
CUSTOM_DATA_VALIDATOR = Proc.new do |custom_data, field_name|
628
case custom_data
639
when Hash
@@ -79,7 +25,7 @@ class Config < ConfigSingleton
7925
end
8026

8127
IS_ARAY_OF_PROC_VALIDATOR = Proc.new do |data, field_name|
82-
raise ArgumentError, "#{field_name} data should be a proc or an array of proc" unless data.all? { |value| value.kind_of?(Proc)}
28+
raise ArgumentError, "#{field_name} data should be a proc or an array of proc" unless data.all? { |value| value.kind_of?(Proc)}
8329
end
8430

8531
IS_PROC_OR_ARRAY_OF_PROC_VALIDATOR = Proc.new do |data, field_name|
@@ -90,69 +36,120 @@ class Config < ConfigSingleton
9036
end
9137
end
9238

93-
def self.reset!
94-
to_reset = self.constants.map {|c| const_get c}
95-
to_reset << self
39+
class << self
9640

97-
to_reset.each do |configer|
98-
configer.instance_variables.each do |var|
99-
configer.send(:remove_instance_variable, var)
41+
# Helper to create accessor with validation
42+
def config_accessor(name, validator: nil)
43+
attr_name = :"@#{name}"
44+
45+
# Define getter
46+
define_singleton_method(name) do
47+
instance_variable_get(attr_name)
48+
end
49+
50+
# Define setter with validation
51+
define_singleton_method("#{name}=") do |value|
52+
if validator
53+
field_name = @underscored_class_name ? "#{@underscored_class_name}.#{name}" : name.to_s
54+
args = validator.arity > 1 ? [value, field_name] : [value]
55+
validator.call(*args)
56+
end
57+
instance_variable_set(attr_name, value)
10058
end
10159
end
60+
61+
def reset!
62+
to_reset = [self] + constants.select { |c| const_get(c).is_a?(Class) && const_get(c) < Config }.map { |c| const_get(c) }
63+
64+
to_reset.each do |configer|
65+
configer.instance_variables.each do |var|
66+
configer.send(:remove_instance_variable, var)
67+
end
68+
end
69+
end
70+
71+
def api_key=(*)
72+
warn "Setting an Intercom API key is no longer supported; remove the `config.api_key = ...` line from config/initializers/intercom.rb"
73+
end
10274
end
10375

76+
# Main configuration options
10477
config_accessor :app_id
10578
config_accessor :session_duration
10679
config_accessor :api_secret
10780
config_accessor :library_url
108-
config_accessor :enabled_environments, &ARRAY_VALIDATOR
81+
config_accessor :enabled_environments, validator: ARRAY_VALIDATOR
10982
config_accessor :include_for_logged_out_users
11083
config_accessor :hide_default_launcher
11184
config_accessor :api_base
11285
config_accessor :encrypted_mode
11386

114-
def self.api_key=(*)
115-
warn "Setting an Intercom API key is no longer supported; remove the `config.api_key = ...` line from config/initializers/intercom.rb"
116-
end
87+
# User configuration group
88+
class User < Config
89+
@underscored_class_name = :user
11790

118-
config_group :user do
119-
config_accessor :current, &IS_PROC_OR_ARRAY_OF_PROC_VALIDATOR
120-
config_accessor :exclude_if, &IS_PROC_VALIDATOR
121-
config_accessor :model, &IS_PROC_VALIDATOR
122-
config_accessor :lead_attributes, &ARRAY_VALIDATOR
123-
config_accessor :custom_data, &CUSTOM_DATA_VALIDATOR
91+
config_accessor :current, validator: IS_PROC_OR_ARRAY_OF_PROC_VALIDATOR
92+
config_accessor :exclude_if, validator: IS_PROC_VALIDATOR
93+
config_accessor :model, validator: IS_PROC_VALIDATOR
94+
config_accessor :lead_attributes, validator: ARRAY_VALIDATOR
95+
config_accessor :custom_data, validator: CUSTOM_DATA_VALIDATOR
12496

12597
def self.company_association=(*)
12698
warn "Setting a company association is no longer supported; remove the `config.user.company_association = ...` line from config/initializers/intercom.rb"
12799
end
128100
end
129101

130-
config_group :company do
131-
config_accessor :current, &IS_PROC_VALIDATOR
132-
config_accessor :exclude_if, &IS_PROC_VALIDATOR
133-
config_accessor :plan, &IS_PROC_VALIDATOR
134-
config_accessor :monthly_spend, &IS_PROC_VALIDATOR
135-
config_accessor :custom_data, &CUSTOM_DATA_VALIDATOR
102+
# Company configuration group
103+
class Company < Config
104+
@underscored_class_name = :company
105+
106+
config_accessor :current, validator: IS_PROC_VALIDATOR
107+
config_accessor :exclude_if, validator: IS_PROC_VALIDATOR
108+
config_accessor :plan, validator: IS_PROC_VALIDATOR
109+
config_accessor :monthly_spend, validator: IS_PROC_VALIDATOR
110+
config_accessor :custom_data, validator: CUSTOM_DATA_VALIDATOR
136111
end
137112

138-
config_group :inbox do
113+
# Inbox configuration group
114+
class Inbox < Config
115+
@underscored_class_name = :inbox
116+
139117
config_accessor :counter # Keep this for backwards compatibility
140118
config_accessor :custom_activator
141-
config_accessor :style do |value|
119+
config_accessor :style, validator: Proc.new { |value|
142120
raise ArgumentError, "inbox.style must be one of :default or :custom" unless [:default, :custom].include?(value)
143-
end
121+
}
144122
end
145123

146-
config_group :jwt do
124+
# JWT configuration group
125+
class Jwt < Config
126+
@underscored_class_name = :jwt
127+
147128
config_accessor :enabled
148129
config_accessor :expiry
149-
config_accessor :signed_user_fields do |value|
130+
config_accessor :signed_user_fields, validator: Proc.new { |value|
150131
unless value.nil? || (value.kind_of?(Array) && value.all? { |v| v.kind_of?(Symbol) || v.kind_of?(String) })
151132
raise ArgumentError, "jwt.signed_user_fields must be an array of symbols or strings"
152133
end
153-
end
134+
}
135+
end
136+
137+
# Methods to access configuration groups
138+
def self.user
139+
User
154140
end
155141

142+
def self.company
143+
Company
144+
end
145+
146+
def self.inbox
147+
Inbox
148+
end
149+
150+
def self.jwt
151+
Jwt
152+
end
156153
end
157154

158155
end

0 commit comments

Comments
 (0)