Sha256: be804e7f3690742328bf68d8a09836333d5ef47973a6d7c372442b721c34e3df

Contents?: true

Size: 1018 Bytes

Versions: 6

Compression:

Stored size: 1018 Bytes

Contents

require 'active_admin/arbre/html/element'

module Arbre
  class Context < Arbre::HTML::Element
    def indent_level
      # A context does not increment the indent_level
      super - 1
    end

    def bytesize
      cached_html.bytesize
    end
    alias :length :bytesize

    def respond_to?(method)
      super || cached_html.respond_to?(method)
    end

    # Webservers treat Arbre::Context as a string. We override
    # method_missing to delegate to the string representation
    # of the html.
    def method_missing(method, *args, &block)
      if cached_html.respond_to? method
        cached_html.send method, *args, &block
      else
        super
      end
    end

    private

    # Caches the rendered HTML so that we don't re-render just to
    # get the content lenght or to delegate a method to the HTML
    def cached_html
      if defined?(@cached_html)
        @cached_html
      else
        html = to_s
        @cached_html = html if html.length > 0
        html
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
activeadmin-0.4.4 lib/active_admin/arbre/context.rb
activeadmin-0.4.3 lib/active_admin/arbre/context.rb
activeadmin-0.4.2 lib/active_admin/arbre/context.rb
activeadmin-0.4.1 lib/active_admin/arbre/context.rb
activeadmin-0.4.0 lib/active_admin/arbre/context.rb
andrewroth_activeadmin-0.3.4.4 lib/active_admin/arbre/context.rb