Sha256: 44217b9e4b46046fbb5993cf352c0fb92760aed0a4909a966ade4e77fbb07382

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

# Replaces the [snippet_x] in all effective regions with static content

module Effective
  class SnippetReplacer
    include ActiveStorage::Blob::Analyzable
    include ActionView::Helpers::UrlHelper
    include ActionView::Helpers::AssetTagHelper

    def replace!
      raise('expected effective regions') unless defined?(Effective::Region)
      raise('expected effective assets') unless defined?(Effective::Asset)
      raise('expected active storage') unless defined?(ActiveStorage)

      Effective::Region.with_snippets.find_each do |region|
        region.snippet_objects.each do |snippet|
          print('.')

          begin
            case snippet.class.name
            when 'Effective::Snippets::EffectiveAsset'
              replace_effective_asset(region, snippet)
            else
              raise("unsupported snippet: #{snippet.class.name}")
            end
          rescue => e
            puts "\nError: #{e}\n"
            remove_snippet(region, snippet)
          end
        end

        region.save!
      end

      puts 'All Done. Have a great day.'
      true
    end

    private

    def replace_effective_asset(region, snippet)
      asset = snippet.asset
      raise("Effective:Asset id=#{snippet.asset_id || 'none'} does not exist") unless asset.present?

      blob = ActiveStorage::Blob.create_and_upload!(io: URI.open(asset.url), filename: asset.file_name)
      url = Rails.application.routes.url_helpers.rails_blob_url(blob, only_path: true)

      content = if asset.image?
        image_tag(url, class: snippet.html_class, alt: snippet.link_title)
      else
        link_to(snippet.link_title, url, class: snippet.html_class, title: snippet.link_title)
      end

      region.content.sub!("[#{snippet.id}]", content.to_s)
      region.snippets.delete(snippet.id)

      true
    end

    def remove_snippet(region, snippet)
      region.content.sub!("[#{snippet.id}]", '')
      region.snippets.delete(snippet.id)
    end


  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_developer-0.6.6 app/models/effective/snippet_replacer.rb
effective_developer-0.6.5 app/models/effective/snippet_replacer.rb