Class: PufferPages::Liquid::Tags::Url

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/puffer_pages/liquid/tags/url.rb

Constant Summary collapse

Syntax =
/^(#{::Liquid::VariableSignature}+)\s*(.*)\s*/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Url

Returns a new instance of Url.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puffer_pages/liquid/tags/url.rb', line 8

def initialize(tag_name, markup, tokens)
  if markup =~ Syntax

    @helper_name, @arguments, @attributes = $1, [], {}

    @arguments = $2.split(',')
    attributes = @arguments.pop if @arguments.last && @arguments.last.strip =~ /(#{::Liquid::TagAttributes})/

    @arguments = @arguments.map do |var|
      var.strip =~ /(#{::Liquid::QuotedFragment})/
      $1 ? $1 : nil
    end.compact

    attributes.scan(::Liquid::TagAttributes) do |key, value|
      @attributes[key] = value
    end if attributes.present?
  else
    raise SyntaxError.new("Error in tag '#{tag_name}' - Valid syntax: #{tag_name} helper_name [object, object...] [, option:value option:value...]")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puffer_pages/liquid/tags/url.rb', line 31

def render(context)
  key = context[@key]
  arguments = @arguments.map do |argument|
    argument = context[argument]
    argument.to_param if argument.is_a?(::Liquid::Drop)
    argument
  end
  attributes = @attributes.each_with_object({}) do |(name, value), result|
    result[name.to_sym] = context[value]
  end
  attributes.merge(:path_only => true) if @tag_name == 'path'

  context.registers[:controller].send("#{@helper_name}_#{@tag_name}", *arguments, attributes)
end