module Shortener::ShortenerHelper

  # generate a url from a url string
  def short_url(url, owner: nil, custom_key: nil, expires_at: nil,
                fresh: false, meta: nil, url_options: {})
    short_url = Shortener::ShortenedUrl.generate(url,
                                                 owner:      owner,
                                                 custom_key: custom_key,
                                                 expires_at: expires_at,
                                                 fresh:      fresh,
                                                 meta:       meta)

    if short_url
      options = { controller: :"shortener/shortened_urls", action: :show, id: short_url.unique_key, only_path: false }.merge(url_options)
      url_for(options)
    else
      url
    end
  end

end

module Shortener::ShortenerHelper
  DEFAULT_HTTP_PORT = 80.freeze

  # generate a url from a url string
  def short_url(url, owner: nil, custom_key: nil, expires_at: nil,
                fresh: false, meta: nil, url_options: {})
    short_url = Shortener::ShortenedUrl.generate(url,
                                                 owner:      owner,
                                                 custom_key: custom_key,
                                                 expires_at: expires_at,
                                                 fresh:      fresh,
                                                 meta:       meta)

    if short_url
      options = {
        controller: 'shortener/shortened_urls',
        action:     :show,
        id:         short_url.unique_key,
        only_path:  false,
        host:       request.host,
        protocol:   request.protocol
      }
      options.merge!({ port: request.port }) unless request.port == DEFAULT_HTTP_PORT

      Rails.application.routes.url_for(options.merge(url_options))
    else
      url
    end
  end

end

Fix shortener URL helper namespace
Fix shortener URL helper namespace