diff --git a/app/controllers/komponent/application_controller.rb b/app/controllers/komponent/application_controller.rb new file mode 100644 index 0000000..04f7fd0 --- /dev/null +++ b/app/controllers/komponent/application_controller.rb @@ -0,0 +1,33 @@ +# 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/#{static_page_root}/*.html.*")}"].map do |path| + Pathname.new(path).basename(".*").basename(".*").to_s + end.sort + end + + def set_locale + I18n.with_locale(params[:locale] || I18n.default_locale) do + 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 new file mode 100644 index 0000000..beffcf4 --- /dev/null +++ b/app/controllers/komponent/page_controller.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Komponent + class PageController < Komponent::ApplicationController + rescue_from ActionView::MissingTemplate, with: :missing_page + + def show + @static_page_path = "#{static_page_root}/#{params[:id]}" + + render template: @static_page_path + 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 5c8e8cd..130bba1 100644 --- a/app/controllers/komponent/styleguide_controller.rb +++ b/app/controllers/komponent/styleguide_controller.rb @@ -1,14 +1,15 @@ # frozen_string_literal: true module Komponent - class StyleguideController < ::ApplicationController - layout 'komponent' + class StyleguideController < Komponent::ApplicationController rescue_from ActionView::MissingTemplate, with: :missing_template def index; end def show @component = Komponent::Component.find(params[:id]) + + render template: @component.examples_view end private 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/app/views/komponent/styleguide/missing_page.html.erb b/app/views/komponent/styleguide/missing_page.html.erb new file mode 100644 index 0000000..0034ed3 --- /dev/null +++ b/app/views/komponent/styleguide/missing_page.html.erb @@ -0,0 +1,3 @@ +

Page missing

+ +

Please create app/views/<%= @static_page_path %>.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/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' %> 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 diff --git a/lib/komponent/engine.rb b/lib/komponent/engine.rb index 29d50d8..66c537d 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| 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) 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 \ %(
🌎 😎