Skip to content

Commit fd2dd88

Browse files
jonasjabarigitbook-bot
authored andcommitted
GitBook: [2.0.0] 63 pages modified
1 parent 18858a2 commit fd2dd88

File tree

2 files changed

+32
-84
lines changed

2 files changed

+32
-84
lines changed

docs/integrations/action-cable.md

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ In this guide we will provide information on how to create channels, consumers a
66

77
## Setup
88

9-
The setup differs slightly depending on your usage of websockets or the asset pipeline.
9+
Create a channel using the rails generator. Run the command `rails generate channel MatestackUiCoreChannel`.
1010

11-
### Websockets
11+
This will create a `app/javascript/channels/matestack_ui_core_channel.js` file where you can setup your subscriptions.
1212

13-
Create a channel using the rails generator. Run the command `rails generate channel MatestackUiCoreChannel`. This will create a `app/javascript/channels/matestack_ui_core_channel.js` file where you can setup your subscriptions. It also generates the corresponding server side `MatestackUiCoreChannel < ApplicationCable::Channel` class.
13+
It also generates the corresponding server side `MatestackUiCoreChannel < ApplicationCable::Channel` class.
1414

15-
The `matestack_ui_core_channel.js` is responsible to create a subscription to the "MatestackUiCoreChannel". All we need to do is to tell this channel that it should trigger an event using the `MatestackUiCore.matestackEventHub` with the received data.
15+
The `matestack_ui_core_channel.js` is responsible to create a subscription to the "MatestackUiCoreChannel".
16+
17+
All we need to do is to tell this channel that it should trigger an event using the `MatestackUiCore.matestackEventHub` with the received data.
1618

1719
`app/javascript/channels/matestack_ui_core_channel.js`
1820

1921
```javascript
22+
import MatestackUiCore from "matestack-ui-core"
2023
import consumer from "./consumer"
2124

2225
consumer.subscriptions.create("MatestackUiCoreChannel", {
@@ -38,30 +41,6 @@ We expect the pushed data to include an _event_ key with the name of the event t
3841

3942
If you do not want to use the rails generator just create the `matestack_ui_core_channel.js` yourself in `app/javascript/channels/` and paste the above code in it.
4043

41-
### Asset pipeline
42-
43-
Like with websockets you can use the rails generator to create a matestack ui core channel by running `rails generate channel MatestackUiCoreChannel`. This will create a `app/assets/javascript/channels/matestack_ui_core_channel.js` file where you can setup your subscriptions. It also generates the corresponding server side `MatestackUiCoreChannel < ApplicationCable::Channel` class.
44-
45-
```javascript
46-
App.matestack_ui_core = App.cable.subscriptions.create("MatestackUiCoreChannel", {
47-
connected() {
48-
// Called when the subscription is ready for use on the server
49-
},
50-
51-
disconnected() {
52-
// Called when the subscription has been terminated by the server
53-
},
54-
55-
received(data) {
56-
MatestackUiCore.matestackEventHub.$emit(data.event, data)
57-
}
58-
});
59-
```
60-
61-
We expect the pushed data to include an _event_ key with the name of the event that should be triggered. We also pass the _data_ as event payload to the event emit, giving you the possibility to work with server side send data.
62-
63-
If you do not want to use the rails generator just create the `matestack_ui_core_channel.js` yourself in `app/assets/javascript/channels/` and paste the above code in it.
64-
6544
## Usage
6645

6746
After setting up the client side JavaScript for our action cable we now take a look at how to create server side events to trigger for example rerenderings of `async`/isolated components or show/hide content with the `toggle` component. We will introduce two different types of creating server side events. First broadcasting events to all subscribed clients and secondly sending events to a user by authenticating a connection through a devise user.
@@ -155,18 +134,18 @@ With the above implemented connection authorization in place we will not be able
155134
```ruby
156135
# app/channels/application_cable/connection.rb
157136
class Connection < ActionCable::Connection::Base
158-
identified_by :current_user
137+
identified_by :current_user
159138

160-
def connect
161-
self.current_user = find_verified_user
162-
end
139+
def connect
140+
self.current_user = find_verified_user
141+
end
163142

164-
protected
143+
protected
165144

166-
def find_verified_admin
167-
env['warden'].user
168-
end
145+
def find_verified_admin
146+
env['warden'].user
169147
end
148+
170149
end
171150
```
172151

@@ -187,6 +166,7 @@ Corresponding front end channel subscription.
187166

188167
```javascript
189168
// app/javascript/channels/public_channel.js
169+
import MatestackUiCore from "matestack-ui-core"
190170
import consumer from "./consumer"
191171

