Class: Gretel::Renderer::LinkCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/gretel/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, links, options = {}) ⇒ LinkCollection

Returns a new instance of LinkCollection.



157
158
159
160
# File 'lib/gretel/renderer.rb', line 157

def initialize(context, links, options = {})
  @context, @links, @options = context, links, options
  concat links
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Proxy to view context.



241
242
243
# File 'lib/gretel/renderer.rb', line 241

def method_missing(method, *args, &block)
  context.send(method, *args, &block)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



155
156
157
# File 'lib/gretel/renderer.rb', line 155

def context
  @context
end

Returns the value of attribute links.



155
156
157
# File 'lib/gretel/renderer.rb', line 155

def links
  @links
end

#optionsObject (readonly)

Returns the value of attribute options.



155
156
157
# File 'lib/gretel/renderer.rb', line 155

def options
  @options
end

Instance Method Details

Proxy for context.link_to that can be overridden by plugins.



236
237
238
# File 'lib/gretel/renderer.rb', line 236

def breadcrumb_link_to(name, url, options = {})
  context.link_to(name, url, options)
end

#keysObject

Helper for returning all link keys to allow for simple testing.



163
164
165
# File 'lib/gretel/renderer.rb', line 163

def keys
  map(&:key)
end

#renderObject Also known as: to_s

Renders the links into breadcrumbs.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gretel/renderer.rb', line 168

def render
  return "" if links.empty?

  # Loop through all but the last (current) link and build HTML of the fragments
  fragments = links[0..-2].map do |link|
    render_fragment(options[:fragment_tag], link.text, link.url, options[:semantic])
  end

  # The current link is handled a little differently, and is only linked if specified in the options
  current_link = links.last
  fragments << render_fragment(options[:fragment_tag], current_link.text, (options[:link_current] ? current_link.url : nil), options[:semantic], class: options[:current_class])

  # Build the final HTML
  html_fragments = []

  if options[:pretext].present?
    html_fragments << (:span, options[:pretext], class: options[:pretext_class])
  end

  html_fragments << fragments.join(options[:separator])

  if options[:posttext].present?
    html_fragments << (:span, options[:posttext], class: options[:posttext_class])
  end

  html = html_fragments.join(" ").html_safe
  (options[:container_tag], html, id: options[:id], class: options[:class])
end

#render_fragment(fragment_tag, text, url, semantic, options = {}) ⇒ Object

Renders HTML for a breadcrumb fragment, i.e. a breadcrumb link.



200
201
202
203
204
205
206
# File 'lib/gretel/renderer.rb', line 200

def render_fragment(fragment_tag, text, url, semantic, options = {})
  if semantic
    render_semantic_fragment(fragment_tag, text, url, options)
  else
    render_nonsemantic_fragment(fragment_tag, text, url, options)
  end
end

#render_nonsemantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object

Renders regular, non-semantic fragment HTML.



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gretel/renderer.rb', line 222

def render_nonsemantic_fragment(fragment_tag, text, url, options = {})
  if fragment_tag
    text = breadcrumb_link_to(text, url) if url.present?
    (fragment_tag, text, class: options[:class])
  elsif url.present?
    breadcrumb_link_to(text, url, class: options[:class])
  elsif options[:class].present?
    (:span, text, class: options[:class])
  else
    text
  end
end

#render_semantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object

Renders semantic fragment HTML.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gretel/renderer.rb', line 209

def render_semantic_fragment(fragment_tag, text, url, options = {})
  if fragment_tag
    text = (:span, text, itemprop: "title")
    text = breadcrumb_link_to(text, url, itemprop: "url") if url.present?
    (fragment_tag, text, class: options[:class], itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  elsif url.present?
    (:span, breadcrumb_link_to((:span, text, itemprop: "title"), url, class: options[:class], itemprop: "url"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  else
    (:span, (:span, text, class: options[:class], itemprop: "title"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb")
  end
end