You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/integrations/action-cable.md
+17-36Lines changed: 17 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,20 @@ In this guide we will provide information on how to create channels, consumers a
6
6
7
7
## Setup
8
8
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`.
10
10
11
-
### Websockets
11
+
This will create a `app/javascript/channels/matestack_ui_core_channel.js` file where you can setup your subscriptions.
12
12
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.
14
14
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.
@@ -38,30 +41,6 @@ We expect the pushed data to include an _event_ key with the name of the event t
38
41
39
42
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.
40
43
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.
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
-
65
44
## Usage
66
45
67
46
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
155
134
```ruby
156
135
# app/channels/application_cable/connection.rb
157
136
classConnection < ActionCable::Connection::Base
158
-
identified_by :current_user
137
+
identified_by :current_user
159
138
160
-
defconnect
161
-
self.current_user = find_verified_user
162
-
end
139
+
defconnect
140
+
self.current_user = find_verified_user
141
+
end
163
142
164
-
protected
143
+
protected
165
144
166
-
deffind_verified_admin
167
-
env['warden'].user
168
-
end
145
+
deffind_verified_admin
146
+
env['warden'].user
169
147
end
148
+
170
149
end
171
150
```
172
151
@@ -187,6 +166,7 @@ Corresponding front end channel subscription.
187
166
188
167
```javascript
189
168
// app/javascript/channels/public_channel.js
169
+
importMatestackUiCorefrom"matestack-ui-core"
190
170
importconsumerfrom"./consumer"
191
171
192
172
consumer.subscriptions.create("PublicChannel", {
@@ -212,6 +192,7 @@ Corresponding front end channel subscription.
Copy file name to clipboardExpand all lines: docs/integrations/devise.md
+15-48Lines changed: 15 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,11 @@
2
2
3
3
Devise is one of the most popular gems for authentication. Find out more about Devise [here](https://github.com/plataformatec/devise/).
4
4
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.
33
6
34
7
## Devise helpers
35
8
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.
37
10
38
11
For example:
39
12
@@ -63,7 +36,7 @@ end
63
36
64
37
## Devise sign in
65
38
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.
67
40
68
41
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.
69
42
@@ -73,13 +46,11 @@ Therefore we need to adjust a few things and create some pages. First we create
@@ -94,7 +65,7 @@ class Profile::Pages::Sessions::SignIn < Matestack::Ui::Page
94
65
method::post,
95
66
path: user_session_path,
96
67
success: {
97
-
transition: {
68
+
redirect: {# or transition, if your app layout does not change
98
69
follow_response:true
99
70
}
100
71
},
@@ -109,8 +80,6 @@ end
109
80
110
81
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.
111
82
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
-
114
83
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.
115
84
116
85
`app/config/routes.rb`
@@ -125,14 +94,12 @@ Rails.application.routes.draw do
125
94
end
126
95
```
127
96
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.
# include your component registry in order to use custom components
135
-
includeComponents::Registry
136
103
137
104
matestack_app Profile::App# specify the corresponding app to wrap pages in
138
105
@@ -174,17 +141,17 @@ config.warden do |manager|
174
141
end
175
142
```
176
143
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.
178
145
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.
180
147
181
148
## Devise sign out
182
149
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:
184
151
185
152
```ruby
186
153
action sign_out_config do
187
-
button text:'Sign out'
154
+
button 'Sign out'
188
155
end
189
156
```
190
157
@@ -194,19 +161,19 @@ def sign_out_config
194
161
method::get,
195
162
path: destroy_admin_session_path,
196
163
success: {
197
-
transition: {
164
+
redirect: {
198
165
follow_response:true
199
166
}
200
167
}
201
168
}
202
169
end
203
170
```
204
171
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.
206
173
207
174
```ruby
208
175
# The default HTTP method used to sign out a resource. Default is :delete.
0 commit comments