Skip to content

Instantly share code, notes, and snippets.

@upinetree
Last active February 18, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upinetree/d744798e134a709169ec to your computer and use it in GitHub Desktop.
Save upinetree/d744798e134a709169ec to your computer and use it in GitHub Desktop.
module SslRedirectable
extend ActiveSupport::Concern
included do
before_action :force_non_ssl, if: :force_non_ssl?
class_attribute :ssl_actions
SSL_ACTIONS_ALL = [:all]
def self.force_ssl(options = {})
self.ssl_actions = options[:only] || SSL_ACTIONS_ALL
super
end
end
def force_non_ssl?
request.ssl? && request.get? && !ssl_required?
end
def ssl_required?
return false unless ssl_actions
ssl_actions.include?(action_name.to_sym) || ssl_actions == SSL_ACTIONS_ALL
end
def force_non_ssl
redirect_to non_ssl_url
flash.keep
end
def non_ssl_url
URI(request.original_url).tap { |u| u.scheme = 'http' }.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment