Sha256: d16af5db9800df575dd771001345faf720a49d5c3cd15ac1243b1fccdb952e04

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

require 'tempfile'

module Locomotive::Wagon

  class PushThemeAssetsCommand < PushBaseCommand

    def entities
      repositories.theme_asset.all.map do |entity|
        decorated = ThemeAssetDecorator.new(entity)
      end.sort { |a, b| a.priority <=> b.priority }
    end

    def decorate(entity)
      entity # already decorated
    end

    def persist(decorated_entity)
      precompile(decorated_entity)

      if (_entity = remote_entity(decorated_entity)).nil?
        api_client.theme_assets.create(decorated_entity.to_hash)
      else
        raise SkipPersistingException.new if same?(decorated_entity, _entity)

        api_client.theme_assets.update(_entity._id, decorated_entity.to_hash)
      end
    end

    def label_for(decorated_entity)
      decorated_entity.relative_url
    end

    private

    def precompile(entity)
      return unless entity.stylesheet_or_javascript?

      Tempfile.new(entity.realname).tap do |file|
        file.write(sprockets_env[entity.short_relative_url].to_s)

        entity.filepath = file.path

        file.close
      end
    end

    def same?(decorated_entity, remote_entity)
      remote_entity.try(:checksum) == decorated_entity.checksum
    end

    def remote_entity(local_entity)
      remote_entities[local_entity.relative_url]
    end

    def remote_entities
      return @remote_entities if @remote_entities

      @remote_entities = {}.tap do |hash|
        api_client.theme_assets.all.each do |entity|
          relative_url = "#{entity.folder}/#{entity.local_path}"
          hash[relative_url] = entity
        end
      end
    end

    def sprockets_env
      @sprockets_env ||= Locomotive::Steam::SprocketsEnvironment.new(File.join(path, 'public'), minify: true)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.0.0.pre.beta.1 lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb
locomotivecms_wagon-2.0.0.pre.alpha.3 lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb
locomotivecms_wagon-2.0.0.pre.alpha.2 lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb