From aeb2bcf29854465b542530b5ff66293f5dddc3da Mon Sep 17 00:00:00 2001 From: Uwe Kubosch Date: Wed, 22 Mar 2023 15:07:58 +0100 Subject: [PATCH] Added slim spring configuration --- build.gradle | 15 +++++++++- .../java/no/datek/slim/SlimConfigurer.java | 28 +++++++++++++++++++ src/main/resources/ruby/slim_helper.rb | 5 ++-- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/main/java/no/datek/slim/SlimConfigurer.java diff --git a/build.gradle b/build.gradle index 9936f6b..f8189b6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,24 @@ plugins { id 'java' + id "io.spring.dependency-management" version "1.1.0" + id "org.springframework.boot" version "2.7.6" id 'maven-publish' id 'signing' } group 'no.datek' -version '0.1.1' +version '0.2.0' +final String JRUBY_VERSION = '9.3.6.0'; + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' +// implementation "org.jruby:jruby:$JRUBY_VERSION" +} java { withJavadocJar() diff --git a/src/main/java/no/datek/slim/SlimConfigurer.java b/src/main/java/no/datek/slim/SlimConfigurer.java new file mode 100644 index 0000000..76287f8 --- /dev/null +++ b/src/main/java/no/datek/slim/SlimConfigurer.java @@ -0,0 +1,28 @@ +package no.datek.slim; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.script.ScriptTemplateConfigurer; +import org.springframework.web.servlet.view.script.ScriptTemplateViewResolver; + +@Configuration +public class SlimConfigurer implements WebMvcConfigurer { + @Bean + public ViewResolver scriptTemplateViewResolver() { + ScriptTemplateViewResolver resolver = new ScriptTemplateViewResolver(); + resolver.setPrefix("/views/"); + resolver.setSuffix(".slim"); + return resolver; + } + + @Bean + public ScriptTemplateConfigurer jrubyConfigurer() { + ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer(); + configurer.setEngineName("jruby"); + configurer.setScripts("ruby/load_slim.rb"); + configurer.setRenderFunction("render_slim"); + return configurer; + } +} diff --git a/src/main/resources/ruby/slim_helper.rb b/src/main/resources/ruby/slim_helper.rb index ba64f44..aab9553 100644 --- a/src/main/resources/ruby/slim_helper.rb +++ b/src/main/resources/ruby/slim_helper.rb @@ -63,14 +63,15 @@ def params.[](key) application_context = rendering_context.application_context message_source = application_context.get_bean(org.springframework.context.MessageSource.java_class) message_source_accessor = MessageSourceAccessor.new(message_source, locale) + current_location = request.getSession().getAttribute("currentLocation") default_context = { _csrf: request.getAttribute('_csrf'), application_context: application_context, content_store: {}, ctx: request.contextPath, - current_location: request.getSession().getAttribute("currentLocation"), - currentLocation: request.getSession().getAttribute("currentLocation"), + current_location: current_location, + currentLocation: current_location, current_user: SecurityContextHolder.context&.authentication&.principal, locale: locale, message: message_source_accessor,