192172
consumer.subscriptions.create("PublicChannel", {
@@ -212,6 +192,7 @@ Corresponding front end channel subscription.
212192

213193
```javascript
214194
// app/javascript/channels/private_channel.js
195+
import MatestackUiCore from "matestack-ui-core"
215196
import consumer from "./consumer"
216197

217198
consumer.subscriptions.create("PrivateChannel", {

docs/integrations/devise.md

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,11 @@
22

33
Devise is one of the most popular gems for authentication. Find out more about Devise [here](https://github.com/plataformatec/devise/).
44

5-
In order to integrate it fully in matestack apps and pages we need to adjust a few things. This guide explains what exactly needs to be adjusted.
6-
7-
## Setting up Devise
8-
9-
We install devise by following devise [installation guide](https://github.com/plataformatec/devise/#getting-started).
10-
11-
First we add `gem 'devise'` to our `Gemfile` and run `bundle install`. Afterwards we need to run devise installation task with `rails generate devise:install`.
12-
13-
Then we need to add an action mailer config like devise tolds us at the end of the installation task. We therefore add the following line to our `config/environments/development.rb`.
14-
15-
```ruby
16-
config.action_mailer.default_url_options = {
17-
host: 'localhost',
18-
port: 3000
19-
}
20-
```
21-
22-
## Devise models
23-
24-
Generating a devise model or updating an existing one works as usual. Just use devise generator to create a model. If you already have a model which you want to extend for use with devise take a look at the devise documentation on how to do so.
25-
26-
Let's create a user model for example by running
27-
28-
```bash
29-
rails generate devise user
30-
```
31-
32-
And ensure `devise_for :user` is added to the `app/config/routes.rb`
5+
In order to integrate it fully in Matestack apps and pages we need to adjust a few things. This guide explains what exactly needs to be adjusted.
336

347
## Devise helpers
358

36-
Again nothing unusual here. We can access devise helper methods inside our controllers, apps, pages and components like we would normally do. In case of our user model this means we could access `current_user` or `user_signed_in?` in apps, pages and components.
9+
We can access devise helper methods inside our controllers, apps, pages and components like we would normally do. In case of our user model this means we could access `current_user` or `user_signed_in?` in apps, pages and components.
3710

3811
For example:
3912

@@ -63,7 +36,7 @@ end
6336

6437
## Devise sign in
6538

66-
Using the default devise sign in views should work without a problem, but they will not be integrated with a matestack app. Let's assume we have a profile matestack app called `Profile::App`. If we want to take advantage of matestacks transitions features \(not reloading our app layout between page transitions\) we can not use devise views, because we would need to redirect to them and therefore need to reload the whole page. Requiring us for example to implement our navigation twice. In our `Profile::App` and also in our devise sign in view.
39+
Using the default devise sign in views should work without a problem, but they will not be integrated with a Matestack app. Let's assume we have a profile Matestack app called `Profile::App`. If we want to take advantage of Matestack's transitions features \(not reloading our app layout between page transitions\) we can not use devise views, because we would need to redirect to them and therefore need to reload the whole page. Requiring us for example to implement our navigation twice. In our `Profile::App` and also in our devise sign in view.
6740

6841
Therefore we need to adjust a few things and create some pages. First we create a custom sign in page containing a form with email and password inputs.
6942

@@ -73,13 +46,11 @@ Therefore we need to adjust a few things and create some pages. First we create
7346
class Profile::Pages::Sessions::SignIn < Matestack::Ui::Page
7447

7548
def response
76-
heading text: 'Sign in'
77-
form form_config do
49+
h1 'Sign in'
50+
matestack_form form_config do
7851
form_input label: 'Email', key: :email, type: :email
7952
form_input label: 'Password', key: :password, type: :password
80-
form_submit do
81-
button text: 'Sign in'
82-
end
53+
button text: 'Sign in', type: :submit
8354
end
8455
toggle show_on: 'sign_in_failure' do
8556
plain 'Your email or password is not valid.'
@@ -94,7 +65,7 @@ class Profile::Pages::Sessions::SignIn < Matestack::Ui::Page
9465
method: :post,
9566
path: user_session_path,
9667
success: {
97-
transition: {
68+
redirect: { # or transition, if your app layout does not change
9869
follow_response: true
9970
}
10071
},
@@ -109,8 +80,6 @@ end
10980

11081
This page displays a form with an email and password input. The default required parameters for a devise sign in. It also contains a `toggle` component which gets shown when the event `sign_in_failure` is emitted. This event gets emitted in case our form submit was unsuccessful as we specified it in our `form_config` hash. If the form is successful our app will make a transition to the page the server would redirect to.
11182

112-
Remember to use `redirect` instead of `transition`, if you have conditional content depending on a logged in user inside your app. You have to use `redirect` because the app needs to be reloaded, which happens only with `redirect`.
113-
11483
In order to render our sign in page when someone tries to access a route which needs authentication or visits the sign in page we must override devise session controller in order to render our page. We do this by configuring our routes to use a custom controller.
11584

11685
`app/config/routes.rb`
@@ -125,14 +94,12 @@ Rails.application.routes.draw do
12594
end
12695
```
12796

128-
Override the `new` action in order to render our sign in page and set the correct matestack app in the controller. Also remember to include the components registry. This is necessary if you use custom components in your app or page, because without it matestack can't resolve them.
97+
Override the `new` action in order to render our sign in page and set the correct Matestack app in the controller. Also remember to include the components registry. This is necessary if you use custom components in your app or page, because without it Matestack can't resolve them.
12998

13099
`app/controllers/users/sessions_controller.rb`
131100

132101
```ruby
133102
class Users::SessionsController < Devise::SessionsController
134-
# include your component registry in order to use custom components
135-
include Components::Registry
136103

137104
matestack_app Profile::App # specify the corresponding app to wrap pages in
138105

@@ -174,17 +141,17 @@ config.warden do |manager|
174141
end
175142
```
176143

177-
That's it. When we now try to sign in with incorrect credentials the `form` component triggers the `sign_in_failure` event, which sets off our `toggle` component resulting in displaying the error message.
144+
That's it. When we now try to sign in with incorrect credentials the `matestack_form` component triggers the `sign_in_failure` event, which sets off our `toggle` component resulting in displaying the error message.
178145

179-
**Wrap Up** That's it. Now you have a working sign in with devise fully integrated into matestack. All we needed to do was creating a sign in page, updating our routes to use a custom session controller, overriding the new action, creating a custom failure app and updating the devise config.
146+
**Wrap Up** That's it. Now you have a working sign in with devise fully integrated into Matestack. All we needed to do was creating a sign in page, updating our routes to use a custom session controller, overriding the new action, creating a custom failure app and updating the devise config.
180147

181148
## Devise sign out
182149

183-
Creating a sign out button in matestack is very straight forward. We use matestacks [`action` component](../built-in-reactivity/call-server-side-actions/action-component-api.md) to create a sign out button. See the example below:
150+
Creating a sign out button in Matestack is very straight forward. We use matestacks [`action` component](../built-in-reactivity/call-server-side-actions/action-component-api.md) to create a sign out button. See the example below:
184151

185152
```ruby
186153
action sign_out_config do
187-
button text: 'Sign out'
154+
button 'Sign out'
188155
end
189156
```
190157

@@ -194,19 +161,19 @@ def sign_out_config
194161
method: :get,
195162
path: destroy_admin_session_path,
196163
success: {
197-
transition: {
164+
redirect: {
198165
follow_response: true
199166
}
200167
}
201168
}
202169
end
203170
```
204171

205-
Notice the `method: :get` in the configuration hash. We use a http GET request to sign out, because the browser will follow the redirect send from devise session controller and then matestack tries to load the page where we have been redirected to. When we would use a DELETE request the action we would be redirected to from the browser will be also requested with a http DELETE request, which will result in a rails routing error. Therefore we use GET and need to configure devise accordingly by changing the `sign_out_via` configuration parameter.
172+
Notice the `method: :get` in the configuration hash. We use a http GET request to sign out, because the browser will follow the redirect send from devise session controller and then Matestack tries to load the page where we have been redirected to. When we would use a DELETE request the action we would be redirected to from the browser will be also requested with a http DELETE request, which will result in a rails routing error. Therefore we use GET and need to configure devise accordingly by changing the `sign_out_via` configuration parameter.
206173

207174
```ruby
208175
# The default HTTP method used to sign out a resource. Default is :delete.
209-
config.sign_out_via = :get
176+
config.sign_out_via = :get
210177
```
211178

212179
That's all we have to do.

0 commit comments

Comments
 (0)