Sha256: 121d9fb29fd2048dc03a119f5a32f03e6153d8c3d025ca7fe50e9b330127195a

Contents?: true

Size: 1.4 KB

Versions: 14

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# 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.
#
class ComfortableMexicanSofa::Content::Tag::Helper < ComfortableMexicanSofa::Content::Tag

  BLACKLIST = %w[eval class_eval instance_eval render].freeze

  attr_reader :method_name

  def initialize(context:, params: [], source: nil)
    super
    @method_name = params.shift

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

  # we output erb into rest of the content
  def allow_erb?
    true
  end

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

  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

end

ComfortableMexicanSofa::Content::Renderer.register_tag(
  :helper, ComfortableMexicanSofa::Content::Tag::Helper
)

Version data entries

14 entries across 14 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/content/tags/helper.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/content/tags/helper.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/content/tags/helper.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/helper.rb
kcy1-1.0 lib/comfortable_mexican_sofa/content/tags/helper.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/helper.rb
comfortable_mexican_sofa-2.0.17 lib/comfortable_mexican_sofa/content/tags/helper.rb
comfortable_mexican_sofa-2.0.16 lib/comfortable_mexican_sofa/content/tags/helper.rb