From 034eaea8f9a9c69f29aaa01996ef8cbdaaaa3b32 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 21:50:49 +0100 Subject: [PATCH 1/7] Use ActionController::Base instead ApplicationController for Styleguide --- app/controllers/komponent/styleguide_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/komponent/styleguide_controller.rb b/app/controllers/komponent/styleguide_controller.rb index 5c8e8cd..849f0e8 100644 --- a/app/controllers/komponent/styleguide_controller.rb +++ b/app/controllers/komponent/styleguide_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Komponent - class StyleguideController < ::ApplicationController + class StyleguideController < ActionController::Base layout 'komponent' rescue_from ActionView::MissingTemplate, with: :missing_template From bb4a0ad95d062ba400cafe3d71c8dfeae6e63f3c Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 21:51:42 +0100 Subject: [PATCH 2/7] Remove components helper and move into Komponent::StyleguideController --- app/controllers/komponent/styleguide_controller.rb | 5 +++++ app/views/komponent/styleguide/index.html.erb | 2 +- lib/komponent/komponent_helper.rb | 4 ---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/komponent/styleguide_controller.rb b/app/controllers/komponent/styleguide_controller.rb index 849f0e8..911ed38 100644 --- a/app/controllers/komponent/styleguide_controller.rb +++ b/app/controllers/komponent/styleguide_controller.rb @@ -5,6 +5,7 @@ class StyleguideController < ActionController::Base layout 'komponent' rescue_from ActionView::MissingTemplate, with: :missing_template + before_action :set_components def index; end def show @@ -13,6 +14,10 @@ def show private + def set_components + @components = Komponent::Component.all + end + def missing_template render 'komponent/styleguide/missing_template', status: :not_found end diff --git a/app/views/komponent/styleguide/index.html.erb b/app/views/komponent/styleguide/index.html.erb index 9c51264..f435b71 100644 --- a/app/views/komponent/styleguide/index.html.erb +++ b/app/views/komponent/styleguide/index.html.erb @@ -1,6 +1,6 @@

Styleguide

-<% if components.any? %> +<% if @components.any? %>

Select one of the components from the side to view its examples and documentation.

<% else %>

<Hint: You haven't created any component yet diff --git a/lib/komponent/komponent_helper.rb b/lib/komponent/komponent_helper.rb index bc8a564..f02bb28 100644 --- a/lib/komponent/komponent_helper.rb +++ b/lib/komponent/komponent_helper.rb @@ -17,10 +17,6 @@ def component(component_name, locals = {}, options = {}, &block) end alias :c :component - def components - Komponent::Component.all - end - def component_with_doc(component_name, locals = {}, options = {}, &block) captured_output = component(component_name, locals, options, &block) From 471201f7fc604b07d0352461acd39c6c6ef9b498 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 21:52:11 +0100 Subject: [PATCH 3/7] Set locale in Komponent::StyleguideController --- app/controllers/komponent/styleguide_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/komponent/styleguide_controller.rb b/app/controllers/komponent/styleguide_controller.rb index 911ed38..3add78a 100644 --- a/app/controllers/komponent/styleguide_controller.rb +++ b/app/controllers/komponent/styleguide_controller.rb @@ -6,6 +6,8 @@ class StyleguideController < ActionController::Base rescue_from ActionView::MissingTemplate, with: :missing_template before_action :set_components + around_action :set_locale + def index; end def show @@ -21,5 +23,11 @@ def set_components def missing_template render 'komponent/styleguide/missing_template', status: :not_found end + + def set_locale + I18n.with_locale(params[:locale] || I18n.default_locale) do + yield + end + end end end From 2451d488a79ea5412d9baaeef7d3265440973b08 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 22:01:01 +0100 Subject: [PATCH 4/7] Cleanup tests --- test/komponent/komponent_helper_test.rb | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/test/komponent/komponent_helper_test.rb b/test/komponent/komponent_helper_test.rb index f9ac73e..4a19e06 100644 --- a/test/komponent/komponent_helper_test.rb +++ b/test/komponent/komponent_helper_test.rb @@ -78,23 +78,6 @@ def test_helper_supports_content_for_across_components component('pong').chomp end - def test_helper_lists_components - assert_equal( - [ - 'all', - 'bar', - 'foo', - 'foo_bar', - 'hello', - 'ping', - 'pong', - 'required', - 'world', - ], - components.keys - ) - end - def test_helper_renders_with_doc assert_equal \ %(

