Sha256: 169e691068aaa31f7890144885276cfdcb2febd5852dc8b9e032d473541c0ddc

Contents?: true

Size: 1.69 KB

Versions: 18

Compression:

Stored size: 1.69 KB

Contents

module ContentTagHelper
  def content_tag(*args, &block)
    if block_given?
      tag = Tag.new(*args)
      old_buf = @output_buffer
      @output_buffer = ActionView::OutputBuffer.new
      value = yield(tag)
      content = tag.render(@output_buffer.presence || value)
      @output_buffer = old_buf
      content
    else
      super
    end
  end

  private

  class Tag
    include ActionView::Helpers::CaptureHelper
    attr_accessor :id
    attr_reader :name, :css

    def initialize(name, *args)
      @name = name
      @attributes = tag_options(*args)
      @attributes[:class] = Tag::CSS.new(@attributes.fetch(:class, {}))
    end

    def tag_options(*args)
      options = nil
      args.each do |a|
        if a.is_a?(Hash)
          options = a
          break
        end
      end
      (options || {}).with_indifferent_access
    end

    def css
      @attributes[:class]
    end

    def []=(k,v)
      @attributes[k] = v
    end
    
    def render(content)
      "<#{name}#{render_attributes}>#{content.to_s.strip}</#{name}>".html_safe
    end

    def render_attributes
      attrs = @attributes.dup
      if css.empty?
        attrs.delete :class
      else
        attrs[:class] = css.to_s
      end

      attrs.keys.map do |k|
        "#{k}='#{attrs[k]}'"
      end.join(' ').prepend(' ').html_safe
    end


    class CSS
      
      def initialize(css)
        if css.is_a? String
          @internals = css.split(' ')
        else
          @internals = css.to_a
        end
      end

      def to_s
        @internals.uniq.join(' ').html_safe
      end

      def empty?
        @internals.empty?
      end

      def <<(name)
        @internals << name
        nil
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ecrire-0.28.3 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.28.2 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.28.1 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.28.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.27.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.26.3 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.26.2 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.26.1 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.26.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.25.2 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.25.1 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.25.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.24.1 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.24.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.23.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.22.1 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.21.0 lib/ecrire/app/helpers/content_tag_helper.rb
ecrire-0.20.0 lib/ecrire/app/helpers/content_tag_helper.rb