Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic html id to Matestack pages #551

Merged
merged 3 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/ui-in-pure-ruby/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ end
```
{% endcode %}

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.

For example, the above controller code will result in the following HTML markup:

```markup
<div id="matestack-page-some-controller-overview" class="matestack-page-root">
<!-- page content -->
</div>
```

Learn more about Pages:

{% page-ref page="pages/" %}
Expand Down
17 changes: 14 additions & 3 deletions lib/matestack/ui/core/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create_children

def page
if params[:only_page]
div class: 'matestack-page-root' do
div id: page_id, class: 'matestack-page-root' do
yield
end
else
Expand All @@ -28,12 +28,12 @@ def page
end
div class: 'matestack-page-wrapper', 'v-bind:class': '{ "loading": loading === true }' do
div 'v-if': 'asyncPageTemplate == null' do
div class: 'matestack-page-root' do
div id: page_id, class: 'matestack-page-root' do
yield
end
end
div 'v-if': 'asyncPageTemplate != null' do
div class: 'matestack-page-root' do
div id: page_id, class: 'matestack-page-root' do
Base.new('v-runtime-template', ':template': 'asyncPageTemplate')
end
end
Expand All @@ -57,6 +57,17 @@ def component_attributes
}
end

def page_id
controller = params[:controller]
.parameterize
.dasherize

action = params[:action]
.underscore
.dasherize

"matestack-page-#{controller}-#{action}"
end
end
end
end
Expand Down
33 changes: 31 additions & 2 deletions spec/test/base/core/page/rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,41 @@
before :all do
Rails.application.routes.append do
scope "page_rendering_components_spec" do
get '/page_test', to: 'page_test#my_action', as: 'rendering_page_test_action'
get '/page_rendering_test', to: 'page_rendering_test#my_action'
end
end
Rails.application.reload_routes!

class ExamplePageRendering < Matestack::Ui::Page
def response
div do
plain "ExamplePage"
end
end
end

class PageRenderingTestController < ActionController::Base
include Matestack::Ui::Core::Helper

def my_action
render ExamplePageRendering
end
end
end

it "wraps page content with a specific dom structure"
before :each do
visit "page_rendering_components_spec/page_rendering_test"
end

it "wraps content with a specific dom structure" do
expect(page).to have_xpath('//div[@id="matestack-ui"]/div[@class="matestack-page-container"]/div[@class="matestack-page-wrapper"]')
end

it "renders its root element with a dynamic id composed of {controller} and {action}" do
expect(page).to have_xpath('//div[@id="matestack-page-page-rendering-test-my-action"]')
end

it "renders its root element with a specific class" do
expect(page).to have_xpath('//div[@class="matestack-page-root"]')
end
end