Skip to content

Commit a87ee93

Browse files
document and test usage of params in link component
1 parent 88effec commit a87ee93

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

docs/components/link.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,37 @@ returns
107107
</a>
108108
</div>
109109
```
110+
111+
## Example 5 - path from symbol
112+
This example renders a link with a get request to any within your Rails application. In case you want to switch between pages within one specific matestack app, using the `transition` component probably is a better idea though!
113+
114+
```ruby
115+
div id: "foo", class: "bar" do
116+
link path: :inline_edit_path, text: 'Click to edit'
117+
end
118+
```
119+
120+
returns
121+
122+
```html
123+
<div id="foo" class="bar">
124+
<a href="/inline_edit">Click to edit</a>
125+
</div>
126+
```
127+
128+
## Example 6 - path from symbol with params
129+
You can also dynamically create `paths` from symbols and params, as displayed below:
130+
131+
```ruby
132+
div id: "foo", class: "bar" do
133+
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
134+
end
135+
```
136+
137+
returns
138+
139+
```html
140+
<div id="foo" class="bar">
141+
<a href="/api/single_endpoint/1">Call API endpoint 1</a>
142+
</div>
143+
```

spec/dummy/app/controllers/api_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ def data
44
render json: ["some", "server", "data"]
55
end
66

7+
def single_endpoint
8+
user = "user number #{params[:number]}"
9+
render json: [user]
10+
end
11+
712
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
scope :api do
4040
get 'data', to: 'api#data'
41+
get 'data/:number', to: 'api#single_endpoint', as: "single_endpoint"
4142
end
4243

4344
end

spec/dummy/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,14 @@ locate-path@^2.0.0:
35143514
p-locate "^2.0.0"
35153515
path-exists "^3.0.0"
35163516

3517+
locate-path@^3.0.0:
3518+
version "3.0.0"
3519+
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
3520+
integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==
3521+
dependencies:
3522+
p-locate "^3.0.0"
3523+
path-exists "^3.0.0"
3524+
35173525
lodash._reinterpolate@^3.0.0:
35183526
version "3.0.0"
35193527
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"

spec/usage/components/link_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,58 @@ def response
125125
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
126126
end
127127

128+
it 'Example 5 - Rails Routing using symbols' do
129+
130+
class ExamplePage < Matestack::Ui::Page
131+
132+
def response
133+
components {
134+
div id: "foo", class: "bar" do
135+
link text: 'Click', path: :inline_edit_path
136+
end
137+
}
138+
end
139+
140+
end
141+
142+
visit "/example"
143+
144+
static_output = page.html
145+
146+
expected_static_output = <<~HTML
147+
<div id="foo" class="bar">
148+
<a href="/my_app/inline_edit">Click</a>
149+
</div>
150+
HTML
151+
152+
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
153+
end
154+
155+
it 'Example 6 - Rails Routing using symbols with params' do
156+
157+
class ExamplePage < Matestack::Ui::Page
158+
159+
def response
160+
components {
161+
div id: "foo", class: "bar" do
162+
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
163+
end
164+
165+
}
166+
end
167+
168+
end
169+
visit "/example"
170+
171+
static_output = page.html
172+
173+
expected_static_output = <<~HTML
174+
<div id="foo" class="bar">
175+
<a href="/api/data/1">Call API endpoint 1</a>
176+
</div>
177+
HTML
178+
179+
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
180+
end
181+
128182
end

0 commit comments

Comments
 (0)