class Integral::ListItemRenderer

Handles safely rendering list items

Attributes

list_item[RW]
opts[RW]

Public Class Methods

new(list_item, opts = {}) click to toggle source

@param list_item [ListItem] object to render @param opts [Hash] options hash

# File lib/integral/list_item_renderer.rb, line 21
def initialize(list_item, opts = {})
  @opts = opts.reverse_merge(
    wrapper_element: 'li',
    child_wrapper_element: 'ul',
    child_wrapper_class: ''
  )

  @list_item = list_item
end
render(list_item, opts = {}) click to toggle source

Renders the provided list item with options given

@return [String] the rendered list item

# File lib/integral/list_item_renderer.rb, line 14
def self.render(list_item, opts = {})
  renderer = new(list_item, opts)
  renderer.render
end

Public Instance Methods

description() click to toggle source

@return [String] description of list item

# File lib/integral/list_item_renderer.rb, line 89
def description
  provide_attr(:description)
end
fallback_image() click to toggle source

@return [String] path to fallback image for list items

# File lib/integral/list_item_renderer.rb, line 159
def fallback_image
  ActionController::Base.helpers.image_path('integral/defaults/no_image_available.jpg')
end
image(version = nil) click to toggle source

@parameter version [Symbol] the version of the image so use if associated image is a file

@return [String] the image URL

# File lib/integral/list_item_renderer.rb, line 143
def image(version = nil)
  image = provide_attr(:image)

  return image.file.url(version) if image.respond_to?(:file)
  return image if image.present?

  fallback_image
end
item_options() click to toggle source

@return [Hash] list item options

# File lib/integral/list_item_renderer.rb, line 53
def item_options
  opts = {}
  opts[:class] = 'dropdown-button' if list_item.has_children?
  opts[:href] = url if url.present?
  opts[:target] = target if target.present? && target != '_self'

  opts
end
non_object_image() click to toggle source

Returns the non object image path

# File lib/integral/list_item_renderer.rb, line 131
def non_object_image
  image = list_item.image

  return image.file.url if image.respond_to?(:file)
  return image if image.present?

  fallback_image
end
non_object_image?() click to toggle source

@return [Boolean] whether item has an image linked to it (which isn't through an object)

# File lib/integral/list_item_renderer.rb, line 126
def non_object_image?
  list_item.image.present?
end
object_image() click to toggle source

Returns the non object image path

# File lib/integral/list_item_renderer.rb, line 116
def object_image
  image = object_data[:image] if object_available?

  return image.file.url if image.respond_to?(:file)
  return image if image.present?

  fallback_image
end
render() click to toggle source

Renders the provided list_item

@return [String] the rendered list item (including possible children)

# File lib/integral/list_item_renderer.rb, line 34
def render
  return render_no_object_warning if list_item.object? && !object_available?

  content_tag opts[:wrapper_element], class: html_classes do
    if list_item.has_children?
      concat render_item
      concat content_tag opts[:child_wrapper_element], render_children, { class: opts[:child_wrapper_class] }, false
    else
      render_item
    end
  end
end
render_children() click to toggle source

Loop over all list item children calling render on each

@return [String] compiled string of all the rendered list item children

# File lib/integral/list_item_renderer.rb, line 65
def render_children
  children = ''

  list_item.children.each do |child|
    children += self.class.render(child, opts)
  end

  children
end
render_item() click to toggle source

@return [String] list item HTML

# File lib/integral/list_item_renderer.rb, line 48
def render_item
  content_tag :a, title, item_options
end
subtitle() click to toggle source

@return [String] subtitle of list item

# File lib/integral/list_item_renderer.rb, line 111
def subtitle
  provide_attr(:subtitle)
end
target() click to toggle source

@return [String] target of list item

# File lib/integral/list_item_renderer.rb, line 94
def target
  list_item.target unless list_item.basic?
end
title() click to toggle source

@return [String] title of list item

# File lib/integral/list_item_renderer.rb, line 84
def title
  provide_attr(:title)
end
title_required?() click to toggle source

@return [Boolean] whether or not title is a required attribute TODO: This and other methods which are only used in backend could be moved to decorators

# File lib/integral/list_item_renderer.rb, line 154
def title_required?
  !list_item.object?
end
type_for_dropdown() click to toggle source

Used within backend for preselecting type in dropdown TODO: Move this onto the model level

# File lib/integral/list_item_renderer.rb, line 77
def type_for_dropdown
  return list_item.type unless list_item.object?

  list_item.object_type.to_s
end
url() click to toggle source

@return [String] URL of list item

# File lib/integral/list_item_renderer.rb, line 99
def url
  return if list_item.basic?

  url = provide_attr(:url)

  return url if url.nil? || url.empty?
  return CGI.unescape(url) if url.match?(URI::DEFAULT_PARSER.make_regexp)

  CGI.unescape("#{Rails.application.routes.default_url_options[:host]}#{url}")
end

Private Instance Methods

html_classes() click to toggle source
# File lib/integral/list_item_renderer.rb, line 200
def html_classes
  return list_item.html_classes unless opts[:html_classes].present?

  "#{list_item.html_classes} #{opts[:html_classes]}"
end
object_available?() click to toggle source
# File lib/integral/list_item_renderer.rb, line 196
def object_available?
  @object_available ||= list_item.object? && list_item.object.present?
end
object_data() click to toggle source
# File lib/integral/list_item_renderer.rb, line 192
def object_data
  @object_data ||= list_item.object.to_list_item
end
provide_attr(attr) click to toggle source

Works out what the provided attr evaluates to.

@param attr [Symbol] attribute to evaluate

@return [String] value of attribute

# File lib/integral/list_item_renderer.rb, line 176
def provide_attr(attr)
  list_item_attr_value = list_item.public_send(attr)

  # Provide user supplied attr
  return list_item_attr_value if list_item_attr_value.present?

  # Provide object supplied attr
  return object_data[attr] if object_available?

  # Provide error - Object is linked but has been deleted
  return 'Object Unavailable' if list_item.object? && !object_available?

  # Provide empty string - no attr supplied and no object linked
  ''
end
render_no_object_warning() click to toggle source
# File lib/integral/list_item_renderer.rb, line 165
def render_no_object_warning
  message = "Tried to render a list item (##{list_item.id}) with no object."
  Rails.logger.debug("IntegralMessage: #{message}")
  "<!-- Warning: #{message} -->"
end