Class: ActionDispatch::Routing::Mapper
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::Mapper
- Defined in:
- lib/activity_notification/rails/routes.rb
Overview
Extended ActionDispatch::Routing::Mapper implementation to add routing method of ActivityNotification.
Instance Method Summary collapse
-
#notify_to(*resources, *options) ⇒ ActionDispatch::Routing::Mapper
Includes notify_to method for routes, which is responsible to generate all necessary routes for activity_notification.
Instance Method Details
#notify_to(*resources, *options) ⇒ ActionDispatch::Routing::Mapper
Includes notify_to method for routes, which is responsible to generate all necessary routes for activity_notification.
When you have an User model configured as a target (e.g. defined acts_as_target), you can create as follows in your routes:
notify_to :users
This method creates the needed routes:
# Notification routes
user_notifications GET /users/:user_id/notifications(.:format)
{ controller:"activity_notification/notifications", action:"index", target_type:"users" }
user_notification GET /users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications", action:"show", target_type:"users" }
user_notification DELETE /users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications", action:"destroy", target_type:"users" }
open_all_user_notifications POST /users/:user_id/notifications/open_all(.:format)
{ controller:"activity_notification/notifications", action:"open_all", target_type:"users" }
move_user_notification POST /users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications", action:"move", target_type:"users" }
open_user_notification POST /users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications", action:"open", target_type:"users" }
When you use devise authentication and you want make notification targets assciated with devise, you can create as follows in your routes:
notify_to :users, with_devise: :users
This with_devise option creates the needed routes assciated with devise authentication:
# Notification with devise routes
user_notifications GET /users/:user_id/notifications(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"index", target_type:"users", devise_type:"users" }
user_notification GET /users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"show", target_type:"users", devise_type:"users" }
user_notification DELETE /users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"destroy", target_type:"users", devise_type:"users" }
open_all_user_notifications POST /users/:user_id/notifications/open_all(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"open_all", target_type:"users", devise_type:"users" }
move_user_notification POST /users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"move", target_type:"users", devise_type:"users" }
open_user_notification POST /users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"open", target_type:"users", devise_type:"users" }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/activity_notification/rails/routes.rb', line 60 def notify_to(*resources) = resources. #TODO check resources if it includes target module if (with_devise = .delete(:with_devise)).present? [:controller] ||= "activity_notification/notifications_with_devise" [:as] ||= "notifications" #TODO check devise configuration in model devise_defaults = { devise_type: with_devise.to_s } else [:controller] ||= "activity_notification/notifications" end [:except] ||= [] [:except].concat( [:new, :create, :edit, :update] ) notification_resources = [:model] || :notifications #TODO other options # :as, :path_prefix, :path_names ... resources.each do |resource| self.resources resource, only: :none do [:defaults] = (devise_defaults || {}).merge({ target_type: resource.to_s }) self.resources notification_resources, do collection do post :open_all unless ignore_path?(:open_all, ) end member do get :move unless ignore_path?(:move, ) post :open unless ignore_path?(:open, ) end end end end self end |