Class: ComfortableMexicanSofa::Content::Tag::Partial

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

Overview

Tag for injecting partials. Example tag:

{{cms:partial path/to/partial, foo: bar, zip: zoop}}

This expands into a familiar:

<%= render partial: "path/to/partial", locals: {foo: bar, zip: zoop} %>

Whitelist is can be used to control what partials are renderable.

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from ComfortableMexicanSofa::Content::Tag

#nodes

Constructor Details

#initialize(context:, params: [], source: nil) ⇒ Partial

Returns a new instance of Partial.


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

def initialize(context:, params: [], source: nil)
  super
  @locals = params.extract_options!
  @path   = params[0]

  unless @path.present?
    raise Error, "Missing path for partial tag"
  end
end

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals


11
12
13
# File 'lib/comfortable_mexican_sofa/content/tags/partial.rb', line 11

def locals
  @locals
end

#pathObject (readonly)

Returns the value of attribute path


11
12
13
# File 'lib/comfortable_mexican_sofa/content/tags/partial.rb', line 11

def path
  @path
end

Instance Method Details

#allow_erb?Boolean

we output erb into rest of the content

Returns:

  • (Boolean)

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

def allow_erb?
  true
end

#contentObject


28
29
30
31
32
33
34
# File 'lib/comfortable_mexican_sofa/content/tags/partial.rb', line 28

def content
  format(
    "<%%= render partial: %<path>p, locals: %<locals>s %%>",
    path: @path,
    locals: @locals
  )
end

#renderObject


36
37
38
39
40
41
42
43
# File 'lib/comfortable_mexican_sofa/content/tags/partial.rb', line 36

def render
  whitelist = ComfortableMexicanSofa.config.allowed_partials
  if whitelist.is_a?(Array)
    whitelist.member?(@path) ? content : ""
  else
    content
  end
end