Sha256: f09a68e2f66d3dfd1327a5dda24fc3c1068210fad643859ba71160054f884374

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 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

    # 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

3 entries across 3 versions & 1 rubygems

Version Path
scrivito_sdk-0.18.1 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.18.0 app/helpers/scrivito/display_helper.rb
scrivito_sdk-0.17.0 app/helpers/scrivito/display_helper.rb