Skip to content

Allow component inline rendering from controller #329

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2015
Merged
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
1 change: 1 addition & 0 deletions lib/react/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require 'react/rails/engine'
require 'react/rails/railtie'
require 'react/rails/view_helper'
require 'react/rails/controller_renderer'
17 changes: 17 additions & 0 deletions lib/react/rails/controller_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class React::Rails::ControllerRenderer
include React::Rails::ViewHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper

attr_accessor :output_buffer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw @garbles these few calls (include ActionView::... + attr_accessor :output_buffer), is this what's required to make your custom object work with view rendering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, I was trying to refactor the view helper a while back and got scared off by how things kept not working, maybe i'll give it another try


def self.call(*args, &block)
new.call(*args, &block)
end

def call(name, options, &block)
props = options.fetch(:props, {})
options = options.slice(:data, :tag).merge(prerender: true)
react_component(name, props, options, &block)
end
end
8 changes: 8 additions & 0 deletions lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class Railtie < ::Rails::Railtie
end
end

initializer "react_rails.add_component_renderer", group: :all do |app|
ActionController::Renderers.add :component do |component_name, options|
html = ::React::Rails::ControllerRenderer.call(component_name, options)
render_options = options.merge(inline: html)
render(render_options)
end
end

initializer "react_rails.bust_cache", group: :all do |app|
asset_variant = React::Rails::AssetVariant.new({
variant: app.config.react.variant,
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/server_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ def console_example_suppressed
React::ServerRendering.reset_pool
@todos = %w{todo1 todo2 todo3}
end

def inline_component
render component: 'TodoList', props: { todos: [{todo: 'Render this inline'}] }, tag: 'span'
end
end
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
collection do
get :console_example
get :console_example_suppressed
get :inline_component
end
end
end
9 changes: 9 additions & 0 deletions test/server_rendered_html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ def wait_to_ensure_asset_pipeline_detects_changes
assert_no_match(/console.warn/, response.body)
assert_no_match(/console.error/, response.body)
end

test 'react inline component rendering' do
get '/server/inline_component'
assert_match(/<span data-react-class=\"TodoList\"/, response.body)
# make sure that the items are prerendered
assert_match(/Render this inline<\/span>/, response.body)
# make sure that the layout is rendered with the component
assert_match(/<title>Dummy<\/title>/, response.body)
end
end