Skip to content

Document link param option, fixes #73 #243

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

Merged
merged 11 commits into from
Nov 6, 2019
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
3 changes: 2 additions & 1 deletion app/concepts/matestack/ui/core/link/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def setup
"id": component_id,
"method": options[:method],
"target": options[:target] ||= nil,
"href": link_path
"href": link_path,
"title": options[:title]
})
end

Expand Down
41 changes: 39 additions & 2 deletions docs/components/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ The HTTP request method the link should implement.
#### # target (optional, default is nil)
Specify where to open the linked document.

#### # title (optional)
Specify a title for the link (shown on mouseover).

## Example 1
This example renders a simple link within a `<div`-tag

Expand All @@ -50,7 +53,7 @@ This example renders a link without a specific link-text, so it wraps the rest o

```ruby
div id: "foo", class: "bar" do
link path: "https://matestack.org" do
link path: "https://matestack.org", title: "The matestack website" do
plain "Here"
end
end
Expand All @@ -59,7 +62,7 @@ end
returns

```html
<div id="foo" class="bar">
<div id="foo" class="bar" title="The matestack website">
<a href="https://matestack.org">Here</a>
</div>
```
Expand Down Expand Up @@ -107,3 +110,37 @@ returns
</a>
</div>
```

## Example 5 - path from symbol
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!

```ruby
div id: "foo", class: "bar" do
link path: :inline_edit_path, text: 'Click to edit'
end
```

returns

```html
<div id="foo" class="bar">
<a href="/inline_edit">Click to edit</a>
</div>
```

## Example 6 - path from symbol with params
You can also dynamically create `paths` from symbols and params, as displayed below:

```ruby
div id: "foo", class: "bar" do
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
end
```

returns

```html
<div id="foo" class="bar">
<a href="/api/single_endpoint/1">Call API endpoint 1</a>
</div>
```
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

scope :api do
get 'data', to: 'api#data'
get 'data/:number', to: 'api#single_endpoint', as: "single_endpoint"
end

end
58 changes: 56 additions & 2 deletions spec/usage/components/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ExamplePage < Matestack::Ui::Page
def response
components {
div id: "foo", class: "bar" do
link path: "https://matestack.org" do
link path: "https://matestack.org", title: "The matestack website" do
plain 'here'
end
end
Expand All @@ -53,7 +53,7 @@ def response

expected_static_output = <<~HTML
<div id="foo" class="bar">
<a href="https://matestack.org">here</a>
<a href="https://matestack.org" title="The matestack website">here</a>
</div>
HTML

Expand Down Expand Up @@ -125,4 +125,58 @@ def response
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
end

it 'Example 5 - Rails Routing using symbols' do

class ExamplePage < Matestack::Ui::Page

def response
components {
div id: "foo", class: "bar" do
link text: 'Click', path: :inline_edit_path
end
}
end

end

visit "/example"

static_output = page.html

expected_static_output = <<~HTML
<div id="foo" class="bar">
<a href="/my_app/inline_edit">Click</a>
</div>
HTML

expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
end

it 'Example 6 - Rails Routing using symbols with params' do

class ExamplePage < Matestack::Ui::Page

def response
components {
div id: "foo", class: "bar" do
link path: :single_endpoint_path, params: {number: 1}, text: 'Call API endpoint 1'
end

}
end

end
visit "/example"

static_output = page.html

expected_static_output = <<~HTML
<div id="foo" class="bar">
<a href="/api/data/1">Call API endpoint 1</a>
</div>
HTML

expect(stripped(static_output)).to ( include(stripped(expected_static_output)) )
end

end