Skip to content

Commit ba433e8

Browse files
authored
Merge pull request #149 from basemate/update_integration_docs
Update integration docs
2 parents 2dcc3d8 + cebcd16 commit ba433e8

File tree

4 files changed

+105
-7
lines changed

4 files changed

+105
-7
lines changed

docs/integrations/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
1. [ActionCable](/docs/integrations/websockets.md)
66

7-
## Auth
7+
## Authentication and Authorization
88

9-
2. Devise (coming soon)
10-
3. Pundit (coming soon)
11-
4. CanCanCan (coming soon)
9+
2. [Devise](/docs/integrations/devise.md)
10+
3. [Pundit or CanCanCan](/docs/integrations/pundit_or_cancancan.md)

docs/integrations/devise.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Authentication via Devise
2+
3+
We recommend using a proven authentication solution like Devise for your Rails app. Find out more about Devise [here](https://github.com/plataformatec/devise/).
4+
5+
## Setting up Devise with your existing matestack application
6+
7+
Just follow the [Devise installation guide](https://github.com/plataformatec/devise/#getting-started) and add it to the models and controller actions you want.
8+
9+
## Adding matestack to a Rails app that already uses Devise
10+
11+
As Devise mainly lives in your models and controllers while matestack is meant to replace your view layer, there shouldn't be much to change for you. Just make sure to trigger the right `before_action`s when adding matestack's `responder_for` to a controller action!
12+
13+
14+
## Example
15+
16+
This is just your average Rails user controller. The `before_action` gets called on initial pageload and on every subsequent AJAX request the client sends.
17+
18+
```ruby
19+
class UserController < ApplicationController
20+
before_action :authenticate_user! # Devise authentication
21+
22+
def show
23+
responder_for(Pages::UserApp::ShowPage) # matestack page responder
24+
end
25+
26+
end
27+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Authorization via Pundit or CanCanCan
2+
3+
At [basemate](https://basemate.io), we have had good experiences using matestack with [Pundit](https://github.com/varvet/pundit). [CanCanCan](https://github.com/CanCanCommunity/cancancan), another very popular authorization library in Rails, is also supported, as shown below!
4+
5+
Both Pundit and CanCanCan use pure Ruby and focus on the model and controller layer, so they are compatible to matestack's UI library.
6+
7+
## Example 1: Pundit
8+
9+
Here we see how Pundit defines policies and we can check for them in the controller action, just before matestack's `responder_for`!
10+
11+
12+
A Pundit example in `app/policies/user_policy.rb`:
13+
```ruby
14+
class UserPolicy
15+
attr_reader :user
16+
17+
def initialize(user)
18+
@user = user
19+
end
20+
21+
def show?
22+
user.is_visible?
23+
end
24+
25+
end
26+
```
27+
28+
Matestack's `app/controllers/user_controller.rb`:
29+
30+
```ruby
31+
class UserController < ApplicationController
32+
33+
def show
34+
@user = User.find_by(id: params[:id])
35+
authorize @user # checking Pundit policy
36+
responder_for(Pages::UserApp::ShowPage) # matestack page responder
37+
end
38+
39+
end
40+
```
41+
42+
## Example 2: CanCanCan
43+
44+
Here we see how CanCanCan defines abilities and we can check for them in the controller action, just before matestack's `responder_for`!
45+
46+
47+
CanCanCan's `app/models/ability.rb` example, borrowed from their guides:
48+
```ruby
49+
class Ability
50+
include CanCan::Ability
51+
52+
def initialize(user)
53+
can :read, :all # permissions for every user, even if not logged in
54+
# [...]
55+
end
56+
57+
end
58+
```
59+
60+
Matestack's `app/controllers/user_controller.rb`:
61+
62+
```ruby
63+
class UserController < ApplicationController
64+
65+
def show
66+
@user = User.find_by(id: params[:id])
67+
authorize! :read, @user # checking for CanCanCan ability
68+
responder_for(Pages::UserApp::ShowPage) # matestack page responder
69+
end
70+
71+
end
72+
```

docs/integrations/websockets.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Websockets
22

3-
Websockets can easily be integrated into matestack. matestack uses Rails ActionCable
3+
Websockets can easily be integrated into matestack. Matestack uses Rails ActionCable
44
for this feature.
55

6-
## Creata a Channel on the serverside
6+
## Create a Channel on the serverside
77

88
`app/channels/matestack_ui_core_channel.rb`
99

@@ -17,7 +17,7 @@ class MatestackUiCoreChannel < ApplicationCable::Channel
1717
end
1818
```
1919

20-
## Add a Subscription on the clientside and link to matestack event hub
20+
## Add a Subscription on the client side and link to matestack event hub
2121

2222
`app/assets/javascripts/application.js`
2323

0 commit comments

Comments
 (0)