Class: Integral::ListItemRenderer
- Inherits:
-
Object
- Object
- Integral::ListItemRenderer
- 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
Instance Attribute Summary collapse
-
#list_item ⇒ Object
Returns the value of attribute list_item.
-
#opts ⇒ Object
Returns the value of attribute opts.
Class Method Summary collapse
-
.render(list_item, opts = {}) ⇒ String
Renders the provided list item with options given.
Instance Method Summary collapse
-
#description ⇒ String
Description of list item.
-
#fallback_image ⇒ String
Path to fallback image for list items.
-
#image(version = nil) ⇒ String
The image URL.
-
#initialize(list_item, opts = {}) ⇒ ListItemRenderer
constructor
A new instance of ListItemRenderer.
-
#item_options ⇒ Hash
List item options.
-
#non_object_image ⇒ Object
Returns the non object image path.
-
#non_object_image? ⇒ Boolean
Whether item has an image linked to it (which isn’t through an object).
-
#object_image ⇒ Object
Returns the non object image path.
-
#render ⇒ String
Renders the provided list_item.
-
#render_children ⇒ String
Loop over all list item children calling render on each.
-
#render_item ⇒ String
List item HTML.
-
#subtitle ⇒ String
Subtitle of list item.
-
#target ⇒ String
Target of list item.
-
#title ⇒ String
Title of list item.
-
#title_required? ⇒ Boolean
TODO: This and other methods which are only used in backend could be moved to decorators.
-
#type_for_dropdown ⇒ Object
Used within backend for preselecting type in dropdown TODO: Move this onto the model level.
-
#url ⇒ String
URL of list item.
Constructor Details
#initialize(list_item, opts = {}) ⇒ ListItemRenderer
Returns a new instance of ListItemRenderer.
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_item ⇒ Object
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 |
#opts ⇒ Object
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
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
#description ⇒ String
Returns description of list item.
88 89 90 |
# File 'lib/integral/list_item_renderer.rb', line 88 def description provide_attr(:description) end |
#fallback_image ⇒ String
Returns 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.
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_options ⇒ Hash
Returns list item options.
53 54 55 56 57 58 59 60 |
# File 'lib/integral/list_item_renderer.rb', line 53 def 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 ⇒ Object
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).
124 125 126 |
# File 'lib/integral/list_item_renderer.rb', line 124 def non_object_image? list_item.image.present? end |
#object_image ⇒ Object
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 |
#render ⇒ String
Renders the provided list_item
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? 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 ⇒ String
Loop over all list item children calling render on each
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_item ⇒ String
Returns list item HTML.
48 49 50 |
# File 'lib/integral/list_item_renderer.rb', line 48 def render_item content_tag :a, title, end |
#subtitle ⇒ String
Returns subtitle of list item.
109 110 111 |
# File 'lib/integral/list_item_renderer.rb', line 109 def subtitle provide_attr(:subtitle) end |
#target ⇒ String
Returns 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 |
#title ⇒ String
Returns 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
152 153 154 |
# File 'lib/integral/list_item_renderer.rb', line 152 def title_required? !list_item.object? end |
#type_for_dropdown ⇒ Object
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 |
#url ⇒ String
Returns 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.[:host]}#{url}" end |