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
-
.add_account_to_path(original_path, account_id) ⇒ String
Prepend “/acct/X” to a URL path.
-
.fetch_current_account_id_from_path(path) ⇒ String
Extract the current-account ID from a dashboard (right_site or embedded library) path.
-
.skip_shard_routing?(path, request_method) ⇒ Boolean
Determine whether shard routing and account-prefixing should be skipped for a given path and request method.
-
.skip_shard_routing_callback ⇒ Object
mattr_accessor is Rails feature so it is not gonna work when we run “rake spec”.
- .skip_shard_routing_callback=(skip_shard_routing_callback) ⇒ Object
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!
46 47 48 49 50 51 52 53 |
# File 'lib/right_sharding/routing.rb', line 46 def add_account_to_path(original_path, account_id) case original_path when /(?:^|\/)api(?:\/|$)/ original_path.sub(/(^|\/)api(\/|$)/, '\1' + "api/acct/#{account_id}" + '\2') else "/acct/#{account_id}" + (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.
36 37 38 |
# File 'lib/right_sharding/routing.rb', line 36 def fetch_current_account_id_from_path(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.
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_callback ⇒ Object
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 |