Sha256: 7d261e1e6e4457676bd8d3b9d489f6bc02348628a99eed0b3766e7b7644a070d

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

module Scrivito

  # This module contains a helper that can be used to render attributes of the CMS models.
  # @api public
  module DisplayHelper

    # For a consistent look of your site and easy maintenance, only the display helpers
    # should be used to render an attribute <em>value</em> of a CMS model.
    #
    #   <%= display_value @obj.title %>
    # Renders the value, taking its type into account.
    # * An HtmlString will be exported by converting its links
    # * A Time will be exported in an international form: Year-Month-Day Hour:Minutes
    # * A non-HTML String will be quoted
    # * other values will be rendered unchanged
    #
    # @api public
    def display_value(value)
      case value
        when HtmlString then convert_links(value).html_safe
        when String then h(value)
        when Time then h(l(value))
        else value
      end
    end

    def display_original_value(value)
      case value
        when HtmlString then convert_links(value, :dont_resolve_blobs => true).html_safe
        else display_value(value)
      end
    end

    # Renders a field from the CMS.
    #
    # <%= display_field @obj, :title %>
    #
    # @api public
    def display_field(obj, attr, options = {})
      display_value obj[attr]
    end

    # Legacy method - deprecated
    #
    # Use display_value and display_field instead
    def display(*args)
      if args.last.kind_of? Hash
        options = args.pop
      else
        options = {}
      end
      return display_value(args.first) if args.size == 1
      display_field(args[0], args[1], options)
    end

    private

    def convert_links(html, cms_path_options = {})
      if html
        html.gsub(%r{<?\bobjid:([a-f0-9]{16})\b>?}) do
          if obj = Obj.find_by_id($1)
            cms_path(obj, cms_path_options)
          else
            "#__target_object_not_reachable"
          end
        end
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scrivito_sdk-0.16.0 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.15.0 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.14.0 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.13.0 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.12.0 app/helpers/scrivito/display_helper.rb