Sha256: e69945004f8a52834dfb7de326705cbaf3b4399b77735d47f69a316d8d525c76

Contents?: true

Size: 1.77 KB

Versions: 19

Compression:

Stored size: 1.77 KB

Contents

module Locomotive
  module Wagon

    class ThemeAssetDecorator < SimpleDelegator

      include ToHashConcern

      EXTENSIONS_TABLE = {
        '.scss'       => '.css',
        '.css.scss'   => '.css',
        '.sass'       => '.css',
        '.css.sass'   => '.css',
        '.less'       => '.css',
        '.css.less'   => '.css',
        '.coffee'     => '.js',
        '.js.coffee'  => '.js'
      }.freeze

      def __attributes__
        %i(source folder checksum)
      end

      def source
        Locomotive::Coal::UploadIO.new(filepath, nil, realname)
      end

      def priority
        stylesheet_or_javascript? ? 100 : 0
      end

      def stylesheet_or_javascript?
        File.extname(realname) =~ /^\.(css|scss|less|js|coffee)/
      end

      def checksum
        Digest::MD5.hexdigest(File.read(filepath))
      end

      # - memoize it because it will not change even if we change the filepath (or source)
      # - we keep the first extension and drop the others (.coffee, .scss, ...etc)
      def realname
        return @realname if @realname

        filename = File.basename(filepath)

        @realname = _realname(filename, 2) || _realname(filename, 1) || filename
      end

      def relative_url
        "#{folder.gsub('\\', '/')}/#{realname}"
      end

      def short_relative_url
        relative_url[/^(javascripts|stylesheets|fonts)\/(.*)$/, 2]
      end

      def filepath
        __getobj__.source
      end

      def filepath=(path)
        __getobj__[:source] = path
      end

      private

      def _realname(filename, length)
        extension = '.' + filename.split('.').last(length).join('.')

        if new_extension = EXTENSIONS_TABLE[extension]
          File.basename(filename, extension) + new_extension
        end
      end

    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.2.0.rc2 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.2.0.rc1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.2.0.beta1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.1.1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.1.0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.1.0.rc5 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.1.0.rc4 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.1.0.rc3 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc8 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc7 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc6 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc5 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc4 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc3 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-2.0.0.rc2 lib/locomotive/wagon/decorators/theme_asset_decorator.rb