Sha256: ad68f246e607bcce4e248e72cacccab13e92b27359674b27cceebad082dbd8ce

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module RailsConnector

  # 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

1 entries across 1 versions & 1 rubygems

Version Path
infopark_cloud_connector-7.1.0 app/helpers/rails_connector/display_helper.rb