Skip to content

Instantly share code, notes, and snippets.

@pascalbetz
Last active December 2, 2015 09:55
Show Gist options
  • Select an option

  • Save pascalbetz/1c6481c8c8c8d11edf9e to your computer and use it in GitHub Desktop.

Select an option

Save pascalbetz/1c6481c8c8c8d11edf9e to your computer and use it in GitHub Desktop.

Revisions

  1. pascalbetz revised this gist Dec 2, 2015. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions assets_uri_helper.rb
    Original file line number Diff line number Diff line change
    @@ -16,10 +16,7 @@ module AssetUriHelpers

    # Generates the application-specific relative paths for assets
    def asset_path(*args)
    config = Config::Assets.new(application_config)
    binding.pry
    # TODO: schau mal config an
    assets_prefix = application_config.assets.prefix.to_s
    assets_prefix = asset_config.prefix
    args.push('') if args.empty?

    path_elements = ['', ASSETS_ROOT_DIRECTORY]
    @@ -45,14 +42,20 @@ def asset_url(*args)

    private

    def assets_class_name
    "#{application_module_name}::Assets"
    end
    def application_class_name
    "#{application_module_name}::Application"
    end

    def application_module_name
    self.class.name.split('::').first # extract app-name from class-name
    end

    def asset_config
    @_asset_config ||= Kernel.const_get(assets_class_name).configuration
    end

    def application_config
    @_application_config ||= Kernel.const_get(application_class_name).configuration
    end
  2. pascalbetz created this gist Dec 2, 2015.
    61 changes: 61 additions & 0 deletions assets_uri_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    require 'uri'

    require 'pry'

    module Lotus
    module Helpers
    # Helper methods to generate asset-paths
    #
    # @since 0.6.0
    # @api public
    module AssetUriHelpers
    # HTTP-path-separator according to https://tools.ietf.org/html/rfc1738 - 3.3 HTTP
    PATH_SEPARATOR = '/'.freeze

    ASSETS_ROOT_DIRECTORY = 'assets'.freeze

    # Generates the application-specific relative paths for assets
    def asset_path(*args)
    config = Config::Assets.new(application_config)
    binding.pry
    # TODO: schau mal config an
    assets_prefix = application_config.assets.prefix.to_s
    args.push('') if args.empty?

    path_elements = ['', ASSETS_ROOT_DIRECTORY]
    path_elements.concat(assets_prefix.split(PATH_SEPARATOR).compact) if !assets_prefix.empty?
    path_elements.concat(args)
    path_elements.join(PATH_SEPARATOR)
    end

    # Generates the application-specific absolute URL for assets
    def asset_url(*args)
    url_path = asset_path(args)

    url_scheme = application_config.scheme.to_s
    url_host = application_config.host.to_s # TODO: to_s should be done in application_config
    url_port = application_config.port.to_i # TODO: to_i should be done in application_config

    url_port = nil if url_port <= 0 # TODO: why could it <= 0 ?

    #binding.pry

    URI::Generic.build({scheme: url_scheme, host: url_host, port: url_port, path: url_path}).to_s
    end

    private

    def application_class_name
    "#{application_module_name}::Application"
    end

    def application_module_name
    self.class.name.split('::').first # extract app-name from class-name
    end

    def application_config
    @_application_config ||= Kernel.const_get(application_class_name).configuration
    end
    end
    end
    end