Sha256: f367f96ca0dd4b5150cc686cdade0bbc681726daf331f29a6be95e62e821b828

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 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 Occams::Content::Tag::Helper < Occams::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

    return if @method_name.present?

    raise Error, 'Missing method name for helper tag'
  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 = Occams.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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
occams-1.0.7.3 lib/occams/content/tags/helper.rb
occams-1.0.7.2 lib/occams/content/tags/helper.rb
occams-1.0.7.1 lib/occams/content/tags/helper.rb
occams-1.0.7 lib/occams/content/tags/helper.rb
occams-1.0.6.1 lib/occams/content/tags/helper.rb
occams-1.0.6 lib/occams/content/tags/helper.rb
occams-1.0.5 lib/occams/content/tags/helper.rb
occams-1.0.4 lib/occams/content/tags/helper.rb
occams-1.0.3 lib/occams/content/tags/helper.rb
occams-1.0.2 lib/occams/content/tags/helper.rb