module Scrivito class UiConfig < Struct.new(:editing_context, :resource, :return_to, :app_extension_tags) def initialize(**named_params) named_params.each { |key, value| self[key] = value } end def to_json { editing_context: editing_context_config, i18n: i18n_config, is_development_mode: Rails.env.development?, resource_dialog: resource_dialog_config, user: user_config, user_permissions: user_permissions_config, app_extension_tags: app_extension_tags, }.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 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?, title: workspace.title, } end def i18n_config {locale: Scrivito::Configuration.ui_locale || I18n.locale} 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), 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