Skip to content

Commit 020d477

Browse files
committed
Add a StringSymbolOrProcSetting inspired by MethodProcHelper.render_or_call_method_or_proc_on
1 parent afd466b commit 020d477

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

lib/active_admin/dynamic_setting.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
module ActiveAdmin
22

33
class DynamicSetting
4-
def self.build(setting)
5-
new(setting)
4+
def self.build(setting, type)
5+
(type ? klass(type) : self).new(setting)
6+
end
7+
8+
def self.klass(type)
9+
klass = "#{type.to_s.camelcase}Setting"
10+
raise ArgumentError, "Unknown type: #{type}" unless ActiveAdmin.const_defined?(klass)
11+
ActiveAdmin.const_get(klass)
612
end
713

814
def initialize(setting)
@@ -14,4 +20,19 @@ def value(*_args)
1420
end
1521
end
1622

23+
# Many configuration options (Ex: site_title, title_image) could either be
24+
# static (String), methods (Symbol) or procs (Proc). This wrapper takes care of
25+
# returning the content when String or using instance_eval when Symbol or Proc.
26+
#
27+
class StringSymbolOrProcSetting < DynamicSetting
28+
def value(context = self)
29+
case @setting
30+
when Symbol, Proc
31+
context.instance_eval(&@setting)
32+
else
33+
@setting
34+
end
35+
end
36+
end
37+
1738
end

lib/active_admin/dynamic_settings_node.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ module ActiveAdmin
55

66
class DynamicSettingsNode < SettingsNode
77
class << self
8-
def register(name, value)
8+
def register(name, value, type = nil)
99
class_attribute "#{name}_setting"
1010
add_reader(name)
11-
add_writer(name)
11+
add_writer(name, type)
1212
send "#{name}=", value
1313
end
1414

1515
def add_reader(name)
16-
define_singleton_method(name) do
17-
send("#{name}_setting").value
16+
define_singleton_method(name) do |*args|
17+
send("#{name}_setting").value(*args)
1818
end
1919
end
2020

21-
def add_writer(name)
21+
def add_writer(name, type)
2222
define_singleton_method("#{name}=") do |value|
23-
send("#{name}_setting=", DynamicSetting.build(value))
23+
send("#{name}_setting=", DynamicSetting.build(value, type))
2424
end
2525
end
2626
end

spec/unit/dynamic_settings_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe ActiveAdmin::DynamicSettingsNode do
4+
subject { ActiveAdmin::DynamicSettingsNode.build }
5+
6+
context "StringSymbolOrProcSetting" do
7+
before { subject.register :foo, 'bar', :string_symbol_or_proc }
8+
9+
it "should pass through a string" do
10+
subject.foo = "string"
11+
expect(subject.foo(self)).to eq "string"
12+
end
13+
14+
it "should instance_exec if context given" do
15+
ctx = Hash[i: 42]
16+
subject.foo = proc { self[:i] += 1 }
17+
expect(subject.foo(ctx)).to eq 43
18+
expect(subject.foo(ctx)).to eq 44
19+
end
20+
21+
it "should send message if symbol given" do
22+
ctx = double
23+
expect(ctx).to receive(:quux).and_return 'qqq'
24+
subject.foo = :quux
25+
expect(subject.foo(ctx)).to eq 'qqq'
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)