Skip to content

Commit 05302cd

Browse files
author
Nils Henning
committed
[TASK] add rails integration guide
1 parent 2a47f84 commit 05302cd

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,93 @@
11
# Rails Integration
22

3+
Matestacks gives you different options to integrate it into your existing rails project or migrate it step by step to a full matestack app.
4+
In the following sections we will present different methods to integrate or migrate your app, including integrating from the inside out with components first, reusing existing partials and resuse complete views as matestack pages.
5+
6+
All this solutions are going to show you different approaches from which you can choose the appropriate one for you.
7+
8+
In order to explain the following approaches we will shortly describe a scenario. Let's assume a very simple app, which has a landing page, products index page and a products show page. The landing page displays a nav, search bar and 3 product teasers. The products index page lists all components reusing the products teaser partial. The products show page just renders the details of a product.
9+
10+
1. [Custom components](#custom-components)
11+
2. [In progress...](#in-progress)
12+
13+
## Custom components
14+
15+
The easiest way to integrate matestack is by creating custom components and using our provided `matestack_component(component, options = {}, &block)` helper. Given the scenario from above we have a product teaser partial in `app/views/products/_teaser.html.erb` containing following content:
16+
17+
```html
18+
<%= link_to product_path(product), class: 'product-teaser' do %>
19+
<div>
20+
<h2><%= product.name %></h2>
21+
<p><%= product.description %></p>
22+
<b><%= product.price %></b>
23+
</div>
24+
<% end %>
25+
```
26+
27+
This is a perfect place to start refactoring our application to use matestack. It's easy to start from the inside out, first replacing parts of your UI with components. As partials already are used to structure your UI in smaller reusable parts they are a perfect starting point. So let's refactor our product teaser into a custom component.
28+
29+
After successfully following the [installation guide](/docs/guides/000-installation/README.md) we can start. Remember to set the id "matestack-ui" in your corresponding layout.
30+
31+
Start by creating a file called `teaser.rb` in `app/matestack/components/products/teaser.rb`. Placement of this file is as you see similar to our partial. In this file we implement our component in pure ruby as follows:
32+
33+
```ruby
34+
class Components::Products::Teaser < Matestack::Ui::Component
35+
36+
requires :product
37+
38+
def response
39+
link path: product_path(product) do
40+
div do
41+
heading size: 2, text: product.name
42+
paragraph text: product.description
43+
b text: product.price
44+
end
45+
end
46+
end
47+
48+
end
49+
```
50+
51+
We inherit from `Matestack::Ui::Component` to create our teaser component. As it should display product informations it requires a product. We can access this product through a getter method `product`. We have now a teaser component, but in order to use it we have to register it and include our registration in our `ApplicationController`.
52+
53+
Let's register our component by creating a component registry in `app/matestack/components/registry.rb`.
54+
55+
```ruby
56+
module Components::Registry
57+
58+
Matestack::Ui::Core::Component::Registry.register_component{
59+
product_teaser: Components::Products::Teaser
60+
}
61+
62+
end
63+
```
64+
65+
The last thing we have to do before we can use our component is to include our registry in the `ApplicationController`.
66+
67+
```ruby
68+
class ApplicationController < ActionController::Base
69+
include Matestack::Ui::Core::ApplicationHelper # see installation guide for details
70+
include Components::Registry
71+
end
72+
```
73+
74+
Now we can use our matestack component in our view. Replacing the `render partial:` call from before with a call to `matestack_component` on our landing page.
75+
76+
`app/views/static/index.html.erb`.
77+
78+
```html
79+
<!-- before -->
80+
<%= render partial: 'products/teaser', collection: @products, as: :product %>
81+
82+
<!-- using our component -->
83+
<%= @products.each do |product|>
84+
<%= matestack_component :product_teaser, product: product>
85+
<% end %>
86+
```
87+
88+
This approach gives you access inside this component to all matestack features except page transitions. You can use `async`, `toggle`, `collection`, `actions`, `forms`, `isolated`, `onclick` and more vue.js components inside your components, enabling you to build modern and interactive UIs in pure ruby, while keeping a low effort for integrating or slowly migrating your project to matestack.
89+
90+
91+
## In progress...
92+
393
![Coming Soon](../../images/coming_soon.png)

0 commit comments

Comments
 (0)