Skip to content

Commit dfd97e6

Browse files
author
Nils Henning
committed
[FEATURE] make isolated components work in rails legayc views
1 parent 2ff5b93 commit dfd97e6

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%div{ class: 'matestack-isolated-component-root'}
2+
=yield

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def show
4040

4141
# this method gets called when the isolate vuejs component requests isolated content
4242
def render_isolated_content
43-
render :children if authorized?
43+
render :children_wrapper do
44+
render :children if authorized?
45+
end
4446
end
4547

4648
def authorized?

app/helpers/matestack/ui/core/application_helper.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ def render(*args)
4444
(matestack_arg < Matestack::Ui::Page)
4545
Rendering::MainRenderer.render(self, matestack_arg, options)
4646
else
47-
# if called from a controller and params contains a component key render without layout
48-
# else it was called from within an app/page and should render normally
49-
# if self.is_a?(ActionController::Base) && params&.dig(:component_key)
50-
# super layout: false
51-
# else
47+
if params[:component_class].present? && !((options = args.first).is_a?(Hash) && options[:html].present?)
48+
Rendering::MainRenderer.render(self, Matestack::Ui::Page, {})
49+
else
5250
super
53-
# end
51+
end
5452
end
5553
end
5654

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ services:
99
- "3000:3000"
1010
environment:
1111
RAILS_ENV: development
12+
links:
13+
- postgres_dummy
1214
volumes:
1315
- ./:/app
1416
- gem-volume:/usr/local/bundle
@@ -59,6 +61,17 @@ services:
5961
volumes:
6062
- test-data-volume:/var/lib/postgresql/data
6163

64+
postgres_dummy:
65+
image: postgres
66+
expose:
67+
- 5432
68+
environment:
69+
POSTGRES_USER: postgres
70+
POSTGRES_PASSWORD: postgres
71+
POSTGRES_DB: dummy
72+
volumes:
73+
- test-data-volume:/var/lib/postgresql/data
74+
6275
volumes:
6376
test-data-volume:
6477
gem-volume:

spec/dummy/app/matestack/components/legacy_views/pages/isolated.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ class Components::LegacyViews::Pages::Isolated < Matestack::Ui::IsolatedComponen
22

33
def response
44
div class: 'my-isolated' do
5-
plain I18n.l(DateTime.now)
5+
plain DateTime.now.strftime('%Q')
6+
end
7+
hr
8+
async rerender_on: 'async_update_time', id: 'foobar' do
9+
paragraph id: 'async-time', text: DateTime.now.strftime('%Q')
610
end
711
end
812

spec/dummy/config/database.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ test:
1313

1414
development:
1515
<<: *default
16-
database: development
16+
host: postgres_dummy
17+
database: dummy
1718

1819
staging:
1920
<<: *default

spec/test/components/dynamic/isolated/isolated_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def response
7070

7171
visit "/example"
7272
# used on page
73-
expect(page).to have_xpath('//div[@id="page-div"]/div[@class="matestack-isolated-component-container"]/div[@class="matestack-isolated-component-wrapper"]/div[@class="some-isolated-component"]/span[@id="text" and contains(.,"some isolated compenent")]')
73+
expect(page).to have_xpath('//div[@id="page-div"]/div[@class="matestack-isolated-component-container"]/div[@class="matestack-isolated-component-wrapper"]/div[@class="matestack-isolated-component-root"]/div[@class="some-isolated-component"]/span[@id="text" and contains(.,"some isolated compenent")]')
7474

7575
# used on component
76-
expect(page).to have_xpath('//div[@id="page-div"]/div[@class="some-other-component"]/div[@class="matestack-isolated-component-container"]/div[@class="matestack-isolated-component-wrapper"]/div[@class="some-isolated-component"]/span[@id="text" and contains(.,"some isolated compenent")]')
76+
expect(page).to have_xpath('//div[@id="page-div"]/div[@class="some-other-component"]/div[@class="matestack-isolated-component-container"]/div[@class="matestack-isolated-component-wrapper"]/div[@class="matestack-isolated-component-root"]/div[@class="some-isolated-component"]/span[@id="text" and contains(.,"some isolated compenent")]')
7777

7878
end
7979

spec/test/components/legacy_views/isolated_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@
88

99
expect(page).to have_content('Isolated Custom Component')
1010
expect(page).to have_css('.my-isolated')
11+
expect(page).to have_css('#async-time')
1112
initial_timestamp = page.find(".my-isolated").text #initial page load
12-
sleep
13+
1314
page.execute_script('MatestackUiCore.matestackEventHub.$emit("update_time")')
1415
expect(page).to have_content('Isolated Custom Component')
1516
expect(page).to have_css('.my-isolated')
17+
expect(page).to have_css('#async-time')
1618
expect(page).not_to have_content(initial_timestamp)
19+
async_timestamp = page.find("#async-time").text #initial page load
20+
21+
expect(page).to have_content(async_timestamp, count: 2)
22+
# rerender async inside isolate
23+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("async_update_time")')
24+
expect(page).to have_css('.my-isolated')
25+
expect(page).to have_css('#async-time')
26+
expect(page).to have_content(async_timestamp, count: 1) # isolate component still has same timestamp, async has different
1727
end
1828

1929
end

0 commit comments

Comments
 (0)