Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
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

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

Examples:

Define notify_to in config/routes.rb

notify_to :users

Define notify_to with options

notify_to :users, only: [:open, :open_all, :move]

Integrated with Devise authentication

notify_to :users, with_devise: :users

Define notification paths including subscription paths

notify_to :users, with_subscription: true

Integrated with Devise authentication as simple default routes including subscription management

notify_to :users, with_devise: :users, devise_default_routes: true, with_subscription: true

Integrated with Devise authentication as simple default routes with scope including subscription management

scope :myscope, as: :myscope do
  notify_to :myscope, with_devise: :users, devise_default_routes: true, with_subscription: true, routing_scope: :myscope
end

Define notification paths as API mode including subscription paths

scope :api do
  scope :"v2" do
    notify_to :users, api_mode: true, with_subscription: true
  end
end

Parameters:

  • resources (Symbol)

    Resources to notify

Options Hash (*options):

  • :routing_scope (String) — default: nil

    Routing scope for notification routes

  • :with_devise (Symbol) — default: false

    Devise resources name for devise integration. Devise integration will be enabled by this option.

  • :devise_default_routes (Boolean) — default: false

    Whether you will create routes as device default routes assciated with authenticated devise resource as the default target

  • :api_mode (Boolean) — default: false

    Whether you will use activity_notification controllers as REST API mode

  • :with_subscription (Hash|Boolean) — default: false

    Subscription path options to define subscription management paths with notification paths. Calls subscribed_by routing when truthy value is passed as this option.

  • :model (String) — default: :notifications

    Model name of notifications

  • :controller (String) — default: "activity_notification/notifications" | activity_notification/notifications_with_devise"

    :controller option as resources routing

  • :as (Symbol) — default: nil

    :as option as resources routing

  • :only (Array) — default: nil

    :only option as resources routing

  • :except (Array) — default: nil

    :except option as resources routing

Returns:



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)
  options = create_options(:notifications, resources.extract_options!, [:new, :create, :edit, :update])

  resources.each do |target|
    options[:defaults] = { target_type: target.to_s }.merge(options[:devise_defaults])
    resources_options = options.select { |key, _| [:api_mode, :with_devise, :devise_default_routes, :with_subscription, :subscription_option, :model, :devise_defaults].exclude? key }
    if options[:with_devise].present? && options[:devise_default_routes].present?
      create_notification_routes options, resources_options
    else
      self.resources target, only: :none do
        create_notification_routes options, resources_options
      end
    end

    if options[:with_subscription].present? && target.to_s.to_model_class.subscription_enabled?
      subscribed_by target, options[: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" }

Examples:

Define subscribed_by in config/routes.rb

subscribed_by :users

Define subscribed_by with options

subscribed_by :users, except: [:index, :show]

Integrated with Devise authentication

subscribed_by :users, with_devise: :users

Define subscription paths as API mode

scope :api do
  scope :"v2" do
    subscribed_by :users, api_mode: true
  end
end

Parameters:

  • resources (Symbol)

    Resources to notify

Options Hash (*options):

  • :routing_scope (String) — default: nil

    Routing scope for subscription routes

  • :with_devise (Symbol) — default: false

    Devise resources name for devise integration. Devise integration will be enabled by this option.

  • :devise_default_routes (Boolean) — default: false

    Whether you will create routes as device default routes assciated with authenticated devise resource as the default target

  • :api_mode (Boolean) — default: false

    Whether you will use activity_notification controllers as REST API mode

  • :model (String) — default: :subscriptions

    Model name of subscriptions

  • :controller (String) — default: "activity_notification/subscriptions" | activity_notification/subscriptions_with_devise"

    :controller option as resources routing

  • :as (Symbol) — default: nil

    :as option as resources routing

  • :only (Array) — default: nil

    :only option as resources routing

  • :except (Array) — default: nil

    :except option as resources routing

Returns:



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)
  options = create_options(:subscriptions, resources.extract_options!, [:new, :edit, :update])

  resources.each do |target|
    options[:defaults] = { target_type: target.to_s }.merge(options[:devise_defaults])
    resources_options = options.select { |key, _| [:api_mode, :with_devise, :devise_default_routes, :model, :devise_defaults].exclude? key }
    if options[:with_devise].present? && options[:devise_default_routes].present?
      create_subscription_routes options, resources_options
    else
      self.resources target, only: :none do
        create_subscription_routes options, resources_options
      end
    end
  end

  self
end