Sha256: 9a62396fe6e78a75fbcc7229c861c005618a7e61e407768f52ddafb03c8576d2

Contents?: true

Size: 1.8 KB

Versions: 65

Compression:

Stored size: 1.8 KB

Contents

module Scrivito
  class AttributeValueRenderer < Struct.new(:view)
    def render(value, type)
      method_name = "render_as_#{type}"
      respond_to?(method_name) ? public_send(method_name, value) : value
    end

    def render_as_date(date)
      if date
        html_escape(localize(date))
      end
    end

    def render_as_enum(enum)
      enum.to_s
    end

    def render_as_html(html)
      if html
        cms_routing = CmsRouting.new(view.request, view, view.scrivito_engine)
        cms_routing.convert_links(html).html_safe
      end
    end

    def render_as_link(link)
      if link
        link_to(link.display_title, scrivito_path(link), target: link.target)
      end
    end

    def render_as_linklist(linklist)
      if linklist.any?
        content_tag(:ul) do
          capture do
            linklist.each do |link|
              concat content_tag(:li) { render_as_link(link) }
            end
          end
        end
      end
    end

    def render_as_multienum(multienum)
      render_as_stringlist(multienum)
    end

    def render_as_reference(reference)
      reference.try(:description_for_editor)
    end

    def render_as_referencelist(referencelist)
      if referencelist.any?
        content_tag(:ul) do
          capture do
            referencelist.each do |reference|
              concat content_tag(:li) { render_as_reference(reference) }
            end
          end
        end
      end
    end

    def render_as_string(string)
      string.is_a?(HtmlString) ? render_as_html(string) : html_escape(string)
    end

    def render_as_stringlist(stringlist)
      stringlist.join(', ')
    end

    private

    delegate :capture, :concat, :content_tag, :localize, :link_to, :scrivito_path, to: :view

    def html_escape(string)
      ERB::Util.html_escape(string)
    end
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
scrivito_sdk-1.5.1.rc2 lib/scrivito/attribute_value_renderer.rb
scrivito_sdk-1.5.1.rc1 lib/scrivito/attribute_value_renderer.rb
scrivito_sdk-1.5.0 lib/scrivito/attribute_value_renderer.rb
scrivito_sdk-1.5.0.rc2 lib/scrivito/attribute_value_renderer.rb
scrivito_sdk-1.5.0.rc1 lib/scrivito/attribute_value_renderer.rb