Sha256: 565e1ca57250cf682e6f8664d163035abd3ec8269f78fe88bb1bca1d812066ee

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

module Spina
  module Embeds
    module TrixConversion
      extend ActiveSupport::Concern
      
      # Wrap rendered partial in an <embed> tag for Trix
      def to_trix_attachment(content = trix_attachment_content)        
        wrap_with_embed_tag(content)
      end
      
      def wrap_with_embed_tag(html)
        element = html_document.create_element("spina-embed", embed_tag_attributes)
        element.inner_html = html
        element.to_html
      end
      
      private
      
        def embed_tag_attributes
          spina_attributes.merge({"data-embed-attributes": embed_attributes_to_json})
        end
      
        def embed_attributes_to_json
          JSON.generate Hash[self.class.embed_attributes.map{|a| [a.to_s, send(a)]}]
        end
        
        def spina_attributes
          {"data-embed-type": self.class.name, "data-controller": "embed-tag"}
        end
      
        def trix_attachment_content
          ApplicationController.render(partial: to_trix_partial_path, formats: :html, object: self, as: model_name.element)
        end
        
        def html_document
          Nokogiri::HTML::Document.new.tap{|doc| doc.encoding = "UTF-8"}
        end
      
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spina-2.12.0 lib/spina/embeds/trix_conversion.rb
spina-2.11.0 lib/spina/embeds/trix_conversion.rb
spina-2.10.0 lib/spina/embeds/trix_conversion.rb
spina-2.9.1 lib/spina/embeds/trix_conversion.rb
spina-2.9.0 lib/spina/embeds/trix_conversion.rb
spina-2.8.1 lib/spina/embeds/trix_conversion.rb
spina-2.8.0 lib/spina/embeds/trix_conversion.rb
spina-2.7.0 lib/spina/embeds/trix_conversion.rb
spina-2.6.2 lib/spina/embeds/trix_conversion.rb
spina-2.6.1 lib/spina/embeds/trix_conversion.rb
spina-2.6.0 lib/spina/embeds/trix_conversion.rb
spina-2.5.0 lib/spina/embeds/trix_conversion.rb