Skip to content

Commit 84eed8b

Browse files
authored
Merge pull request #551 from marcosvafilho/dynamic_page_id
Add dynamic html id to Matestack pages
2 parents fece878 + 75bbcc1 commit 84eed8b

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

docs/ui-in-pure-ruby/overview.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ end
144144
```
145145
{% endcode %}
146146

147+
Both the controller and action names will be dynamically added to the `id` attribute of the page's root element, allowing CSS rules to directly target specific pages, and tests to easily locate the page's content.
148+
149+
For example, the above controller code will result in the following HTML markup:
150+
151+
```markup
152+
<div id="matestack-page-some-controller-overview" class="matestack-page-root">
153+
<!-- page content -->
154+
</div>
155+
```
156+
147157
Learn more about Pages:
148158

149159
{% page-ref page="pages/" %}

lib/matestack/ui/core/page.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def create_children
1515

1616
def page
1717
if params[:only_page]
18-
div class: 'matestack-page-root' do
18+
div id: page_id, class: 'matestack-page-root' do
1919
yield
2020
end
2121
else
@@ -28,12 +28,12 @@ def page
2828
end
2929
div class: 'matestack-page-wrapper', 'v-bind:class': '{ "loading": loading === true }' do
3030
div 'v-if': 'asyncPageTemplate == null' do
31-
div class: 'matestack-page-root' do
31+
div id: page_id, class: 'matestack-page-root' do
3232
yield
3333
end
3434
end
3535
div 'v-if': 'asyncPageTemplate != null' do
36-
div class: 'matestack-page-root' do
36+
div id: page_id, class: 'matestack-page-root' do
3737
Base.new('v-runtime-template', ':template': 'asyncPageTemplate')
3838
end
3939
end
@@ -57,6 +57,17 @@ def component_attributes
5757
}
5858
end
5959

60+
def page_id
61+
controller = params[:controller]
62+
.parameterize
63+
.dasherize
64+
65+
action = params[:action]
66+
.underscore
67+
.dasherize
68+
69+
"matestack-page-#{controller}-#{action}"
70+
end
6071
end
6172
end
6273
end

spec/test/base/core/page/rendering_spec.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,41 @@
66
before :all do
77
Rails.application.routes.append do
88
scope "page_rendering_components_spec" do
9-
get '/page_test', to: 'page_test#my_action', as: 'rendering_page_test_action'
9+
get '/page_rendering_test', to: 'page_rendering_test#my_action'
1010
end
1111
end
1212
Rails.application.reload_routes!
13+
14+
class ExamplePageRendering < Matestack::Ui::Page
15+
def response
16+
div do
17+
plain "ExamplePage"
18+
end
19+
end
20+
end
21+
22+
class PageRenderingTestController < ActionController::Base
23+
include Matestack::Ui::Core::Helper
24+
25+
def my_action
26+
render ExamplePageRendering
27+
end
28+
end
1329
end
1430

15-
it "wraps page content with a specific dom structure"
31+
before :each do
32+
visit "page_rendering_components_spec/page_rendering_test"
33+
end
34+
35+
it "wraps content with a specific dom structure" do
36+
expect(page).to have_xpath('//div[@id="matestack-ui"]/div[@class="matestack-page-container"]/div[@class="matestack-page-wrapper"]')
37+
end
1638

39+
it "renders its root element with a dynamic id composed of {controller} and {action}" do
40+
expect(page).to have_xpath('//div[@id="matestack-page-page-rendering-test-my-action"]')
41+
end
42+
43+
it "renders its root element with a specific class" do
44+
expect(page).to have_xpath('//div[@class="matestack-page-root"]')
45+
end
1746
end

0 commit comments

Comments
 (0)