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