Class: Integral::ListItemRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::AssetTagHelper, ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper
Defined in:
lib/integral/list_item_renderer.rb

Overview

Handles safely rendering list items

Direct Known Subclasses

PartialListItemRenderer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_item, opts = {}) ⇒ ListItemRenderer

Returns a new instance of ListItemRenderer.

Parameters:

  • list_item (ListItem)

    object to render

  • opts (Hash) (defaults to: {})

    options hash



21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#list_itemObject

Returns the value of attribute list_item.



9
10
11
# File 'lib/integral/list_item_renderer.rb', line 9

def list_item
  @list_item
end

#optsObject

Returns the value of attribute opts.



9
10
11
# File 'lib/integral/list_item_renderer.rb', line 9

def opts
  @opts
end

Class Method Details

.render(list_item, opts = {}) ⇒ String

Renders the provided list item with options given

Returns:

  • (String)

    the rendered list item



14
15
16
17
# File 'lib/integral/list_item_renderer.rb', line 14

def self.render(list_item, opts = {})
  renderer = new(list_item, opts)
  renderer.render
end

Instance Method Details

#descriptionString

Returns description of list item.

Returns:

  • (String)

    description of list item



88
89
90
# File 'lib/integral/list_item_renderer.rb', line 88

def description
  provide_attr(:description)
end

#fallback_imageString

Returns path to fallback image for list items.

Returns:

  • (String)

    path to fallback image for list items



157
158
159
# File 'lib/integral/list_item_renderer.rb', line 157

def fallback_image
  ActionController::Base.helpers.image_path('integral/defaults/no_image_available.jpg')
end

#image(version = nil) ⇒ String

Returns the image URL.

Returns:

  • (String)

    the image URL



141
142
143
144
145
146
147
148
# File 'lib/integral/list_item_renderer.rb', line 141

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_optionsHash

Returns list item options.

Returns:

  • (Hash)

    list item options



53
54
55
56
57
58
59
60
# 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_imageObject

Returns the non object image path



129
130
131
132
133
134
135
136
# File 'lib/integral/list_item_renderer.rb', line 129

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?Boolean

Returns whether item has an image linked to it (which isn’t through an object).

Returns:

  • (Boolean)

    whether item has an image linked to it (which isn’t through an object)



124
125
126
# File 'lib/integral/list_item_renderer.rb', line 124

def non_object_image?
  list_item.image.present?
end

#object_imageObject

Returns the non object image path



114
115
116
117
118
119
120
121
# File 'lib/integral/list_item_renderer.rb', line 114

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

#renderString

Renders the provided list_item

Returns:

  • (String)

    the rendered list item (including possible children)



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/integral/list_item_renderer.rb', line 34

def render
  return render_no_object_warning if list_item.object? && !object_available?

   opts[:wrapper_element], class: html_classes do
    if list_item.has_children?
      concat render_item
      concat  opts[:child_wrapper_element], render_children, { class: opts[:child_wrapper_class] }, false
    else
      render_item
    end
  end
end

#render_childrenString

Loop over all list item children calling render on each

Returns:

  • (String)

    compiled string of all the rendered list item children



65
66
67
68
69
70
71
72
73
# 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_itemString

Returns list item HTML.

Returns:

  • (String)

    list item HTML



48
49
50
# File 'lib/integral/list_item_renderer.rb', line 48

def render_item
   :a, title, item_options
end

#subtitleString

Returns subtitle of list item.

Returns:

  • (String)

    subtitle of list item



109
110
111
# File 'lib/integral/list_item_renderer.rb', line 109

def subtitle
  provide_attr(:subtitle)
end

#targetString

Returns target of list item.

Returns:

  • (String)

    target of list item



93
94
95
# File 'lib/integral/list_item_renderer.rb', line 93

def target
  list_item.target unless list_item.basic?
end

#titleString

Returns title of list item.

Returns:

  • (String)

    title of list item



83
84
85
# File 'lib/integral/list_item_renderer.rb', line 83

def title
  provide_attr(:title)
end

#title_required?Boolean

TODO: This and other methods which are only used in backend could be moved to decorators

Returns:

  • (Boolean)

    whether or not title is a required attribute



152
153
154
# File 'lib/integral/list_item_renderer.rb', line 152

def title_required?
  !list_item.object?
end

#type_for_dropdownObject

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



77
78
79
80
# 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

#urlString

Returns URL of list item.

Returns:

  • (String)

    URL of list item



98
99
100
101
102
103
104
105
106
# File 'lib/integral/list_item_renderer.rb', line 98

def url
  return if list_item.basic?
  url = provide_attr(:url)

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

  "#{Rails.application.routes.default_url_options[:host]}#{url}"
end