Class: ActionDispatch::Routing::Mapper
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::Mapper
- Includes:
- ActivityNotification::PolymorphicHelpers
- 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 notifications of activity_notification.
-
#subscribed_by(*resources, *options) ⇒ ActionDispatch::Routing::Mapper
Includes subscribed_by method for routes, which is responsible to generate all necessary routes for subscriptions of 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 notifications of 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 GET /users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications", action:"move", target_type:"users" }
open_user_notification PUT /users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications", action:"open", target_type:"users" }
You can also configure notification routes with scope like this:
scope :myscope, as: :myscope do
notify_to :users, routing_scope: :myscope
end
This routing_scope option creates the needed routes with specified scope like this:
# Notification routes
myscope_user_notifications GET /myscope/users/:user_id/notifications(.:format)
{ controller:"activity_notification/notifications", action:"index", target_type:"users", routing_scope: :myscope }
myscope_user_notification GET /myscope/users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications", action:"show", target_type:"users", routing_scope: :myscope }
myscope_user_notification DELETE /myscope/users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications", action:"destroy", target_type:"users", routing_scope: :myscope }
open_all_myscope_user_notifications POST /myscope/users/:user_id/notifications/open_all(.:format)
{ controller:"activity_notification/notifications", action:"open_all", target_type:"users", routing_scope: :myscope }
move_myscope_user_notification GET /myscope/users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications", action:"move", target_type:"users", routing_scope: :myscope }
open_myscope_user_notification PUT /myscope/users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications", action:"open", target_type:"users", routing_scope: :myscope }
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 GET /users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"move", target_type:"users", devise_type:"users" }
open_user_notification PUT /users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"open", target_type:"users", devise_type:"users" }
When you use with_devise option and you want to make simple default routes as follows, you can use devise_default_routes option:
notify_to :users, with_devise: :users, devise_default_routes: true
These with_devise and devise_default_routes options create the needed routes assciated with authenticated devise resource as the default target
# Notification with default devise routes
user_notifications GET /notifications(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"index", target_type:"users", devise_type:"users" }
user_notification GET /notifications/:id(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"show", target_type:"users", devise_type:"users" }
user_notification DELETE /notifications/:id(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"destroy", target_type:"users", devise_type:"users" }
open_all_user_notifications POST /notifications/open_all(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"open_all", target_type:"users", devise_type:"users" }
move_user_notification GET /notifications/:id/move(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"move", target_type:"users", devise_type:"users" }
open_user_notification PUT /notifications/:id/open(.:format)
{ controller:"activity_notification/notifications_with_devise", action:"open", target_type:"users", devise_type:"users" }
When you use activity_notification controllers as REST API mode, you can create as follows in your routes:
scope :api do
scope :"v2" do
notify_to :users, api_mode: true
end
end
This api_mode option creates the needed routes as REST API:
# Notification as API mode routes
GET /api/v2/users/:user_id/notifications(.:format)
{ controller:"activity_notification/notifications_api", action:"index", target_type:"users" }
GET /api/v2/users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications_api", action:"show", target_type:"users" }
DELETE /api/v2/users/:user_id/notifications/:id(.:format)
{ controller:"activity_notification/notifications_api", action:"destroy", target_type:"users" }
POST /api/v2/users/:user_id/notifications/open_all(.:format)
{ controller:"activity_notification/notifications_api", action:"open_all", target_type:"users" }
GET /api/v2/users/:user_id/notifications/:id/move(.:format)
{ controller:"activity_notification/notifications_api", action:"move", target_type:"users" }
PUT /api/v2/users/:user_id/notifications/:id/open(.:format)
{ controller:"activity_notification/notifications_api", action:"open", target_type:"users" }
When you would like to define subscription management paths with notification paths, you can create as follows in your routes:
notify_to :users, with_subscription: true
or you can also set options for subscription path:
notify_to :users, with_subscription: { except: [:index] }
If you configure this :with_subscription option with :with_devise option, with_subscription paths are also automatically configured with devise authentication as the same as notifications
notify_to :users, with_devise: :users, with_subscription: true
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/activity_notification/rails/routes.rb', line 147 def notify_to(*resources) = (:notifications, resources., [:new, :create, :edit, :update]) resources.each do |target| [:defaults] = { target_type: target.to_s }.merge([:devise_defaults]) = .select { |key, _| [:api_mode, :with_devise, :devise_default_routes, :with_subscription, :subscription_option, :model, :devise_defaults].exclude? key } if [:with_devise].present? && [:devise_default_routes].present? create_notification_routes , else self.resources target, only: :none do create_notification_routes , end end if [:with_subscription].present? && target.to_s.to_model_class.subscription_enabled? subscribed_by target, [:subscription_option] end end self end |
#subscribed_by(*resources, *options) ⇒ ActionDispatch::Routing::Mapper
Includes subscribed_by method for routes, which is responsible to generate all necessary routes for subscriptions of 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:
subscribed_by :users
This method creates the needed routes:
# Subscription routes
user_subscriptions GET /users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions", action:"index", target_type:"users" }
find_user_subscriptions GET /users/:user_id/subscriptions/find(.:format)
{ controller:"activity_notification/subscriptions", action:"find", target_type:"users" }
user_subscription GET /users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions", action:"show", target_type:"users" }
PUT /users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions", action:"create", target_type:"users" }
DELETE /users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions", action:"destroy", target_type:"users" }
subscribe_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe", target_type:"users" }
unsubscribe_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe", target_type:"users" }
subscribe_to_email_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe_to_email", target_type:"users" }
unsubscribe_to_email_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe_to_email", target_type:"users" }
subscribe_to_optional_target_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe_to_optional_target", target_type:"users" }
unsubscribe_to_optional_target_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe_to_optional_target", target_type:"users" }
You can also configure notification routes with scope like this:
scope :myscope, as: :myscope do
subscribed_by :users, routing_scope: :myscope
end
This routing_scope option creates the needed routes with specified scope like this:
# Subscription routes
myscope_user_subscriptions GET /myscope/users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions", action:"index", target_type:"users", routing_scope: :myscope }
find_myscope_user_subscriptions GET /myscope/users/:user_id/subscriptions/find(.:format)
{ controller:"activity_notification/subscriptions", action:"find", target_type:"users", routing_scope: :myscope }
myscope_user_subscription GET /myscope/users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions", action:"show", target_type:"users", routing_scope: :myscope }
PUT /myscope/users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions", action:"create", target_type:"users", routing_scope: :myscope }
DELETE /myscope/users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions", action:"destroy", target_type:"users", routing_scope: :myscope }
subscribe_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/subscribe(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe", target_type:"users", routing_scope: :myscope }
unsubscribe_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/unsubscribe(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe", target_type:"users", routing_scope: :myscope }
subscribe_to_email_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/subscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe_to_email", target_type:"users", routing_scope: :myscope }
unsubscribe_to_email_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/unsubscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe_to_email", target_type:"users", routing_scope: :myscope }
subscribe_to_optional_target_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/subscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions", action:"subscribe_to_optional_target", target_type:"users", routing_scope: :myscope }
unsubscribe_to_optional_target_myscope_user_subscription PUT /myscope/users/:user_id/subscriptions/:id/unsubscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions", action:"unsubscribe_to_optional_target", target_type:"users", routing_scope: :myscope }
When you use devise authentication and you want make subscription targets assciated with devise, you can create as follows in your routes:
subscribed_by :users, with_devise: :users
This with_devise option creates the needed routes assciated with devise authentication:
# Subscription with devise routes
user_subscriptions GET /users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"index", target_type:"users", devise_type:"users" }
find_user_subscriptions GET /users/:user_id/subscriptions/find(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"find", target_type:"users", devise_type:"users" }
user_subscription GET /users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"show", target_type:"users", devise_type:"users" }
PUT /users/:user_id/subscriptions(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"create", target_type:"users", devise_type:"users" }
DELETE /users/:user_id/subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"destroy", target_type:"users", devise_type:"users" }
subscribe_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe", target_type:"users", devise_type:"users" }
unsubscribe_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe", target_type:"users", devise_type:"users" }
subscribe_to_email_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe_to_email", target_type:"users", devise_type:"users" }
unsubscribe_to_email_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe_to_email", target_type:"users", devise_type:"users" }
subscribe_to_optional_target_user_subscription PUT /users/:user_id/subscriptions/:id/subscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe_to_optional_target", target_type:"users", devise_type:"users" }
unsubscribe_to_optional_target_user_subscription PUT /users/:user_id/subscriptions/:id/unsubscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe_to_optional_target", target_type:"users", devise_type:"users" }
When you use with_devise option and you want to make simple default routes as follows, you can use devise_default_routes option:
subscribed_by :users, with_devise: :users, devise_default_routes: true
These with_devise and devise_default_routes options create the needed routes assciated with authenticated devise resource as the default target
# Subscription with devise routes
user_subscriptions GET /subscriptions(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"index", target_type:"users", devise_type:"users" }
find_user_subscriptions GET /subscriptions/find(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"find", target_type:"users", devise_type:"users" }
user_subscription GET /subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"show", target_type:"users", devise_type:"users" }
PUT /subscriptions(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"create", target_type:"users", devise_type:"users" }
DELETE /subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"destroy", target_type:"users", devise_type:"users" }
subscribe_user_subscription PUT /subscriptions/:id/subscribe(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe", target_type:"users", devise_type:"users" }
unsubscribe_user_subscription PUT /subscriptions/:id/unsubscribe(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe", target_type:"users", devise_type:"users" }
subscribe_to_email_user_subscription PUT /subscriptions/:id/subscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe_to_email", target_type:"users", devise_type:"users" }
unsubscribe_to_email_user_subscription PUT /subscriptions/:id/unsubscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe_to_email", target_type:"users", devise_type:"users" }
subscribe_to_optional_target_user_subscription PUT /subscriptions/:id/subscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"subscribe_to_optional_target", target_type:"users", devise_type:"users" }
unsubscribe_to_optional_target_user_subscription PUT /subscriptions/:id/unsubscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_with_devise", action:"unsubscribe_to_optional_target", target_type:"users", devise_type:"users" }
When you use activity_notification controllers as REST API mode, you can create as follows in your routes:
scope :api do
scope :"v2" do
subscribed_by :users, api_mode: true
end
end
This api_mode option creates the needed routes as REST API:
# Subscription as API mode routes
GET /subscriptions(.:format)
{ controller:"activity_notification/subscriptions_api", action:"index", target_type:"users" }
GET /subscriptions/find(.:format)
{ controller:"activity_notification/subscriptions_api", action:"find", target_type:"users" }
GET /subscriptions/optional_target_names(.:format)
{ controller:"activity_notification/subscriptions_api", action:"optional_target_names", target_type:"users" }
GET /subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_api", action:"show", target_type:"users" }
PUT /subscriptions(.:format)
{ controller:"activity_notification/subscriptions_api", action:"create", target_type:"users" }
DELETE /subscriptions/:id(.:format)
{ controller:"activity_notification/subscriptions_api", action:"destroy", target_type:"users" }
PUT /subscriptions/:id/subscribe(.:format)
{ controller:"activity_notification/subscriptions_api", action:"subscribe", target_type:"users" }
PUT /subscriptions/:id/unsubscribe(.:format)
{ controller:"activity_notification/subscriptions_api", action:"unsubscribe", target_type:"users" }
PUT /subscriptions/:id/subscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_api", action:"subscribe_to_email", target_type:"users" }
PUT /subscriptions/:id/unsubscribe_to_email(.:format)
{ controller:"activity_notification/subscriptions_api", action:"unsubscribe_to_email", target_type:"users" }
PUT /subscriptions/:id/subscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_api", action:"subscribe_to_optional_target", target_type:"users" }
PUT /subscriptions/:id/unsubscribe_to_optional_target(.:format)
{ controller:"activity_notification/subscriptions_api", action:"unsubscribe_to_optional_target", target_type:"users" }
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/activity_notification/rails/routes.rb', line 342 def subscribed_by(*resources) = (:subscriptions, resources., [:new, :edit, :update]) resources.each do |target| [:defaults] = { target_type: target.to_s }.merge([:devise_defaults]) = .select { |key, _| [:api_mode, :with_devise, :devise_default_routes, :model, :devise_defaults].exclude? key } if [:with_devise].present? && [:devise_default_routes].present? create_subscription_routes , else self.resources target, only: :none do create_subscription_routes , end end end self end |