-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65e7821
commit aa8b823
Showing
25 changed files
with
176 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'coveralls' | ||
|
||
SimpleCov.minimum_coverage 90 | ||
SimpleCov.formatter = Coveralls::SimpleCov::Formatter | ||
|
||
SimpleCov.start do | ||
add_filter "/fixtures/" | ||
add_filter "/test/" | ||
add_filter "/features/" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'simplecov' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="foo"><%= properties[:bar] %></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./foo.scss"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.foo { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module FooComponent | ||
extend ComponentHelper | ||
|
||
property :bar, default: "Foobar" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div class="hello"><%= t(".hello") %></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
en: | ||
hello_component: | ||
hello: "Hello" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fr: | ||
hello_component: | ||
hello: "Bonjour" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./hello.scss"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.hello { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module HelloComponent | ||
extend ComponentHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module Komponent | ||
class Renderer | ||
attr_reader :context | ||
|
||
def initialize(controller) | ||
@context = controller.view_context.dup | ||
@view_renderer = @context.view_renderer = @context.view_renderer.dup | ||
@lookup_context = @view_renderer.lookup_context = @view_renderer.lookup_context.dup | ||
end | ||
|
||
def render(component, locals = {}, &block) | ||
parts = component.split("/") | ||
component_name = parts.join("_") | ||
|
||
component_module_path = resolved_component_path(component) | ||
.join("#{component_name}_component") | ||
require_dependency(component_module_path) | ||
component_module = "#{component_name}_component".camelize.constantize | ||
|
||
@context.class_eval { prepend component_module } | ||
@context.class_eval { prepend Komponent::Translation } | ||
|
||
@lookup_context.prefixes = ["components/#{component}"] | ||
|
||
capture_block = proc { capture(&block) } if block | ||
|
||
@context.instance_eval do | ||
if component_module.respond_to?(:properties) | ||
locals = locals.dup | ||
component_module.properties.each do |name, options| | ||
unless locals.has_key?(name) | ||
if options.has_key?(:default) | ||
locals[name] = options[:default] | ||
elsif options[:required] | ||
raise "Missing required component parameter: #{name}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
locals.each do |name, value| | ||
instance_variable_set(:"@#{name}", locals[name]) | ||
end | ||
|
||
define_singleton_method(:properties) { locals } | ||
define_singleton_method(:block_given_to_component?) { !!block } | ||
end | ||
|
||
begin | ||
@context.render("components/#{component}/#{parts.join('_')}", &capture_block) | ||
rescue ActionView::MissingTemplate | ||
warn "[DEPRECATION WARNING] `#{parts.last}` filename in namespace is deprecated in favor of `#{parts.join('_')}`." | ||
@context.render("components/#{component}/#{parts.last}", &capture_block) | ||
end | ||
end | ||
|
||
private | ||
|
||
def resolved_component_path(component) | ||
Komponent::ComponentPathResolver.new.resolve(component) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class FakeController < ApplicationController | ||
def initialize(method_name = nil, &method_body) | ||
if method_name and block_given? | ||
self.class.send(:define_method, method_name, method_body) | ||
Rails.application.routes.draw do | ||
get method_name, to: "fake##{method_name}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
class ComponentRendererTest < ActionController::TestCase | ||
def test_ | ||
@controller = FakeController.new | ||
|
||
renderer = Komponent::Renderer.new(@controller) | ||
renderer.render('all', text: 'hello world') | ||
@context = renderer.context | ||
|
||
assert_respond_to @context, :block_given_to_component? | ||
assert_respond_to @context, :properties | ||
assert_respond_to @context, :translate | ||
assert_equal @context.instance_variable_get(:'@text'), 'hello world' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'simplecov' | ||
|
||
ENV['RAILS_ENV'] ||= 'test' | ||
require File.expand_path('../../fixtures/my_app/config/environment', __FILE__) | ||
require 'rails/test_help' |