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
|
# File 'lib/generators/ez/settings/install_generator.rb', line 5
def create_initializer_file
create_file 'config/initializers/ez_settings.rb',
"# By default engine try to inherit from ApplicationController, but you could change this:
# Ez::Settings.config.base_controller = 'Admin::BaseController'
#
# Then you should define settings interfaces (you can create as much as you need)
# Ierachy is pretty simple: Interface -> Group -> Key
#
# Interface DSL allows you to do this very declaratively
#
app = Ez::Settings::Interface.define :app do # :app - interface name
group :general do # :general - name of the group
key :app_title, default: -> { 'Main app title' } # :app_title - key name for store value in :general group for :app interface
end
# And so on...
group :admin do
key :app_title, default: -> { 'Admin app title' }
end
# If you want to see all power of the engine, add this showcase:
group :showcase do
key :string, default: -> { 'simple string' }
key :bool, type: :boolean, default: -> { true }
key :integer, type: :integer, default: -> { 777 }
key :select, type: :select, default: -> { 'foo' }, collection: %w(foo bar baz)
key :not_validate, required: false, presence: false
key :not_for_ui, required: false, ui: false
end
# Keys could have:
# :type (string by default), now ez-settings supports only: string, boolean, integer and select
# :default value (as callable objects)
# :required - be or not (all keys are required by default)
# :ui visible or not (all keys are UI visible by default)
end
# After defining settings interface groups/keys you need to configure it:
app.configure do |config|
# Backend adapter to store all settings
config.backend = Ez::Settings::Backend::FileSystem.new(Rails.root.join('config', 'settings.yml'))
# Redis is also supported:
# config.backend = Ez::Settings::Backend::Redis.new('config')
# Default path for redirect in controller
config.default_path = '/admin/settings'
# Pass your custom css classes through css_map config
# Defaults would be merged with yours:
# config.custom_css_map = {
# nav_label: 'ez-settings-nav-label',
# nav_menu: 'ez-settings-nav-menu',
# nav_menu_item: 'ez-settings-nav-menu-item',
# overview_page_wrapper: 'ez-settings-overview',
# overview_page_section: 'ez-settings-overview-section',
# overview_page_section_header: 'ez-settings-overview-section-header',
# overview_page_section_content: 'ez-settings-overview-section-content',
# overview_page_section_content_key: 'ez-settings-overview-section-content-key',
# overview_page_section_content_value: 'ez-settings-overview-section-content-value',
# group_page_wrapper: 'ez-settings-group-wrapper',
# group_page_inner_wrapper: 'ez-settings-group-inner-wrapper',
# group_page_header: 'ez-settings-group-header',
# group_page_form_wrapper: 'ez-settings-group-form-wrapper',
# group_page_form_inner: 'ez-settings-group-form-inner',
# group_page_form_field_row: 'ez-settings-group-form-field-row',
# group_page_form_string_wrapper: 'ez-settings-group-form-string-wrapper',
# group_page_form_boolean_wrapper: 'ez-settings-group-form-boolean-wrapper',
# group_page_form_select_wrapper: 'ez-settings-group-form-select-wrapper',
# group_page_actions_wrapper: 'ez-settings-group-actions-wrapper',
# group_page_actions_save_button: 'ez-settings-group-actions-save-btn',
# group_page_actions_cancel_link: 'ez-settings-group-actions-cancel-link'
# }
#
# Highly recommend inspecting settings page DOM.
# You can find there a lot of interesting id/class stuff
#
# You even can define dynamic map for allows to decide which CSS class could be added
# `if` must contain callable object that receives controller as a first argument and dynamic element as second one:
#
# In this example, you easily could add 'active' CSS class if route end with some fragment:
# config.dynamic_css_map = {
# nav_menu_item: {
# css_class: 'active',
# if: ->(controller, path_fragment) { controller.request.path.end_with?(path_fragment.to_s) }
# }
# }
end
# Register `app` variable as settings interface
Ez::Registry.in(:settings_interfaces, by: 'YourAppName') do |registry|
registry.add app
end"
end
|