-
-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathconfiguration.rb
More file actions
409 lines (357 loc) · 12.1 KB
/
Copy pathconfiguration.rb
File metadata and controls
409 lines (357 loc) · 12.1 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
module Avo
class Configuration
attr_writer :app_name
attr_writer :root_path
attr_writer :cache_store
attr_writer :logger
attr_writer :turbo
attr_writer :pagination
attr_writer :explicit_authorization
attr_writer :exclude_from_status
attr_writer :persistence
attr_writer :resource_row_controls_config
attr_writer :hotkeys
# When unset, Tailwind scans Rails.root.join("app"). Each entry is an absolute path or a path relative to Rails.root.
attr_writer :tailwindcss_content_sources
attr_accessor :timezone
attr_accessor :per_page
attr_accessor :per_page_steps
attr_accessor :via_per_page
attr_accessor :locale
attr_accessor :currency
attr_accessor :default_view_type
attr_accessor :license_key
attr_accessor :authorization_methods
attr_accessor :authenticate
attr_accessor :current_user
attr_accessor :id_links_to_resource
attr_accessor :cache_resources_on_index_view
attr_accessor :context
attr_accessor :hide_layout_when_printing
attr_accessor :initial_breadcrumbs
attr_accessor :home_path
attr_accessor :search_debounce
attr_accessor :view_component_path
attr_accessor :display_license_request_timeout_error
attr_accessor :current_user_resource_name
attr_accessor :raise_error_on_missing_policy
attr_accessor :global_search
attr_accessor :buttons_on_form_footers
attr_accessor :main_menu
attr_accessor :profile_menu
attr_accessor :model_resource_mapping
attr_reader :resource_default_view
attr_accessor :authorization_client
attr_accessor :field_wrapper_layout
attr_accessor :sign_out_path_name
attr_accessor :resources
attr_accessor :prefix_path
attr_accessor :resource_parent_controller
attr_accessor :default_url_options
attr_accessor :click_row_to_view_record
attr_accessor :alert_dismiss_time
attr_accessor :is_admin_method
attr_accessor :is_developer_method
attr_accessor :search_results_count
attr_accessor :first_sorting_option
attr_accessor :associations_lookup_list_limit
attr_accessor :column_names_mapping
attr_accessor :column_types_mapping
attr_accessor :model_generator_hook
attr_accessor :send_metadata
attr_accessor :use_stacked_fields
attr_accessor :default_editor_url
attr_writer :body_classes
attr_accessor :sidebar_toggle_visible
attr_accessor :tailwindcss_integration_enabled
attr_accessor :mount_lookbook
unless defined?(CONTAINER_WIDTH_DEFAULTS)
CONTAINER_WIDTH_DEFAULTS = {
index: :large,
show: :small,
new: :small,
edit: :small,
create: :small,
update: :small
}.freeze
VALID_CONTAINER_WIDTHS = %i[full large small].freeze
CONTAINER_WIDTH_GROUPS = {
forms: %i[new edit create update],
display: %i[index show],
single: %i[show new edit create update]
}.freeze
end
def container_width=(value)
case value
when NilClass
@container_width = nil
when Symbol
raise ArgumentError, "Invalid container width: #{value}. Must be one of #{VALID_CONTAINER_WIDTHS}" unless VALID_CONTAINER_WIDTHS.include?(value)
@container_width = CONTAINER_WIDTH_DEFAULTS.transform_values { value }
when Hash
valid_keys = CONTAINER_WIDTH_DEFAULTS.keys + CONTAINER_WIDTH_GROUPS.keys
invalid_keys = value.keys.reject { |k| valid_keys.include?(k) }
raise ArgumentError, "Invalid container width keys: #{invalid_keys}. Valid keys: #{valid_keys}" if invalid_keys.any?
invalid_values = value.values.reject { |v| VALID_CONTAINER_WIDTHS.include?(v) }
raise ArgumentError, "Invalid container widths: #{invalid_values}" if invalid_values.any?
# Expand group aliases first
expanded_aliases = value
.select { |k, _| CONTAINER_WIDTH_GROUPS.key?(k) }
.each_with_object({}) do |(alias_key, width), result|
CONTAINER_WIDTH_GROUPS[alias_key].each { |view| result[view] = width }
end
# Specific keys win over group aliases
specific_keys = value.reject { |k, _| CONTAINER_WIDTH_GROUPS.key?(k) }
@container_width = CONTAINER_WIDTH_DEFAULTS.merge(expanded_aliases).merge(specific_keys)
end
end
def container_width
@container_width || CONTAINER_WIDTH_DEFAULTS
end
def tailwindcss_content_sources
if @tailwindcss_content_sources.nil?
[Rails.root.join("app")]
else
Array(@tailwindcss_content_sources)
end
end
def initialize
@root_path = "/avo"
@app_name = ::Rails.application.class.to_s.split("::").first.underscore.humanize(keep_id_suffix: true)
@timezone = "UTC"
@per_page = 24
@per_page_steps = [12, 24, 48, 72]
@via_per_page = 8
@locale = nil
@currency = "USD"
@default_view_type = :table
@license_key = nil
@current_user = proc {}
@authenticate = proc {}
@explicit_authorization = false
@authorization_methods = {
index: "index?",
show: "show?",
edit: "edit?",
new: "new?",
update: "update?",
create: "create?",
destroy: "destroy?"
}
@id_links_to_resource = false
@cache_resources_on_index_view = Avo::PACKED
@persistence = {
driver: nil
}
@context = proc {}
@initial_breadcrumbs = proc {
add_breadcrumb title: I18n.t("avo.home").humanize, path: avo.root_path, icon: "tabler/outline/home"
}
@hide_layout_when_printing = false
@home_path = nil
@search_debounce = 300
@view_component_path = "app/components"
@display_license_request_timeout_error = true
@current_user_resource_name = "user"
@raise_error_on_missing_policy = false
@buttons_on_form_footers = false
@main_menu = nil
@profile_menu = nil
@model_resource_mapping = {}
@resource_default_view = Avo::ViewInquirer.new("show")
@authorization_client = :pundit
@field_wrapper_layout = :inline
@resources = nil
@resource_parent_controller = "Avo::ResourcesController"
@cache_store = computed_cache_store
@logger = default_logger
@turbo = default_turbo
@default_url_options = []
@pagination = {}
@click_row_to_view_record = true
@alert_dismiss_time = 5000
@is_admin_method = :is_admin?
@is_developer_method = :is_developer?
@search_results_count = 8
@first_sorting_option = :desc # :desc or :asc
@associations_lookup_list_limit = 1000
@exclude_from_status = []
@column_names_mapping = {}
@column_types_mapping = {}
@resource_row_controls_config = {}
@hotkeys = {}
@global_search = {
enabled: true,
navigation_section: true
}
@model_generator_hook = true
@send_metadata = true
@use_stacked_fields = false
@default_editor_url = "cursor://file/%{path}"
@sidebar_toggle_visible = true
@body_classes = []
@tailwindcss_integration_enabled = true
@mount_lookbook = false
end
unless defined?(RESOURCE_ROW_CONTROLS_CONFIG_DEFAULTS)
RESOURCE_ROW_CONTROLS_CONFIG_DEFAULTS = {
placement: :right,
float: false,
show_on_hover: false
}.freeze
end
unless defined?(HOTKEYS_DEFAULTS)
HOTKEYS_DEFAULTS = {
enabled: true,
show_key_badges: true
}.freeze
end
def resource_row_controls_config
RESOURCE_ROW_CONTROLS_CONFIG_DEFAULTS.merge @resource_row_controls_config
end
def hotkeys
HOTKEYS_DEFAULTS.merge @hotkeys
end
# Authorization is enabled when:
# (avo-pro gem is installed) AND (authorization_client is NOT nil)
def authorization_enabled?
@authorization_enabled ||= Avo.plugin_manager.installed?("avo-pro") && !authorization_client.nil?
end
def current_user_method(&block)
@current_user = block if block.present?
end
def current_user_method=(method)
@current_user = method if method.present?
end
def authenticate_with(&block)
@authenticate = block if block.present?
end
def set_context(&block)
@context = block if block.present?
end
def set_initial_breadcrumbs(&block)
@initial_breadcrumbs = block if block.present?
end
def namespace
if Avo.configuration.root_path.present?
Avo.configuration.root_path.delete "/"
else
root_path.delete "/"
end
end
def root_path
return "" if @root_path === "/"
@root_path
end
def appearance
@appearance ||= Avo::Configuration::Appearance.new
end
def appearance=(options = {})
@appearance = Avo::Configuration::Appearance.new(options)
end
def app_name
Avo::ExecutionContext.new(target: @app_name).handle
end
def resource_default_view=(view)
@resource_default_view = Avo::ViewInquirer.new(view.to_s)
end
def cache_store
Avo::ExecutionContext.new(
target: @cache_store,
production_rejected_cache_stores: %w[ActiveSupport::Cache::MemoryStore ActiveSupport::Cache::NullStore]
).handle
end
# When not in production we'll just use the FileStore which is good enough.
# When running in production we'll use Rails.cache if it's not ActiveSupport::Cache::MemoryStore or ActiveSupport::Cache::NullStore.
# If it's one of rejected cache stores, we'll use the FileStore.
# We decided against the MemoryStore in production because it will not be shared between multiple processes (when using Puma).
def computed_cache_store
file_store_instance = ActiveSupport::Cache.lookup_store(:file_store, Rails.root.join("tmp", "cache"))
-> {
if Rails.env.production?
if Rails.cache.class.to_s.in?(production_rejected_cache_stores)
file_store_instance
else
Rails.cache
end
else
file_store_instance
end
}
end
def logger
Avo::ExecutionContext.new(target: @logger).handle
end
def default_logger
-> {
file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log"))
file_logger.datetime_format = "%Y-%m-%d %H:%M:%S"
file_logger.formatter = proc do |severity, time, progname, msg|
"[Avo->] #{time}: #{msg}\n".tap do |i|
puts i
end
end
file_logger
}
end
def turbo
Avo::ExecutionContext.new(target: @turbo).handle
end
def exclude_from_status
Avo::ExecutionContext.new(target: @exclude_from_status).handle
end
def default_turbo
-> do
{
instant_click: true
}
end
end
def pagination
Avo::ExecutionContext.new(target: @pagination).handle
end
def default_locale
@locale || I18n.default_locale
end
# Known RTL (Right-to-Left) locale codes
RTL_LOCALES = %w[ar he fa ur yi ps sd ku ckb ug dv].freeze unless defined?(RTL_LOCALES)
# Check if the given locale is RTL
def self.rtl_locale?(locale = I18n.locale)
RTL_LOCALES.include?(locale.to_s.split("-").first.downcase)
end
# Check if the current locale is RTL
def rtl?
self.class.rtl_locale?(I18n.locale)
end
# Returns "rtl" or "ltr" based on current locale
def text_direction
rtl? ? "rtl" : "ltr"
end
def explicit_authorization
Avo::ExecutionContext.new(target: @explicit_authorization).handle
end
def body_classes
Avo::ExecutionContext.new(target: @body_classes).handle
end
def persistence
Avo::ExecutionContext.new(target: @persistence).handle
end
def session_persistence_enabled?
persistence[:driver] == :session
end
def mount_avo_engines=(...)
raise "'mount_avo_engines' option is now obsolete. \n" \
"Please refer to the upgrade guide for details on the new mounting point: \n" \
"https://docs.avohq.io/3.0/upgrade.html#Avo's%20mounting%20point%20update"
end
end
def self.configuration
@configuration ||= Configuration.new
end
def self.configuration=(config)
@configuration = config
end
def self.configure
yield configuration
end
end