Skip to content

Commit 22886a0

Browse files
author
Nils Henning
committed
add rails_view component to render rails legacy views
1 parent d7779dc commit 22886a0

File tree

29 files changed

+604
-74
lines changed

29 files changed

+604
-74
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def my_simple_slot
363363
end
364364
```
365365

366-
### Components in Rails Legacy Views
366+
### Components in Rails Views
367367

368368
It is now possible to use core and custom components in your Rails legacy views. Matestack provides a `matestack_component` helper to use components in views and partials.
369369

@@ -395,6 +395,17 @@ And use it in your view as follows:
395395
<%= matestack_component(:header_component, title: 'A Title') %>
396396
```
397397

398+
### Rails Views in Matestack Pages/Components
399+
400+
To render existing rails views inside your components or pages use the new `rails_view` component. It replaces the old `html` component.
401+
You can render existing partials and views with this helper anywhere in your app.
402+
Views/Partials that are rendered with a `rails_view` component can access instance variables of the corresponding controller
403+
or you can pass data directly as a hash to it and access it in your view. See below for further details and examples.
404+
405+
Rails View in a component:
406+
```
407+
```
408+
398409

399410
### Removed Components
400411

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ group :development, :test do
3030
gem 'pry-rails'
3131
gem 'pry-byebug'
3232
gem 'turbolinks'
33+
gem 'simple_form'
3334
end
3435

3536
group :test do

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ GEM
221221
selenium-webdriver (3.142.7)
222222
childprocess (>= 0.5, < 4.0)
223223
rubyzip (>= 1.2.2)
224+
simple_form (5.0.2)
225+
actionpack (>= 5.0)
226+
activemodel (>= 5.0)
224227
simplecov (0.18.1)
225228
docile (~> 1.1)
226229
simplecov-html (~> 0.11.0)
@@ -290,6 +293,7 @@ DEPENDENCIES
290293
rspec-retry
291294
rspec-wait (~> 0.0.9)
292295
selenium-webdriver (~> 3.142, >= 3.142.7)
296+
simple_form
293297
simplecov
294298
sqlite3 (~> 1.4)
295299
trailblazer

app/concepts/matestack/ui/core/component/base.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,20 @@ def initialize(model = nil, options = {})
5050
# and it just grabs @options[:context]
5151
@controller_context = context&.fetch(:controller_context, nil)
5252

53+
# Add matestack context, containing controller etc.
54+
@matestack_context = options.dig(:matestack_context)
55+
5356
# TODO: technically only relevant for Dynamic, however it relies on
5457
# @options being set but must be set before `setup` is called.
5558
# As both happen in this controller it isn't possible to squeeze
5659
# it inbetween the super calls in the Dynamic super class.
5760
#
5861
# This is the configuration for the VueJS component
59-
@component_config = @options.except(:context, :children, :url_params, :included_config)
62+
@component_config = @options.except(:context, :children, :url_params, :included_config, :matestack_context)
6063

6164
# TODO: no idea why this is called `url_params` it contains
6265
# much more than this e.g. almost all params so maybe rename it?
63-
@url_params = context&.[](:params)&.except(:action, :controller, :component_key)
66+
@url_params = context&.[](:params)&.except(:action, :controller, :component_key, :matestack_context)
6467

6568
# used when creating the child component tree
6669
# if true, the block of an async component with a defer value will not be processed
@@ -309,26 +312,34 @@ def yield_components
309312
# should work currently
310313
def add_context_to_options(args, included_config=nil)
311314
case args.size
312-
when 0 then [{context: context, included_config: included_config}]
315+
when 0 then [
316+
{
317+
context: context,
318+
included_config: included_config,
319+
}
320+
]
313321
when 1 then
314322
arg = args.first
315323
if arg.is_a?(Hash)
316324
arg[:context] = context
317325
arg[:included_config] = included_config
326+
arg[:matestack_context] = @matestack_context
318327
[arg]
319328
else
320-
[arg, {context: context, included_config: included_config}]
329+
[arg, {context: context, included_config: included_config, matestack_context: @matestack_context}]
321330
end
322331
when 2 then
323332
if args[1] == :include
324333
if args.first.is_a?(Hash)
325334
args.first[:context] = context
326335
args.first[:included_config] = included_config
336+
args.first[:matestack_context] = @matestack_context
327337
[args.first]
328338
end
329339
else
330340
args[1][:context] = context
331341
args[1][:included_config] = included_config
342+
args[1][:matestack_context] = @matestack_context
332343
[args.first, args[1]]
333344
end
334345
else

app/concepts/matestack/ui/core/html/html.haml

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/concepts/matestack/ui/core/html/html.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/concepts/matestack/ui/core/html/html.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/concepts/matestack/ui/core/js/core.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import pageContent from '../page/content'
88
import store from '../app/store'
99
import component from '../component/component'
1010
import anonymDynamicComponent from '../component/anonym-dynamic-component'
11-
import html from '../html/html'
1211
import transition from '../transition/transition'
1312
import action from '../action/action'
1413
import form from '../form/form'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
= include_partial
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require_dependency "cell/partial"
2+
3+
module Matestack::Ui::Core::View
4+
class View < Matestack::Ui::Core::Component::Static
5+
include Cell::ViewModel::Partial
6+
view_paths << "#{::Rails.root}/app/views"
7+
8+
# Only one option should be set at a time. If view is set partial will be ignored.
9+
optional view: { as: :view_path } # Specify a view path to render
10+
optional partial: { as: :partial_path } # Specifiy a partial to render
11+
12+
def vue_js_component_name
13+
'matestack-ui-core-rails-view'
14+
end
15+
16+
def include_partial
17+
controller = @view_context.present? ? @view_context.controller : options[:matestack_context][:controller]
18+
if view_path
19+
controller.render_to_string view_path, layout: false, locals: locals
20+
elsif partial_path
21+
controller.render_to_string partial: partial_path, layout: false, locals: locals
22+
else
23+
raise 'view or partial param missing for RailsView Component'
24+
end
25+
end
26+
27+
private
28+
29+
def locals
30+
options.except(:matestack_context, :partial, :view, :partial_path, :view_path, :included_config, :context)
31+
end
32+
33+
end
34+
end

0 commit comments

Comments
 (0)