Module: RightSharding::Routing

Defined in:
lib/right_sharding/routing.rb

Overview

Rails routing functionality embedded into RightSharding. The module contains public utility methods; most of the actual routing and redirection logic is tucked away inside the routing middleware that lives under this namespace.

Class Method Summary collapse

Class Method Details

.add_account_to_path(original_path, account_id) ⇒ String

Prepend “/acct/X” to a URL path. Does not check if the path already contains this prefix; be careful!

Parameters:

  • original_path (String)

    the path-info of the original URI, e.g. “/dashboard;index”

  • account_id (Integer)

    the account ID to prepend to the path

Returns:

  • (String)

    the original_path with an account-id prefix



46
47
48
49
50
51
52
53
# File 'lib/right_sharding/routing.rb', line 46

def (original_path, )
  case original_path
  when /(?:^|\/)api(?:\/|$)/
    original_path.sub(/(^|\/)api(\/|$)/, '\1' + "api/acct/#{}" + '\2')
  else
    "/acct/#{}" + (original_path =~ /^\// ? "" : "/") + original_path
  end
end

.fetch_current_account_id_from_path(path) ⇒ String

Extract the current-account ID from a dashboard (right_site or embedded library) path.

Returns:

  • (String)

    a numeric string, or nil if the path does not include account info



36
37
38
# File 'lib/right_sharding/routing.rb', line 36

def (path)
  path[/(?:^|\/)acct\/(\d+)(?:\/|$)/, 1]
end

.skip_shard_routing?(path, request_method) ⇒ Boolean

Determine whether shard routing and account-prefixing should be skipped for a given path and request method. This delegates to a callback that should be provided by whoever initializes the Routing middleware.

Returns:

  • (Boolean)

    true if shard routing and account-prefixing should be skipped; false otherwise

See Also:



29
30
31
# File 'lib/right_sharding/routing.rb', line 29

def skip_shard_routing?(path, request_method)
  skip_shard_routing_callback.present? && skip_shard_routing_callback.call(path, request_method)
end

.skip_shard_routing_callbackObject

mattr_accessor is Rails feature so it is not gonna work when we run “rake spec”. # mattr_accessor :skip_shard_routing_callback



14
15
16
# File 'lib/right_sharding/routing.rb', line 14

def self.skip_shard_routing_callback
  @@skip_shard_routing_callback ||= nil
end

.skip_shard_routing_callback=(skip_shard_routing_callback) ⇒ Object



17
18
19
# File 'lib/right_sharding/routing.rb', line 17

def self.skip_shard_routing_callback=(skip_shard_routing_callback)
  @@skip_shard_routing_callback = skip_shard_routing_callback
end