Skip to content

Commit 9948809

Browse files
author
Nils Henning
committed
[FEATURE] added cable component, updated async, added/fixed tests
1 parent 75cfcdf commit 9948809

File tree

16 files changed

+99
-38
lines changed

16 files changed

+99
-38
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
%component{dynamic_tag_attributes}
1+
%component{dynamic_tag_attributes.merge('initial-template': "#{render_content}")}
22
%div{class: "matestack-cable-component-container", "v-bind:class": "{ 'loading': loading === true }"}
3-
%div{class: "matestack-cable-component-wrapper", "v-if": "cableTemplate == null", "v-bind:class": "{ 'loading': loading === true }"}
4-
= render_content
53
%div{class: "matestack-cable-component-wrapper", "v-if": "cableTemplate != null", "v-bind:class": "{ 'loading': loading === true }"}
64
%v-runtime-template{":template":"cableTemplate"}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import componentMixin from '../component/component'
55

66
const componentDef = {
77
mixins: [componentMixin],
8+
props: {
9+
initialTemplate: String,
10+
},
811
data: function(){
912
return {
1013
cableTemplate: null,
@@ -66,11 +69,12 @@ const componentDef = {
6669
return payload.data
6770
},
6871
},
69-
created: function () {
70-
this.cableTemplateDomElement = document.querySelector("#" + this.componentConfig["id"])
71-
this.cableTemplate = this.cableTemplateDomElement.outerHTML
72+
mounted: function() {
7273
const self = this
73-
var events = this.componentConfig['append_on']
74+
var dom_elem = document.createElement('div')
75+
dom_elem.innerHTML = this.initialTemplate
76+
this.cableTemplateDomElement = dom_elem.querySelector("#" + this.componentConfig["id"])
77+
this.cableTemplate = this.cableTemplateDomElement.outerHTML
7478
this.registerEvents(this.componentConfig['append_on'], self.append)
7579
this.registerEvents(this.componentConfig['prepend_on'], self.prepend)
7680
this.registerEvents(this.componentConfig['delete_on'], self.delete)
@@ -79,6 +83,7 @@ const componentDef = {
7983
},
8084
beforeDestroy: function() {
8185
const self = this
86+
this.cableTemplate = null
8287
this.removeEvents(this.componentConfig['append_on'], self.append)
8388
this.removeEvents(this.componentConfig['prepend_on'], self.prepend)
8489
this.removeEvents(this.componentConfig['delete_on'], self.delete)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class Cable < Matestack::Ui::Core::Component::Dynamic
44

55
requires :id
66

7-
def response
7+
def setup
88
component_config[:component_key] = id
99
end
10-
10+
1111
def show
1212
render :cable
1313
end

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,10 @@ def prepare
165165
def params
166166
if @matestack_context.present? && @matestack_context[:controller].present?
167167
@matestack_context[:controller].params
168-
else
168+
elsif context.present? && context[:params]
169169
context[:params]
170+
else
171+
ActionController::Parameters.new({})
170172
end
171173
end
172174

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def responder_for(*args)
9999
end
100100

101101
def matestack_component(component, options = {}, &block)
102-
context = (options[:matestack_context] ||= {}).merge(controller: @_controller || self)
102+
controller = (self.class <= ActionController::Base) ? self : @_controller
103+
context = (options[:matestack_context] ||= {}).merge(controller: controller)
103104
Matestack::Ui::Core::Component::Base
104105
.new(options.merge(matestack_context: context))
105106
.send(component, options.merge(matestack_context: context), &block).to_s

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Components::LegacyViews::Pages::Isolated < Matestack::Ui::IsolatedComponent
22

33
def response
4+
time = DateTime.now.strftime('%Q')
45
div class: 'my-isolated' do
5-
plain DateTime.now.strftime('%Q')
6+
plain time
67
end
78
hr
89
async rerender_on: 'async_update_time', id: 'foobar' do
9-
paragraph id: 'async-time', text: DateTime.now.strftime('%Q')
10+
paragraph id: 'async-time', text: time
1011
end
1112
end
1213

spec/dummy/app/matestack/demo/pages/my_first_page.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def prepare
1010
end
1111

1212
def response
13-
heading size: 2, text: "This is Page 1"
13+
# heading size: 2, text: "This is Page 1"
1414

15-
div id: "some-id", class: "some-class" do
16-
plain "hello from page 1"
17-
end
15+
# div id: "some-id", class: "some-class" do
16+
# plain "hello from page 1"
17+
# end
1818

1919

20-
my_card
21-
my_demo_card
20+
# my_card
21+
# my_demo_card
2222

23-
foo_fancy_stuff title: 'Huhu'
23+
# foo_fancy_stuff title: 'Huhu'
2424

2525

2626
# ul do

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
unless Rails.application.routes.url_helpers.method_defined?(:matestack_components_test_path)
146146
Rails.application.routes.append do
147147
get '/matestack_components_test', to: 'matestack_components#matestack_components_test', as: :matestack_components_test
148+
get '/matestack_transition_test', to: 'matestack_components#matestack_transition_test', as: :matestack_transition_test
148149
end
149150
Rails.application.reload_routes!
150151
end

spec/test/components/dynamic/cable/cable_spec.rb

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'rails_helper'
22

3-
describe "Cable Component - append", type: :feature, js: true do
3+
describe "Cable Component", type: :feature, js: true do
44
include Utils
5+
include Matestack::Ui::Core::ApplicationHelper
56

67
it 'should have the typical matestack wrapping classes' do
78
matestack_render do
@@ -24,8 +25,46 @@
2425
end
2526

2627
it 'should work after a page transition' do
27-
pending
28-
fail
28+
matestack_render(reset_app: false, page: MatestackTransitionPage) do
29+
cable id: 'cable-replace', replace_on: 'replace' do
30+
paragraph id: 'id', text: 'Paragraph'
31+
end
32+
end
33+
matestack_render(reset_app: false) do
34+
transition path: :matestack_transition_test_path, text: 'Transition'
35+
end
36+
expect(page).to have_content('Transition')
37+
expect(page).not_to have_content('Paragraph')
38+
click_on 'Transition'
39+
expect(page).not_to have_content('Transition')
40+
expect(page).to have_selector('p#id', text: 'Paragraph')
41+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("replace", { data: "<h1 id=\"id\">replaced</h1>" })')
42+
expect(page).to have_selector('h1#id', text: 'replaced')
43+
reset_matestack_app
44+
end
45+
46+
it 'should be possible to add vue.js components' do
47+
matestack_render do
48+
cable id: 'cable-replace', replace_on: 'replace' do
49+
paragraph text: 'paragraph', id: 'id'
50+
end
51+
toggle show_on: 'test' do
52+
plain 'event successful emitted'
53+
end
54+
end
55+
expect(page).to have_selector('p#id', text: 'paragraph')
56+
expect(page).not_to have_selector('button', text: 'Click')
57+
expect(page).not_to have_content('event successful emitted')
58+
# onclick = matestack_component(:heading, text: 'test')
59+
onclick = matestack_component(:onclick, emit: :test) { button text: 'Click' }
60+
script = "MatestackUiCore.matestackEventHub.$emit('replace', { data: \"#{onclick}\" })".gsub("\n", "")
61+
page.execute_script(script)
62+
expect(page).not_to have_selector('p#id', text: 'paragraph')
63+
expect(page).to have_selector('button', text: 'Click')
64+
expect(page).not_to have_content('event successful emitted')
65+
66+
click_on 'Click'
67+
expect(page).to have_content('event successful emitted')
2968
end
3069

3170
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def response
370370
visit "/example"
371371

372372
page_timestamp = page.find('#page-timestamp').text.to_i
373-
component_timestamp = page.find('#isolated-component-timestamp').text.to_i
373+
component_timestamp = page.find('#isolated-component-timestamp', wait: 3).text.to_i
374374

375375
expect(component_timestamp-page_timestamp).to be >= 2
376376

0 commit comments

Comments
 (0)