Kombu
Kombu provides the ability to render the specified component directly from the controller.
This gem may replace any javascript component except layouts in the views
directory.
This gem was inspired by the following projects.
Usage
Setup
Please configure the following settings according to your application.
Rails.application.configure do
config.kombu.javascript_entry_tag_proc = -> { helpers.javascript_pack_tag(@entry, defer: true) }
config.kombu.stylesheet_entry_tag_proc = -> { helpers.stylesheet_pack_tag(@entry) }
end
Use Kombu::Renderable
If you include Kombu::Renderable
in any controller, you can use kombu_render_component
which can render component directly.
class ArticlesController < ApplicationController
include Kombu::Renderable
def index
@title = 'Articles'
@articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
kombu_render_component('article-index-page', attributes: { 'title': @title, ':articles': @articles.to_json })
</artile-index-page>
end
end
(Optional) Rename method name.
If you do not like the method name, you can change it to any name you like.
module ComponentRenderable
extend ActiveSupport::Concern
include Kombu::Renderable
included do
alias render_component kombu_render_component
end
end
class ArticlesController < ApplicationController
include ComponentRenderable
def index
@title = 'Articles'
@articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
render_component('article-index-page', attributes: { 'title': @title, ':articles': @articles.to_json })
end
end
Testing (RSpec only)
Kombu provides a matcher to validate values passed to kombu_render_component
.
You can use kombu_component_rendered
by enabling it in RSpec.configure
.
require 'kombu/test/rspec/matchers/component_renderd_matcher'
RSpec.configure do |config|
config.include Kombu::RSpec::Matchers::ComponentRenderedMatcher, type: :request
end
describe 'GET /articles', type: :request do
before { get articles_path }
it 'Specific arguments must be passed to render_component.' do
title = 'Articles'
articles = [{ id: 1, title: 'artile1', body: 'body1' }, { id: 2, title: 'artile2', body: 'body2' }]
expect(controller).to kombu_component_rendered('article-index-page', attributes: { 'title': title, ':articles': articles.to_json })
end
end
How do it work
See lib/kombu/renderable.rb
.
Installation
Add this line to your application's Gemfile:
gem "kombu"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install kombu
Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.