Class: ComfortableMexicanSofa::Content::Tag::Helper

Inherits:
ComfortableMexicanSofa::Content::Tag show all
Defined in:
lib/comfortable_mexican_sofa/content/tags/helper.rb

Overview

Tag for injecting view helpers. Example tag:

{{cms:helper method_name, param_a, param_b, foo: bar}}

This expands into something like this:

<%= method_name("param_a", "param_b", "foo" => "bar") %>

Whitelist is can be used to control what helpers are available. By default there’s a blacklist of methods that should not be called.

Constant Summary collapse

BLACKLIST =
%w(eval class_eval instance_eval render)

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params

Instance Method Summary collapse

Methods inherited from ComfortableMexicanSofa::Content::Tag

#nodes, #parse_params_string

Constructor Details

#initialize(context, params_string) ⇒ Helper

Returns a new instance of Helper.



14
15
16
17
18
19
20
21
# File 'lib/comfortable_mexican_sofa/content/tags/helper.rb', line 14

def initialize(context, params_string)
  super
  @method_name = params.shift

  unless @method_name.present?
    raise Error, "Missing method name for helper tag"
  end
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



12
13
14
# File 'lib/comfortable_mexican_sofa/content/tags/helper.rb', line 12

def method_name
  @method_name
end

Instance Method Details

#allow_erbObject

we output erb into rest of the content



24
25
26
# File 'lib/comfortable_mexican_sofa/content/tags/helper.rb', line 24

def allow_erb
  true
end

#contentObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/comfortable_mexican_sofa/content/tags/helper.rb', line 28

def content
  helper_params = params.map do |p|
    case p
    when Hash
      p.to_s
    else
      "\"#{p}\""
    end
  end.join(",")
  "<%= #{method_name}(#{helper_params}) %>"
end

#renderObject



40
41
42
43
44
45
46
47
# File 'lib/comfortable_mexican_sofa/content/tags/helper.rb', line 40

def render
  whitelist = ComfortableMexicanSofa.config.allowed_helpers
  if whitelist.is_a?(Array)
    content if whitelist.map!(&:to_s).member?(method_name)
  else
    content unless BLACKLIST.member?(method_name)
  end
end