Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 3ca2317

Browse files
committed
Support rendering components from controllers
1 parent f2302b2 commit 3ca2317

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
svelte-rails (0.1.0)
4+
svelte-rails (0.2.0)
55
execjs
66
railties (>= 5.2)
77

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,22 @@ https://github.com/nning/svelte-rails-demo/commits/master
3131
<%= svelte_component :Hello, name: 'Svelte' %>
3232
```
3333

34+
## Controller Renderer
35+
36+
```ruby
37+
class TodoController < ApplicationController
38+
def index
39+
@todos = Todo.all
40+
render component: 'TodoList', props: { todos: @todos }
41+
end
42+
end
43+
```
44+
45+
`prerender` is activated by default, can be disabled with `prerender: false`.
46+
3447
## Missing Features
3548

3649
* HMR and Bundle consistency (server-rendered HTML is cached and client-side updates on changes to the sources)
37-
* Render components directly from controllers
3850
* Generator for components
3951
* Render pools
4052
* Better documentation for setup

lib/svelte/rails.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ module Rails
77
end
88

99
require 'svelte/rails/view_helper'
10+
require 'svelte/rails/controller_renderer'
11+
1012
require 'svelte/rails/railtie' if defined?(Rails)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'svelte/rails/view_helper'
2+
3+
module Svelte
4+
module Rails
5+
class ControllerRenderer
6+
include Svelte::Rails::ViewHelper
7+
# include ActionView::Helpers::TagHelper
8+
# include ActionView::Helpers::TextHelper
9+
10+
attr_accessor :output_buffer
11+
12+
# @return [String] HTML for `component_name` with `options[:props]`
13+
def call(component_name, options, &block)
14+
props = options.fetch(:props, {})
15+
options = default_options.merge(options.slice(:data, :aria, :tag, :class, :id, :prerender, :camelize_props))
16+
svelte_component(component_name, props, options, &block)
17+
end
18+
19+
private
20+
21+
def default_options
22+
{ prerender: true }
23+
end
24+
end
25+
end
26+
end

lib/svelte/rails/railtie.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

33
module Svelte::Rails
44
class Railtie < ::Rails::Railtie
5-
initializer 'react_rails.setup_view_helpers', after: :load_config_initializers, group: :all do |app|
5+
initializer 'svelte_rails.setup_view_helpers', after: :load_config_initializers, group: :all do |app|
66
ActiveSupport.on_load(:action_view) do
77
include ::Svelte::Rails::ViewHelper
88
end
99
end
1010

11+
initializer 'svelte_rails.add_component_renderer', group: :all do |app|
12+
ActionController::Renderers.add :component do |component_name, options|
13+
renderer = ::Svelte::Rails::ControllerRenderer.new
14+
html = renderer.call(component_name, options)
15+
render_options = options.merge(inline: html)
16+
render(render_options)
17+
end
18+
end
19+
1120
rake_tasks do
1221
load 'svelte/rails/install_task.rake'
1322
end

lib/svelte/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Svelte
22
module Rails
3-
VERSION = "0.1.0"
3+
VERSION = '0.2.0'
44
end
55
end

0 commit comments

Comments
 (0)