File: comment_form.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (37 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

class CommentForm < Liquid::Block
  Syntax = /(#{Liquid::VariableSignature}+)/

  def initialize(tag_name, markup, options)
    super

    if markup =~ Syntax
      @variable_name = Regexp.last_match(1)
      @attributes    = {}
    else
      raise SyntaxError, "Syntax Error in 'comment_form' - Valid syntax: comment_form [article]"
    end
  end

  def render_to_output_buffer(context, output)
    article = context[@variable_name]

    context.stack do
      context['form'] = {
        'posted_successfully?' => context.registers[:posted_successfully],
        'errors' => context['comment.errors'],
        'author' => context['comment.author'],
        'email' => context['comment.email'],
        'body' => context['comment.body'],
      }

      output << wrap_in_form(article, render_all(@nodelist, context, output))
      output
    end
  end

  def wrap_in_form(article, input)
    %(<form id="article-#{article.id}-comment-form" class="comment-form" method="post" action="">\n#{input}\n</form>)
  end
end