module Scrivito class UiConfig < Struct.new(:editing_context, :resource, :return_to, :app_extension_tags, :obj_models, :widget_models) def initialize(**named_params) named_params.each { |key, value| self[key] = value } end def to_json { app_extension_tags: app_extension_tags, editing_context: editing_context_config, session: session_config, i18n: i18n_config, is_development_mode: Rails.env.development?, resource_dialog: resource_dialog_config, user: user_config, user_permissions: user_permissions_config, obj_class: obj_class_config, widget_class: widget_class_config, }.to_json end private def editing_context_config { display_mode: editing_context.display_mode, selected_workspace: workspace_config(selected_workspace), visible_workspace: workspace_config(visible_workspace), } end def session_config Session.renew(SecureRandom.hex(8), editor) end def workspace_config(workspace) { id: workspace.id, memberships: workspace.memberships.map do |membership| { user_id: membership.user_id, role: membership.role, description: membership.user.description } end, outdated: workspace.outdated?, auto_update: workspace.auto_update?, title: workspace.title, } end def i18n_config {locale: Scrivito::Configuration.ui_locale || I18n.locale} end def obj_class_config obj_models.map(&:as_json) end def widget_class_config widget_models.map(&:as_json) end def resource_dialog_config return {} unless resource { obj: Configuration.obj_formats.fetch('_default').call(resource, editor), return_to: return_to, } end def user_permissions_config { create_workspace: editor.can?(:create, :workspace), rename_workspace: editor.can?(:write, selected_workspace), invite_to_workspace: editor.can?(:invite_to, selected_workspace), rebase_workspace: editor.can?(:write, selected_workspace), delete_workspace: editor.can?(:delete, selected_workspace), publish_workspace: editor.can?(:publish, selected_workspace), } end def user_config { current: { id: editor.id } } end def editor editing_context.editor end def selected_workspace editing_context.selected_workspace end def visible_workspace editing_context.visible_workspace end end end