Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve styleguide #156

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/controllers/komponent/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions app/controllers/komponent/page_controller.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions app/controllers/komponent/styleguide_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/komponent/styleguide/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Styleguide</h1>

<% if components.any? %>
<% if @components.any? %>
<p>Select one of the components from the side to view its examples and documentation.</p>
<% else %>
<p><<strong>Hint:</strong> You haven't created any component yet</h2>
Expand Down
3 changes: 3 additions & 0 deletions app/views/komponent/styleguide/missing_page.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Page missing</h1>

<p>Please create <code>app/views/<%= @static_page_path %>.html.<%= Rails.application.config.app_generators.rails[:template_engine] || :erb %></code> file.</p>
1 change: 0 additions & 1 deletion app/views/komponent/styleguide/show.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/layouts/komponent.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<title>Styleguide</title>
<%= javascript_pack_tag 'komponent' %>
<%= stylesheet_pack_tag 'komponent', media: 'all' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= c 'komponent/header' %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

Komponent::Engine.routes.draw do
resources :styleguide, only: %i[index show]
resources :page, only: :show
end
1 change: 1 addition & 0 deletions lib/komponent/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
4 changes: 0 additions & 4 deletions lib/komponent/komponent_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 0 additions & 17 deletions test/komponent/komponent_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
%(<div class="all">🌎 😎</div>
Expand Down