Skip to content

Commit 4543e28

Browse files
authored
Merge pull request #411 from matestack/origin/nh/render-components-with-view-context
added specs for component access tp action view context and added acc…
2 parents 38c8082 + 95fc52b commit 4543e28

15 files changed

+17286
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class App < Trailblazer::Cell
44
include Matestack::Ui::Core::Cell
55
include Matestack::Ui::Core::ApplicationHelper
66
include Matestack::Ui::Core::ToCell
7+
include Matestack::Ui::Core::HasViewContext
78

89
view_paths << "#{Matestack::Ui::Core::Engine.root}/app/concepts"
910

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ def show(component_key=nil, only_page=false)
8989
nodes_to_cell
9090
render :page
9191
when :render_page_with_app
92-
concept(@app_class).call(:show, @page_id, @nodes)
92+
@app_class.new(nil, context: {
93+
params: options[:context][:params],
94+
request: options[:context][:request],
95+
view_context: options[:context][:view_context]
96+
}, controller_instance: options[:controller_instance]).call(:show, @page_id, @nodes)
9397
when :render_component
9498
begin
9599
render_child_component component_key

spec/dummy/public/packs-test/js/application-523107e3f7258f36c3ed.js

Lines changed: 17049 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/dummy/public/packs-test/js/application-523107e3f7258f36c3ed.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/dummy/public/packs-test/js/application-a0dd669893d8c2bbbbb9.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*!
2+
* Determine if an object is a Buffer
3+
*
4+
* @author Feross Aboukhadijeh <https://feross.org>
5+
* @license MIT
6+
*/
7+
8+
/*!
9+
* Vue.js v2.6.10
10+
* (c) 2014-2019 Evan You
11+
* Released under the MIT License.
12+
*/
13+
14+
/**
15+
* vuex v3.1.1
16+
* (c) 2019 Evan You
17+
* @license MIT
18+
*/
Binary file not shown.

spec/dummy/public/packs-test/js/application-a0dd669893d8c2bbbbb9.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"application.js": "/packs-test/js/application-523107e3f7258f36c3ed.js",
3+
"application.js.map": "/packs-test/js/application-523107e3f7258f36c3ed.js.map",
4+
"entrypoints": {
5+
"application": {
6+
"js": [
7+
"/packs-test/js/application-523107e3f7258f36c3ed.js"
8+
],
9+
"js.map": [
10+
"/packs-test/js/application-523107e3f7258f36c3ed.js.map"
11+
]
12+
}
13+
}
14+
}
145 Bytes
Binary file not shown.

spec/support/capybara.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require "matestack/ui/core"
66

77

8+
Capybara.server_port = 33123
9+
Capybara.server_host = "0.0.0.0"
10+
811

912
# Capybara.app = Matestack::Ui::Core::Engine
1013
Capybara.app = Dummy::Application

spec/usage/base/app_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,60 @@ def page2
158158

159159
end
160160

161+
it "has access to view context" do
162+
163+
class Apps::ExampleApp < Matestack::Ui::App
164+
165+
def response
166+
components {
167+
heading size: 1, text: "My Example App Layout"
168+
if @view_context.view_renderer.instance_of?(ActionView::Renderer)
169+
plain "has access to ActionView Context"
170+
end
171+
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
172+
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
173+
main do
174+
page_content
175+
end
176+
}
177+
end
178+
179+
end
180+
181+
module Pages::ExampleApp
182+
183+
end
184+
185+
class Pages::ExampleApp::ExamplePage < Matestack::Ui::Page
186+
187+
def response
188+
components {
189+
div id: "my-div-on-page-1" do
190+
heading size: 2, text: "This is Page 1"
191+
end
192+
}
193+
end
194+
195+
end
196+
197+
198+
class ExampleAppPagesController < ExampleController
199+
include Matestack::Ui::Core::ApplicationHelper
200+
201+
def page1
202+
responder_for(Pages::ExampleApp::ExamplePage)
203+
end
204+
205+
end
206+
207+
visit "app_specs/my_example_app/page1"
208+
209+
expect(page).to have_content("has access to ActionView Context")
210+
expect(page).to have_content("Test Link")
211+
expect(page).to have_content("3 minutes")
212+
213+
end
214+
161215
it "can navigate back using browser history"
162216

