module DefaultUrlOptions

Public Class Methods

autoconfigure(&block) click to toggle source
# File lib/rails_default_url_options.rb, line 179
def DefaultUrlOptions.autoconfigure(&block)
  DefaultUrlOptions.autoconfigure!(&block)
end
autoconfigure!(&block) click to toggle source
# File lib/rails_default_url_options.rb, line 164
def DefaultUrlOptions.autoconfigure!(&block)
  unless DefaultUrlOptions.configured?
    DefaultUrlOptions.configure(
      :host => 'localhost',
      :port => 3000
    )
  end

  if block
    DefaultUrlOptions.callbacks.push(block)
  end

  DefaultUrlOptions.install_before_action!
end
callbacks() click to toggle source
# File lib/rails_default_url_options.rb, line 157
def DefaultUrlOptions.callbacks
  @callbacks ||= []
end
configure(request = {}, &block) click to toggle source
# File lib/rails_default_url_options.rb, line 42
def DefaultUrlOptions.configure(request = {}, &block)
  default_url_options = DefaultUrlOptions

  default_url_options.clear

  if request.is_a?(Hash)
    protocol = request[:protocol] || request['protocol']
    host = request[:host] || request['host']
    port = request[:port] || request['port']
  else
    protocol = request.protocol
    host = request.host
    port = request.port
  end

  default_url_options[:protocol] = protocol
  default_url_options[:host] = host
  default_url_options[:port] = port

  normalize!

# force action_mailer to not suck so bad
#
  Rails.configuration.action_mailer.default_url_options = default_url_options

  if ActionMailer::Base.respond_to?('default_url_options=')
    ActionMailer::Base.default_url_options = default_url_options
  end

  default_url_options
end
configure!(request = {}) click to toggle source
# File lib/rails_default_url_options.rb, line 74
def DefaultUrlOptions.configure!(request = {})
  configure(request) unless configured?
ensure
  configured!
end
configured!() click to toggle source
# File lib/rails_default_url_options.rb, line 80
def DefaultUrlOptions.configured!
  @configured = true
end
configured?() click to toggle source
# File lib/rails_default_url_options.rb, line 84
def DefaultUrlOptions.configured?
  defined?(@configured) and @configured
end
dependencies() click to toggle source
# File lib/rails_default_url_options.rb, line 37
def DefaultUrlOptions.dependencies
  {}
end
domain() click to toggle source
# File lib/rails_default_url_options.rb, line 142
def DefaultUrlOptions.domain
  case host
    when '0.0.0.0', '127.0.0.1'
      'localhost'
    when /\A[\d.]+\Z/iomx
      host
    else
      host.split('.').last(2).join('.')
  end
end
host() click to toggle source
# File lib/rails_default_url_options.rb, line 134
def DefaultUrlOptions.host
  DefaultUrlOptions[:host]
end
install_before_action!() click to toggle source
# File lib/rails_default_url_options.rb, line 183
def DefaultUrlOptions.install_before_action!
  if defined?(::ActionController::Base)
    ::ActionController::Base.module_eval do
      def configure_default_url_options!
        unless DefaultUrlOptions.configured?
          if Rails.env.production?
            DefaultUrlOptions.configure!(request)
          else
            DefaultUrlOptions.configure(request)
          end

          DefaultUrlOptions.callbacks.each do |block|
            block.call
          end

          DefaultUrlOptions.callbacks.clear
        end
      end

      prepend_before_action = respond_to?(:prepend_before_filter) ? :prepend_before_filter : :prepend_before_action

      send(prepend_before_action, :configure_default_url_options!)
    end
  end
end
normalize!(&block) click to toggle source
# File lib/rails_default_url_options.rb, line 96
def DefaultUrlOptions.normalize!(&block)
  if block
    @normalize = block
    return
  end

  if @normalize
    return instance_eval(&@normalize)
  end

  case protocol.to_s
    when /https/i
      delete(:port) if port.to_s == '443'

    when /http/i
      delete(:port) if port.to_s == '80'
  end

  keys.each do |key|
    if self[key].nil?
      delete(key)
      next
    end

    if key.is_a?(Symbol)
      next
    end

    self[key.to_s.to_sym] = self.delete(key)
  end

  self
end
port() click to toggle source
# File lib/rails_default_url_options.rb, line 138
def DefaultUrlOptions.port
  DefaultUrlOptions[:port]
end
protocol() click to toggle source
# File lib/rails_default_url_options.rb, line 130
def DefaultUrlOptions.protocol
  DefaultUrlOptions[:protocol]
end
to_yaml(*args, &block) click to toggle source
# File lib/rails_default_url_options.rb, line 153
def DefaultUrlOptions.to_yaml(*args, &block)
  Hash.new.update(self).to_yaml(*args, &block)
end
version() click to toggle source
# File lib/rails_default_url_options.rb, line 33
def DefaultUrlOptions.version
  '6.0.0'
end

Public Instance Methods

configure_default_url_options!() click to toggle source
# File lib/rails_default_url_options.rb, line 186
def configure_default_url_options!
  unless DefaultUrlOptions.configured?
    if Rails.env.production?
      DefaultUrlOptions.configure!(request)
    else
      DefaultUrlOptions.configure(request)
    end

    DefaultUrlOptions.callbacks.each do |block|
      block.call
    end

    DefaultUrlOptions.callbacks.clear
  end
end