Module: Redirectr::ControllerMethods
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/redirectr.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#back_or_default(default = nil) ⇒ Object
Used in back links, referrer based redirection after actions etc.
-
#current_url(anchor: nil) ⇒ Object
Return the complete URL of the current request.
-
#default_url ⇒ Object
to be overwritten by your controllers.
- #in_whitelist?(parsed_url) ⇒ Boolean
- #redirect_to_with_whitelist(redirect_url) ⇒ Object
- #redirect_whitelist ⇒ Object
- #redirectr_referrer_token_path(rt) ⇒ Object
- #redirectr_referrer_token_url(rt) ⇒ Object
-
#referrer_or_current_url ⇒ Object
Return the referrer or the current path, it the former is not set.
-
#referrer_param ⇒ Object
Return the name of the parameter used to pass the referrer path.
-
#referrer_url ⇒ Object
reads referrer_param from HTTP params and validates it against the whitelist.
Instance Method Details
#back_or_default(default = nil) ⇒ Object
Used in back links, referrer based redirection after actions etc. Accepts a default redirect path in case no param is set, default being root_url. To set an own default path (per controller), you can overwrite the default_url method (see below). Example:
class MyController
def create
@my = My.create(...)
redirect_to back_or_default(my_url)
end
end
The above example will redirect to the referrer_url if it is defined, otherwise it will redirect to the my_url
Example:
class MyController
def create
@my = My.create(...)
redirect_to back_or_default
end
end
The above example will redirect to the referrer_url if it is defined, otherwise it will redirect to the root_url of the application.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/redirectr.rb', line 154 def back_or_default(default = nil) if self.referrer_url.present? self.referrer_url else url = default || self.default_url case url when nil raise Redirectr::InvalidUrl, 'No URL given' when String ReferrerToken(url) else ReferrerToken(url_for(url)) end end end |
#current_url(anchor: nil) ⇒ Object
Return the complete URL of the current request. Note that this does include ALL query parameters and the host name, thus allowing you to navigate back and forth between different hosts. If you want the pre-0.1.0 behaviour back, just overwrite this method in your controller so it returns “request.env”. Example:
<%= link_to my_messages_url referrer_param => current_url %>
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/redirectr.rb', line 95 def current_url(anchor: nil) url = if request.respond_to? :url # for rack >= 2.0.0 request.url elsif request.respond_to? :original_url # for rails >= 4.0.0 request.original_url else request.env['REQUEST_URI'] end if anchor if anchor.is_a?(ActiveRecord::Base) anchor = ActionView::RecordIdentifier.dom_id(anchor) end url = URI.parse(url.to_s) url.fragment = anchor url = url.to_s end ReferrerToken(url) end |
#default_url ⇒ Object
to be overwritten by your controllers
172 173 174 |
# File 'lib/redirectr.rb', line 172 def default_url root_url end |
#in_whitelist?(parsed_url) ⇒ Boolean
211 212 213 |
# File 'lib/redirectr.rb', line 211 def in_whitelist?(parsed_url) redirect_whitelist.find {|url| parsed_url.host == url.host and parsed_url.port == url.port } end |
#redirect_to_with_whitelist(redirect_url) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/redirectr.rb', line 195 def redirect_to_with_whitelist(redirect_url) case redirect_url when nil raise 'Cannot redirect to nil' when String parsed_url = URI.parse(redirect_url) if parsed_url.relative? or in_whitelist? parsed_url redirect_to parsed_url else raise Redirectr::UrlNotInWhitelist, "#{parsed_url.inspect} - #{redirect_whitelist.inspect}" end else redirect_to default end end |
#redirect_whitelist ⇒ Object
215 216 217 218 |
# File 'lib/redirectr.rb', line 215 def redirect_whitelist @redirect_whitelist ||= [URI.parse(self.current_url.to_s)] + Array(Redirectr.config.whitelist).map {|url| URI.parse url.to_s } end |
#redirectr_referrer_token_path(rt) ⇒ Object
71 72 73 |
# File 'lib/redirectr.rb', line 71 def redirectr_referrer_token_path(rt) rt.to_s end |
#redirectr_referrer_token_url(rt) ⇒ Object
67 68 69 |
# File 'lib/redirectr.rb', line 67 def redirectr_referrer_token_url(rt) rt.to_s end |
#referrer_or_current_url ⇒ Object
Return the referrer or the current path, it the former is not set. Useful in cases where there might be a redirect path that has to be taken note of but in case it is not present, the current path will be redirected back to. Example:
<%= link_to my_messages_url referrer_param => referrer_or_current_url %>
122 123 124 |
# File 'lib/redirectr.rb', line 122 def referrer_or_current_url referrer_url.blank? ? current_url : referrer_url end |
#referrer_param ⇒ Object
Return the name of the parameter used to pass the referrer path. Use this instead of the real param name in creating your own links to allow easily changing the name later Example:
<%= link_to my_messages_url :filter_by => 'date', referrer_param => current_url %>
82 83 84 |
# File 'lib/redirectr.rb', line 82 def referrer_param Redirectr::REFERRER_PARAM_NAME end |
#referrer_url ⇒ Object
reads referrer_param from HTTP params and validates it against the whitelist
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/redirectr.rb', line 177 def referrer_url return nil if params[referrer_param].blank? referrer_token = ReferrerToken.from_param params[referrer_param] raise Redirectr::InvalidReferrerToken, "no URL matches given token value #{params[referrer_param]}" if referrer_token.blank? parsed_url = URI.parse referrer_token.to_s if parsed_url.absolute? and in_whitelist? parsed_url referrer_token elsif parsed_url.relative? referrer_token elsif Redirectr.config.discard_referrer_on_invalid_origin nil else raise Redirectr::UrlNotInWhitelist, "#{parsed_url.inspect} - #{redirect_whitelist.inspect}" end end |