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
551603a
commit f6ce86a
Showing
26 changed files
with
201 additions
and
66 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,7 @@ | ||
SimpleCov.minimum_coverage 90 | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,4 @@ install: | |
- yarn | ||
|
||
script: | ||
- bundle exec rubocop | ||
- bundle exec rake test | ||
- bundle exec rake test:all_with_coverage |
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 |
---|---|---|
@@ -1,25 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rubocop/rake_task" | ||
require "rake/testtask" | ||
require 'cucumber' | ||
require 'cucumber/rake/task' | ||
require "cucumber" | ||
require "cucumber/rake/task" | ||
require "coveralls/rake/task" | ||
|
||
namespace :test do | ||
task all: [:unit, :cucumber] | ||
task all: [:rubocop, :unit, :cucumber] | ||
task all_with_coverage: [:all, "coveralls:push"] | ||
|
||
RuboCop::RakeTask.new | ||
|
||
Rake::TestTask.new(:unit) do |t| | ||
t.libs << "test" | ||
t.libs << "lib" | ||
t.test_files = FileList["test/**/*_test.rb"] | ||
t.verbose = true | ||
t.warning = false | ||
end | ||
|
||
Cucumber::Rake::Task.new(:cucumber) do |t| | ||
t.cucumber_opts = "--format pretty" | ||
end | ||
|
||
Coveralls::RakeTask.new | ||
end | ||
|
||
task test: 'test:all' | ||
task test: "test:all" | ||
|
||
task default: :test |
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Komponent | ||
class ComponentRenderer | ||
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 | ||
|
||
instance_variable_set(:"@block_given_to_component", capture_block) | ||
|
||
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,43 @@ | ||
# 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_methods_are_accessible_in_context | ||
@controller = FakeController.new | ||
|
||
renderer = Komponent::ComponentRenderer.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' | ||
assert_nil @context.instance_variable_get(:'@block_given_to_component') | ||
end | ||
|
||
def test_block | ||
@controller = FakeController.new | ||
|
||
renderer = Komponent::ComponentRenderer.new(@controller) | ||
renderer.render('all') do | ||
"<p>HELLO</p>" | ||
end | ||
@context = renderer.context | ||
|
||
assert_equal @context.block_given_to_component?, true | ||
assert_not_nil @context.instance_variable_get(:'@block_given_to_component') | ||
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' |