Class: DatGretel::Renderer::LinkCollection
- Inherits:
-
Array
- Object
- Array
- DatGretel::Renderer::LinkCollection
- Defined in:
- lib/dat_gretel/renderer.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#breadcrumb_link_to(name, url, options = {}) ⇒ Object
Proxy for
context.link_to
that can be overridden by plugins. -
#initialize(context, links, options = {}) ⇒ LinkCollection
constructor
A new instance of LinkCollection.
-
#keys ⇒ Object
Helper for returning all link keys to allow for simple testing.
-
#method_missing(method, *args, &block) ⇒ Object
Proxy to view context.
-
#render ⇒ Object
(also: #to_s)
Renders the links into breadcrumbs.
-
#render_fragment(fragment_tag, text, url, semantic, options = {}) ⇒ Object
Renders HTML for a breadcrumb fragment, i.e.
-
#render_nonsemantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object
Renders regular, non-semantic fragment HTML.
-
#render_semantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object
Renders semantic fragment HTML.
Constructor Details
#initialize(context, links, options = {}) ⇒ LinkCollection
Returns a new instance of LinkCollection.
159 160 161 162 |
# File 'lib/dat_gretel/renderer.rb', line 159 def initialize(context, links, = {}) @context, @links, @options = context, links, 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.
254 255 256 |
# File 'lib/dat_gretel/renderer.rb', line 254 def method_missing(method, *args, &block) context.send(method, *args, &block) end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
157 158 159 |
# File 'lib/dat_gretel/renderer.rb', line 157 def context @context end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
157 158 159 |
# File 'lib/dat_gretel/renderer.rb', line 157 def links @links end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
157 158 159 |
# File 'lib/dat_gretel/renderer.rb', line 157 def @options end |
Instance Method Details
#breadcrumb_link_to(name, url, options = {}) ⇒ Object
Proxy for context.link_to
that can be overridden by plugins.
249 250 251 |
# File 'lib/dat_gretel/renderer.rb', line 249 def (name, url, = {}) context.link_to(name, url, ) end |
#keys ⇒ Object
Helper for returning all link keys to allow for simple testing.
165 166 167 |
# File 'lib/dat_gretel/renderer.rb', line 165 def keys map(&:key) end |
#render ⇒ Object Also known as: to_s
Renders the links into breadcrumbs.
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 196 197 198 199 200 201 |
# File 'lib/dat_gretel/renderer.rb', line 170 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| if [:fragment_class].present? render_fragment([:fragment_tag], link.text, link.url, [:semantic], class: [:fragment_class]) else render_fragment([:fragment_tag], link.text, link.url, [:semantic]) end 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([:fragment_tag], current_link.text, ([:link_current] ? current_link.url : nil), [:semantic], class: [:current_class], current_link: current_link.url) # Build the final HTML html_fragments = [] if [:pretext].present? html_fragments << content_tag(:span, [:pretext], class: [:pretext_class]) end html_fragments << fragments.join([:separator]) if [:posttext].present? html_fragments << content_tag(:span, [:posttext], class: [:posttext_class]) end html = html_fragments.join(" ").html_safe content_tag([:container_tag], html, id: [:id], class: [:class]) end |
#render_fragment(fragment_tag, text, url, semantic, options = {}) ⇒ Object
Renders HTML for a breadcrumb fragment, i.e. a breadcrumb link.
206 207 208 209 210 211 212 |
# File 'lib/dat_gretel/renderer.rb', line 206 def render_fragment(fragment_tag, text, url, semantic, = {}) if semantic render_semantic_fragment(fragment_tag, text, url, ) else render_nonsemantic_fragment(fragment_tag, text, url, ) end end |
#render_nonsemantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object
Renders regular, non-semantic fragment HTML.
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/dat_gretel/renderer.rb', line 235 def render_nonsemantic_fragment(fragment_tag, text, url, = {}) if fragment_tag text = (text, url) if url.present? content_tag(fragment_tag, text, class: [:class]) elsif url.present? (text, url, class: [:class]) elsif [:class].present? content_tag(:span, text, class: [:class]) else text end end |
#render_semantic_fragment(fragment_tag, text, url, options = {}) ⇒ Object
Renders semantic fragment HTML.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/dat_gretel/renderer.rb', line 215 def render_semantic_fragment(fragment_tag, text, url, = {}) if fragment_tag text = content_tag(:span, text, itemprop: "title") if url.present? text = (text, url, itemprop: "url") elsif [:current_link].present? current_url = "#{root_url}#{[:current_link].gsub(/^\//, '')}" text = text + tag(:meta, itemprop: "url", content: current_url) end content_tag(fragment_tag, text, class: [:class], itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb") elsif url.present? content_tag(:span, (content_tag(:span, text, itemprop: "title"), url, class: [:class], itemprop: "url"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb") else content_tag(:span, content_tag(:span, text, class: [:class], itemprop: "title"), itemscope: "", itemtype: "http://data-vocabulary.org/Breadcrumb") end end |