-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.rb
More file actions
120 lines (104 loc) · 3.22 KB
/
settings.rb
File metadata and controls
120 lines (104 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
module TinyAdmin
class Settings
include Singleton
DEFAULTS = {
%i[authentication plugin] => Plugins::NoAuth,
%i[authentication login] => Views::Pages::SimpleAuthLogin,
%i[authorization_class] => Plugins::Authorization,
%i[components field_value] => Views::Components::FieldValue,
%i[components flash] => Views::Components::Flash,
%i[components head] => Views::Components::Head,
%i[components navbar] => Views::Components::Navbar,
%i[components pagination] => Views::Components::Pagination,
%i[content_page] => Views::Pages::Content,
%i[helper_class] => Support,
%i[page_not_allowed] => Views::Pages::PageNotAllowed,
%i[page_not_found] => Views::Pages::PageNotFound,
%i[record_not_found] => Views::Pages::RecordNotFound,
%i[repository] => Plugins::ActiveRecordRepository,
%i[root_path] => "/admin",
%i[root page] => Views::Pages::Root,
%i[root title] => "TinyAdmin",
%i[sections] => []
}.freeze
OPTIONS = %i[
authentication
authorization_class
components
content_page
extra_styles
helper_class
page_not_allowed
page_not_found
record_not_found
repository
root
root_path
sections
scripts
style_links
].freeze
attr_reader :store
OPTIONS.each do |option|
define_method(option) do
self[option]
end
define_method("#{option}=") do |value|
self[option] = value
end
end
def [](*path)
key, option = fetch_setting(path)
option[key]
end
def []=(*path, value)
key, option = fetch_setting(path)
option[key] = value
convert_value(key, value)
end
def load_settings
return if @loaded
# default values
DEFAULTS.each do |(option, param), default|
if param
self[option] ||= {}
self[option][param] ||= default
else
self[option] ||= default
end
end
@store ||= TinyAdmin::Store.new(self)
self.root_path = "/" if root_path == ""
if authentication[:plugin].is_a?(Module) && authentication[:plugin] <= Plugins::SimpleAuth
logout_path = "#{root_path}/auth/logout"
authentication[:logout] ||= TinyAdmin::Section.new(name: "logout", slug: "logout", path: logout_path)
end
store.prepare_sections(sections, logout: authentication[:logout])
@loaded = true
end
def reset!
@options = {}
@store = nil
@loaded = false
end
private
def fetch_setting(path)
@options ||= {}
*parts, last = path.map(&:to_sym)
[last, parts.inject(@options) { |result, part| result[part] ||= {} }]
end
def convert_value(key, value)
if value.is_a?(Hash)
value.each_key do |key2|
path = [key, key2]
if (DEFAULTS[path].is_a?(Class) || DEFAULTS[path].is_a?(Module)) && self[key][key2].is_a?(String)
self[key][key2] = Object.const_get(self[key][key2])
end
end
elsif value.is_a?(String) && (DEFAULTS[[key]].is_a?(Class) || DEFAULTS[[key]].is_a?(Module))
self[key] = Object.const_get(self[key])
end
end
end
end