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 value 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, :ignore_body_data_url => true).html_safe else display_value(value) end end # Renders a field from the CMS, including an edit marker for the preview # If the option :marker is +false+, no edit marker will be rendered. # # <%= display_field @obj, :title %> # # When creating an edit marker, all options except :marker are passed to {MarkerHelper#edit_marker}. # # @api public def display_field(obj, attr, options = {}) options.reverse_merge!({ :marker => ![:id, :path, :created, :last_changed, :version].include?(attr.to_sym) }) if options.delete :marker edit_marker(obj, attr, options) do |obj, attr| display_value obj[attr] end else display_value obj[attr] end 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{?}) do cms_path(Obj.find($1), cms_path_options) end end end end end