🌎 😎
From 8ce8f45b61d8f776aef5d971a86f400b7e8c0048 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 23:23:03 +0100 Subject: [PATCH 5/7] wip: support static pages --- .../komponent/application_controller.rb | 29 +++++++++++++++++++ app/controllers/komponent/page_controller.rb | 17 +++++++++++ .../komponent/styleguide_controller.rb | 18 ++---------- .../styleguide/missing_page.html.erb | 3 ++ app/views/komponent/styleguide/show.html.erb | 1 - config/routes.rb | 1 + 6 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 app/controllers/komponent/application_controller.rb create mode 100644 app/controllers/komponent/page_controller.rb create mode 100644 app/views/komponent/styleguide/missing_page.html.erb delete mode 100644 app/views/komponent/styleguide/show.html.erb diff --git a/app/controllers/komponent/application_controller.rb b/app/controllers/komponent/application_controller.rb new file mode 100644 index 0000000..8ef1e27 --- /dev/null +++ b/app/controllers/komponent/application_controller.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Komponent + class ApplicationController < ActionController::Base + layout 'komponent' + + before_action :set_components + before_action :set_static_pages + around_action :set_locale + + private + + def set_components + @components = Komponent::Component.all + end + + def set_static_pages + @static_pages ||= Dir["#{Rails.root.join("app/views/komponent/static/*.html.*")}"].map do |path| + Pathname.new(path).basename(".*").basename(".*").to_s + end + end + + def set_locale + I18n.with_locale(params[:locale] || I18n.default_locale) do + yield + end + end + end +end diff --git a/app/controllers/komponent/page_controller.rb b/app/controllers/komponent/page_controller.rb new file mode 100644 index 0000000..3a96052 --- /dev/null +++ b/app/controllers/komponent/page_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Komponent + class PageController < Komponent::ApplicationController + rescue_from ActionView::MissingTemplate, with: :missing_page + + def show + render template: "komponent/static/#{params[:id]}" + end + + private + + def missing_page + render 'komponent/styleguide/missing_page', status: :not_found + end + end +end diff --git a/app/controllers/komponent/styleguide_controller.rb b/app/controllers/komponent/styleguide_controller.rb index 3add78a..130bba1 100644 --- a/app/controllers/komponent/styleguide_controller.rb +++ b/app/controllers/komponent/styleguide_controller.rb @@ -1,33 +1,21 @@ # frozen_string_literal: true module Komponent - class StyleguideController < ActionController::Base - layout 'komponent' + class StyleguideController < Komponent::ApplicationController rescue_from ActionView::MissingTemplate, with: :missing_template - before_action :set_components - around_action :set_locale - def index; end def show @component = Komponent::Component.find(params[:id]) + + render template: @component.examples_view end private - def set_components - @components = Komponent::Component.all - end - def missing_template render 'komponent/styleguide/missing_template', status: :not_found end - - def set_locale - I18n.with_locale(params[:locale] || I18n.default_locale) do - yield - end - end end end diff --git a/app/views/komponent/styleguide/missing_page.html.erb b/app/views/komponent/styleguide/missing_page.html.erb new file mode 100644 index 0000000..bcadd80 --- /dev/null +++ b/app/views/komponent/styleguide/missing_page.html.erb @@ -0,0 +1,3 @@ +

Page missing

+ +

Please create app/views/komponent/static/<%= params[:id] %>.html.<%= Rails.application.config.app_generators.rails[:template_engine] || :erb %> file.

diff --git a/app/views/komponent/styleguide/show.html.erb b/app/views/komponent/styleguide/show.html.erb deleted file mode 100644 index 716aad5..0000000 --- a/app/views/komponent/styleguide/show.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render partial: @component.examples_view %> diff --git a/config/routes.rb b/config/routes.rb index 7293246..6aee3be 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,5 @@ Komponent::Engine.routes.draw do resources :styleguide, only: %i[index show] + resources :page, only: :show end From ff48f877c1b4db1d1dabca2896d3421595e11f7c Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 23:38:50 +0100 Subject: [PATCH 6/7] wip: support static pages --- app/controllers/komponent/application_controller.rb | 8 ++++++-- app/controllers/komponent/page_controller.rb | 4 +++- app/views/komponent/styleguide/missing_page.html.erb | 2 +- lib/komponent/engine.rb | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/komponent/application_controller.rb b/app/controllers/komponent/application_controller.rb index 8ef1e27..04f7fd0 100644 --- a/app/controllers/komponent/application_controller.rb +++ b/app/controllers/komponent/application_controller.rb @@ -15,9 +15,9 @@ def set_components end def set_static_pages - @static_pages ||= Dir["#{Rails.root.join("app/views/komponent/static/*.html.*")}"].map do |path| + @static_pages ||= Dir["#{Rails.root.join("app/views/#{static_page_root}/*.html.*")}"].map do |path| Pathname.new(path).basename(".*").basename(".*").to_s - end + end.sort end def set_locale @@ -25,5 +25,9 @@ def set_locale yield end end + + def static_page_root + Rails.application.config.komponent.static_root + end end end diff --git a/app/controllers/komponent/page_controller.rb b/app/controllers/komponent/page_controller.rb index 3a96052..beffcf4 100644 --- a/app/controllers/komponent/page_controller.rb +++ b/app/controllers/komponent/page_controller.rb @@ -5,7 +5,9 @@ class PageController < Komponent::ApplicationController rescue_from ActionView::MissingTemplate, with: :missing_page def show - render template: "komponent/static/#{params[:id]}" + @static_page_path = "#{static_page_root}/#{params[:id]}" + + render template: @static_page_path end private diff --git a/app/views/komponent/styleguide/missing_page.html.erb b/app/views/komponent/styleguide/missing_page.html.erb index bcadd80..0034ed3 100644 --- a/app/views/komponent/styleguide/missing_page.html.erb +++ b/app/views/komponent/styleguide/missing_page.html.erb @@ -1,3 +1,3 @@

Page missing

-

Please create app/views/komponent/static/<%= params[:id] %>.html.<%= Rails.application.config.app_generators.rails[:template_engine] || :erb %> file.

+

Please create app/views/<%= @static_page_path %>.html.<%= Rails.application.config.app_generators.rails[:template_engine] || :erb %> file.

diff --git a/lib/komponent/engine.rb b/lib/komponent/engine.rb index 477160c..5bd313c 100644 --- a/lib/komponent/engine.rb +++ b/lib/komponent/engine.rb @@ -23,6 +23,7 @@ class Engine < Rails::Engine config.before_configuration do |app| app.config.komponent = config.komponent app.config.komponent.root = app.config.root.join("frontend") + app.config.komponent.static_root = "komponent/static" end config.after_initialize do |app| From 6b2d2bec2e89e719e3c4854e4593013dbce05051 Mon Sep 17 00:00:00 2001 From: Nicolas Brousse Date: Wed, 20 Nov 2019 23:52:42 +0100 Subject: [PATCH 7/7] Remove csrf_meta_tags from Styleguide layout --- app/views/layouts/komponent.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/layouts/komponent.html.erb b/app/views/layouts/komponent.html.erb index 972c1c8..5df150e 100644 --- a/app/views/layouts/komponent.html.erb +++ b/app/views/layouts/komponent.html.erb @@ -4,7 +4,6 @@ Styleguide <%= javascript_pack_tag 'komponent' %> <%= stylesheet_pack_tag 'komponent', media: 'all' %> - <%= csrf_meta_tags %> <%= c 'komponent/header' %>