163217
it "just uses serverside routes, which works standalone"

spec/usage/base/component_spec.rb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,102 @@ def response
933933

934934
end
935935

936+
describe "View Context Access" do
937+
938+
it "a component can access view context scope" do
939+
940+
module Matestack::Ui::Core::SomeStaticComponent
941+
class SomeStaticComponent < Matestack::Ui::StaticComponent
942+
943+
def response
944+
components {
945+
div id: "my-component" do
946+
if @view_context.view_renderer.instance_of?(ActionView::Renderer)
947+
plain "has access to ActionView Context"
948+
end
949+
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
950+
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
951+
end
952+
}
953+
end
954+
955+
end
956+
end
957+
958+
class Pages::ExamplePage < Matestack::Ui::Page
959+
960+
def response
961+
components {
962+
div id: "div-on-page" do
963+
someStaticComponent
964+
end
965+
}
966+
end
967+
968+
end
969+
970+
visit "/component_test"
971+
972+
expect(page).to have_content("has access to ActionView Context")
973+
expect(page).to have_content("Test Link")
974+
expect(page).to have_content("3 minutes")
975+
976+
end
977+
978+
it "a component can access view context scope when rerendered via async" do
979+
980+
module Matestack::Ui::Core::SomeStaticComponent
981+
class SomeStaticComponent < Matestack::Ui::StaticComponent
982+
983+
def response
984+
components {
985+
div id: "my-component" do
986+
div id: "timestamp" do
987+
plain "#{DateTime.now.strftime('%Q')}"
988+
end
989+
if @view_context.view_renderer.instance_of?(ActionView::Renderer)
990+
plain "has access to ActionView Context"
991+
end
992+
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
993+
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
994+
end
995+
}
996+
end
997+
998+
end
999+
end
1000+
1001+
class Pages::ExamplePage < Matestack::Ui::Page
1002+
1003+
def response
1004+
components {
1005+
div id: "div-on-page" do
1006+
async rerender_on: "some_event" do
1007+
someStaticComponent
1008+
end
1009+
end
1010+
}
1011+
end
1012+
1013+
end
1014+
1015+
visit "/component_test"
1016+
1017+
element = page.find("#timestamp")
1018+
before_content = element.text
1019+
1020+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("some_event")')
1021+
1022+
expect(page).not_to have_content(before_content)# check if async reload has really worked!
1023+
1024+
expect(page).to have_content("has access to ActionView Context")
1025+
expect(page).to have_content("Test Link")
1026+
expect(page).to have_content("3 minutes")
1027+
1028+
end
1029+
1030+
end
1031+
9361032
describe "Doesn't have Controller Action Scope Access"
9371033

9381034
describe "Default Tag Attributes"

spec/usage/base/page_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,47 @@ def my_action
7575

7676
end
7777

78+
it "can access ActionView Context" do
79+
80+
class ExamplePage < Matestack::Ui::Page
81+
82+
def response
83+
components {
84+
div do
85+
plain @bar
86+
if @view_context.view_renderer.instance_of?(ActionView::Renderer)
87+
plain "has access to ActionView Context"
88+
end
89+
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
90+
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
91+
end
92+
}
93+
end
94+
95+
end
96+
97+
98+
class PageTestController < ActionController::Base
99+
layout "application"
100+
101+
include Matestack::Ui::Core::ApplicationHelper
102+
103+
def my_action
104+
@bar = "bar"
105+
responder_for(ExamplePage)
106+
end
107+
108+
end
109+
110+
visit "/page_test"
111+
112+
expect(page).to have_content("bar")
113+
expect(page).to have_content("has access to ActionView Context")
114+
expect(page).to have_content("Test Link")
115+
expect(page).to have_content("3 minutes")
116+
117+
end
118+
78119
it "can resolve data in a prepare method, which runs before rendering"
79120

80121
it "can use classic ruby within component orchestration"

0 commit comments

Comments
 (0)