File tree Expand file tree Collapse file tree 3 files changed +57
-8
lines changed Expand file tree Collapse file tree 3 files changed +57
-8
lines changed Original file line number Diff line number Diff line change 1
1
module ActiveAdmin
2
2
3
3
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 )
6
12
end
7
13
8
14
def initialize ( setting )
@@ -14,4 +20,19 @@ def value(*_args)
14
20
end
15
21
end
16
22
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
+
17
38
end
Original file line number Diff line number Diff line change @@ -5,22 +5,22 @@ module ActiveAdmin
5
5
6
6
class DynamicSettingsNode < SettingsNode
7
7
class << self
8
- def register ( name , value )
8
+ def register ( name , value , type = nil )
9
9
class_attribute "#{ name } _setting"
10
10
add_reader ( name )
11
- add_writer ( name )
11
+ add_writer ( name , type )
12
12
send "#{ name } =" , value
13
13
end
14
14
15
15
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 )
18
18
end
19
19
end
20
20
21
- def add_writer ( name )
21
+ def add_writer ( name , type )
22
22
define_singleton_method ( "#{ name } =" ) do |value |
23
- send ( "#{ name } _setting=" , DynamicSetting . build ( value ) )
23
+ send ( "#{ name } _setting=" , DynamicSetting . build ( value , type ) )
24
24
end
25
25
end
26
26
end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments