From 50874df46f81f5b704a25613a6fd73abdd218988 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sun, 5 Feb 2023 16:18:18 +0200 Subject: [PATCH] an opal experiment --- Gemfile | 2 ++ Gemfile.lock | 13 +++++++++++++ app/assets/config/manifest.js | 1 + app/assets/javascript/opal_main.js.rb | 1 + app/controllers/kanta_controller.rb | 7 +++++++ app/views/kanta/_form.html.haml | 10 ++++++++++ app/views/kanta/edit.html.haml | 7 +++++++ app/views/kanta/index.html.haml | 19 +++++++++++++++++++ app/views/kanta/new.html.haml | 5 +++++ app/views/kanta/show.html.haml | 18 ++++++++++++++++++ app/views/layouts/application.haml | 3 ++- config/initializers/opal.rb | 22 ++++++++++++++++++++++ config/routes.rb | 1 + lib/opal/rails/haml_filter.rb | 19 +++++++++++++++++++ 14 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascript/opal_main.js.rb create mode 100644 app/controllers/kanta_controller.rb create mode 100644 app/views/kanta/_form.html.haml create mode 100644 app/views/kanta/edit.html.haml create mode 100644 app/views/kanta/index.html.haml create mode 100644 app/views/kanta/new.html.haml create mode 100644 app/views/kanta/show.html.haml create mode 100644 config/initializers/opal.rb create mode 100644 lib/opal/rails/haml_filter.rb diff --git a/Gemfile b/Gemfile index 5dd813e..13a4b59 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,8 @@ gem "devise-async" gem "passenger" , "6.0.15" , require: "phusion_passenger/rack_handler" gem "bootsnap", require: false +gem "opal-rails" + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mingw x64_mingw ] diff --git a/Gemfile.lock b/Gemfile.lock index 81f2d82..6a5eae8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -262,6 +262,18 @@ GEM mustache nokogiri (~> 1.7) sanitize + opal (1.7.2) + ast (>= 2.3.0) + parser (~> 3.0, >= 3.0.3.2) + opal-rails (2.0.2) + opal (~> 1.0) + opal-sprockets (~> 1.0) + rails (>= 6.0, < 7.1) + sprockets-rails (>= 3.0) + opal-sprockets (1.0.3) + opal (>= 1.0, < 2.0) + sprockets (~> 4.0) + tilt (>= 1.4) open4 (1.3.4) orm_adapter (0.5.0) parser (3.2.0.0) @@ -435,6 +447,7 @@ DEPENDENCIES kaminari merged! mina + opal-rails passenger (= 6.0.15) pg (~> 1.1) pundit (~> 2.3) diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index b06fc42..481e3f6 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -3,3 +3,4 @@ //= link_tree ../../javascript .js //= link_tree ../../../vendor/javascript .js //= link_tree ../builds +//= link_directory ../javascript .js \ No newline at end of file diff --git a/app/assets/javascript/opal_main.js.rb b/app/assets/javascript/opal_main.js.rb new file mode 100644 index 0000000..508ba44 --- /dev/null +++ b/app/assets/javascript/opal_main.js.rb @@ -0,0 +1 @@ +require "opal" diff --git a/app/controllers/kanta_controller.rb b/app/controllers/kanta_controller.rb new file mode 100644 index 0000000..b4de40a --- /dev/null +++ b/app/controllers/kanta_controller.rb @@ -0,0 +1,7 @@ +class KantaController < ApplicationController + + def show + @image = Merged::Image.first + end + +end diff --git a/app/views/kanta/_form.html.haml b/app/views/kanta/_form.html.haml new file mode 100644 index 0000000..99b17e7 --- /dev/null +++ b/app/views/kanta/_form.html.haml @@ -0,0 +1,10 @@ += form_for @kantum do |f| + - if @kantum.errors.any? + #error_explanation + %h2= "#{pluralize(@kantum.errors.count, "error")} prohibited this kantum from being saved:" + %ul + - @kantum.errors.full_messages.each do |message| + %li= message + + .actions + = f.submit 'Save' diff --git a/app/views/kanta/edit.html.haml b/app/views/kanta/edit.html.haml new file mode 100644 index 0000000..d2becac --- /dev/null +++ b/app/views/kanta/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing kantum + += render 'form' + += link_to 'Show', @kantum +\| += link_to 'Back', kanta_path diff --git a/app/views/kanta/index.html.haml b/app/views/kanta/index.html.haml new file mode 100644 index 0000000..fe2ed70 --- /dev/null +++ b/app/views/kanta/index.html.haml @@ -0,0 +1,19 @@ +%h1 Listing kanta + +%table + %thead + %tr + %th + %th + %th + + %tbody + - @kanta.each do |kantum| + %tr + %td= link_to 'Show', kantum + %td= link_to 'Edit', edit_kantum_path(kantum) + %td= link_to 'Destroy', kantum, method: :delete, data: { confirm: 'Are you sure?' } + +%br + += link_to 'New Kantum', new_kantum_path diff --git a/app/views/kanta/new.html.haml b/app/views/kanta/new.html.haml new file mode 100644 index 0000000..0524e57 --- /dev/null +++ b/app/views/kanta/new.html.haml @@ -0,0 +1,5 @@ +%h1 New kantum + += render 'form' + += link_to 'Back', kanta_path diff --git a/app/views/kanta/show.html.haml b/app/views/kanta/show.html.haml new file mode 100644 index 0000000..77afcf2 --- /dev/null +++ b/app/views/kanta/show.html.haml @@ -0,0 +1,18 @@ +.flex.justify-center + .flex.flex-col + = @image.attributes + + %a.underline{ '@click' => "hi"} + Back + +:opal + class Kanta + def initialize(hash) + hash.each do |k,v| + puts k + puts v + end + end + end + + kanta = Kanta.new(bg: 'bg-cyan-200') diff --git a/app/views/layouts/application.haml b/app/views/layouts/application.haml index 9aa533a..160b7eb 100644 --- a/app/views/layouts/application.haml +++ b/app/views/layouts/application.haml @@ -27,7 +27,8 @@ = csp_meta_tag = stylesheet_link_tag "tailwind" = stylesheet_link_tag "application" - + = javascript_include_tag "opal_main" + = javascript_importmap_tags - if false %script{:src => "https://cdn.tailwindcss.com"} diff --git a/config/initializers/opal.rb b/config/initializers/opal.rb new file mode 100644 index 0000000..aaba247 --- /dev/null +++ b/config/initializers/opal.rb @@ -0,0 +1,22 @@ +# Check out the full list of the available configuration options at +# https://github.com/opal/opal/blob/master/lib/opal/config.rb + +Rails.application.configure do + # We suggest keeping the configuration above as default for all environments, + # disabling some of them might slightly reduce the bundle size or reduce performance + # by degrading some ruby features. + config.opal.method_missing_enabled = true + config.opal.const_missing_enabled = true + config.opal.arity_check_enabled = true + config.opal.freezing_stubs_enabled = true + config.opal.dynamic_require_severity = :ignore + + # To enable passing assigns from the controller to the opal template handler + # change the following configuration to one of these values: + # + # - true # both locals and instance variables + # - :locals # only locals + # - :ivars # only instance variables + # + config.opal.assigns_in_templates = false +end diff --git a/config/routes.rb b/config/routes.rb index d5e869a..96bf0bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :kanta resources :pictures resources :stories diff --git a/lib/opal/rails/haml_filter.rb b/lib/opal/rails/haml_filter.rb new file mode 100644 index 0000000..6d41a8e --- /dev/null +++ b/lib/opal/rails/haml_filter.rb @@ -0,0 +1,19 @@ +require "haml" +require "haml/filters" +require "haml/filters/base" + +module Haml + class Filters + class Opal < Base + def compile(node) + temple = [:multi] + temple << [:static, ""] + temple + end + end + end +end + +Haml::Filters.registered[:opal] ||= Haml::Filters::Opal