class Som::Liquid::Tags::ImprovedLinkTo
Constants
- Syntax
Attributes
current_page[RW]
Public Class Methods
new(tag_name, markup, options)
click to toggle source
Calls superclass method
# File lib/som/liquid/tags/improved_link_to.rb, line 15 def initialize(tag_name, markup, options) markup =~ Syntax self.set_options(markup, options) super end
Public Instance Methods
render(context)
click to toggle source
Calls superclass method
# File lib/som/liquid/tags/improved_link_to.rb, line 21 def render(context) self.current_page = context.registers[:page] render_path(context) do |page, path| label = label_from_page(page) if render_as_block? context.stack do context.scopes.last['target'] = page label = super.html_safe end end tag_attributes = build_tag_attributes(page) %{<a #{tag_attributes} href="#{path}">#{label}</a>} end end
Protected Instance Methods
build_tag_attributes(page)
click to toggle source
# File lib/som/liquid/tags/improved_link_to.rb, line 53 def build_tag_attributes(page) css_class = page_active?(page) ? "#{@_options[:class]} active".strip : "#{@_options[:class]}" tag_attributes = css_class.blank? ? "" : %{class="#{css_class}" } @_options.each do |key, value| unless (key == :class || value.blank?) attribute = key.to_s attribute = attribute.gsub('_','-') if attribute.include? "data_" tag_attributes << "#{attribute}=#{value} " end end tag_attributes.strip end
label_from_page(page)
click to toggle source
# File lib/som/liquid/tags/improved_link_to.rb, line 41 def label_from_page(page) if page.templatized? page.send(:_source).content_entry._label else page.title end end
page_active?(page)
click to toggle source
# File lib/som/liquid/tags/improved_link_to.rb, line 49 def page_active?(page) self.current_page.fullpath =~ /^#{page.fullpath}(\/.*)?$/ end
set_options(markup, options)
click to toggle source
# File lib/som/liquid/tags/improved_link_to.rb, line 66 def set_options(markup, options) @_options = {id: '', class: ''} markup.scan(::Liquid::TagAttributes) { |key, value| @_options[key.to_sym] = value.gsub(/"|'/, '') } end