public
Last active

Force non SSL Rails Concern. Inverse of force_ssl method. Accepts same options. Add to your app/controllers/concerns/ directory.

  • Download Gist
force_non_ssl.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Usage:
# In your (Application)Controller:
# include Concerns::ForceNonSSL
# force_non_ssl
#
# You can use the same options as with force_ssl.
# See: http://api.rubyonrails.org/classes/ActionController/ForceSSL/ClassMethods.html#method-i-force_ssl
#
# Code based on: https://github.com/rails/rails/blob/ab08519b1aed46dbd4b3e13932bbaddfe42d8315/actionpack/lib/action_controller/metal/force_ssl.rb
#
require 'active_support/concern'
module Concerns::ForceNonSSL
 
extend ActiveSupport::Concern
 
ACTION_OPTIONS = [:only, :except, :if, :unless]
URL_OPTIONS = [:protocol, :host, :domain, :subdomain, :port, :path]
REDIRECT_OPTIONS = [:status, :flash, :alert, :notice]
 
module ClassMethods
 
def force_non_ssl(options = {})
action_options = options.slice(*ACTION_OPTIONS)
redirect_options = options.except(*ACTION_OPTIONS)
before_filter(action_options) do
force_non_ssl_redirect(redirect_options)
end
end
 
end
 
def force_non_ssl_redirect(host_or_options = nil)
if request.ssl?
options = {
:protocol => 'http://',
:host => request.host,
:path => request.fullpath,
:status => :moved_permanently
}
 
if host_or_options.is_a?(Hash)
options.merge!(host_or_options)
elsif host_or_options
options.merge!(:host => host_or_options)
end
 
non_secure_url = ActionDispatch::Http::URL.url_for(options.slice(*URL_OPTIONS))
flash.keep if respond_to?(:flash)
redirect_to non_secure_url, options.slice(*REDIRECT_OPTIONS)
end
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.