Skip to content

Commit ca806d9

Browse files
committed
try fix authentication for notifications api
1 parent 9ce5b9d commit ca806d9

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,7 @@ $ rake db:migrate
4242

4343
Running the generator will do a few things:
4444

45-
1. add the engine routes to your routes.rb:
46-
47-
```ruby
48-
# config/routes.rb
49-
mount Wupee::Engine, at: "/wupee"
50-
```
51-
2. create <a name="initializer">wupee initializer</a>:
45+
1. create <a name="initializer">wupee initializer</a>:
5246

5347
```ruby
5448
# config/initializers/wupee.rb
@@ -67,7 +61,7 @@ Running the generator will do a few things:
6761
# # logic goes here, returning a boolean
6862
# end
6963
```
70-
3. create a mailer `NotificationsMailer` which inheritates from `Wupee::NotificationsMailer`
64+
2. create a mailer `NotificationsMailer` which inheritates from `Wupee::NotificationsMailer`
7165

7266
```ruby
7367
# app/mailers/notifications_mailer.rb
@@ -77,7 +71,7 @@ Running the generator will do a few things:
7771
end
7872
```
7973

80-
4. adds wupee to your locale yml file (for email subjects)
74+
3. adds wupee to your locale yml file (for email subjects)
8175
```yml
8276
# config/locales/en.yml
8377
en:
@@ -168,14 +162,30 @@ You can also use the method `notify` this way:
168162
## Wupee::Api::NotificationsController
169163

170164
The controller have various actions all scoped for the current user:
171-
* `wupee/api/notifications#index` : fetch notifications, take an optional parameter `is_read` (false by default)
165+
* `wupee/api/notifications#index` : fetch notifications, takes an optional parameter `scopes` (example: scopes=read,ordered)
172166
* `wupee/api/notifications#show` : fetch a notification
173-
* `wupee/api/notifications#mark_as_read` : mark as read a notification
174-
* `wupee/api/notifications#mark_all_as_read` : mark as read all notifications
167+
* `wupee/api/notifications#mark_as_read` : mark a notification as read
168+
* `wupee/api/notifications#mark_all_as_read` : mark all notifications as read
175169

176170
To use this controller, define a controller inheriting from `Wupee::Api::NotificationsController`, set the routes in your `config/routes.rb`
177171
and define a method `current_user` which returns the user signed in.
178172

173+
Example:
174+
```ruby
175+
# config/routes.rb
176+
namespace :api, defaults: { format: :json } do
177+
resources :notifications, only: [:index, :show] do
178+
patch :mark_as_read, on: :member
179+
patch :mark_all_as_read, on: :collection
180+
end
181+
end
182+
183+
# app/controllers/api/notifications_controller.rb
184+
class Api::NotificationsController < Wupee::Api::NotificationsController
185+
before_action :authenticate_user! # if you are using devise
186+
end
187+
```
188+
179189
## Why WUPEE ?
180190

181191
**W**hat's **UP** Sl**EE**de

config/routes.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
Wupee::Engine.routes.draw do
2-
namespace :api, defaults: { format: :json } do
3-
resources :notifications, only: [:index, :show] do
4-
patch :mark_as_read, on: :member
5-
patch :mark_all_as_read, on: :collection
6-
end
7-
end
82
end

spec/controllers/notifications_controller_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
require 'rails_helper'
22

3-
RSpec.describe Wupee::Api::NotificationsController, type: :controller do
4-
routes { Wupee::Engine.routes }
3+
RSpec.describe NotificationsController, type: :controller do
54
let(:notification) { create :notification }
65

7-
before :all do
8-
Wupee::Api::NotificationsController.class_eval do
9-
def current_user
10-
end
11-
end
12-
end
13-
146
before :each do
15-
allow_any_instance_of(Wupee::Api::NotificationsController).to receive(:current_user).and_return(notification.receiver)
7+
allow_any_instance_of(NotificationsController).to receive(:current_user).and_return(notification.receiver)
168
end
179

1810
it 'should get current user' do
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class NotificationsController < Wupee::Api::NotificationsController
2+
def current_user
3+
end
4+
end

spec/dummy/config/routes.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Rails.application.routes.draw do
2-
mount Wupee::Engine, at: "/wupee"
2+
resources :notifications, only: [:index, :show], defaults: { format: :json } do
3+
patch :mark_as_read, on: :member
4+
patch :mark_all_as_read, on: :collection
5+
end
36
end

0 commit comments

Comments
 (0)