Class: Locomotive::Steam::Liquid::Tags::ModelForm

Inherits:
Liquid::Block
  • Object
show all
Includes:
Concerns::Attributes
Defined in:
lib/locomotive/steam/liquid/tags/model_form.rb

Overview

Display the form html tag with the appropriate hidden fields in order to create a content entry from a public site. It handles callbacks, csrf and target url out of the box.

Usage:

model_form ‘newsletter_addresses’ %

<input type='text' name='content[email]' />
 <input type='submit' value='Add' />

endform_form %

model_form ‘newsletter_addresses’, class: ‘a-css-class’, success: ‘www.google.fr’, error: ‘/error’ %…endform_form %

Constant Summary collapse

Syntax =
/(#{::Liquid::QuotedFragment})\s*,*(.*)?/o.freeze

Instance Attribute Summary collapse

Attributes included from Concerns::Attributes

#attributes, #raw_attributes

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ ModelForm

Returns a new instance of ModelForm.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 27

def initialize(tag_name, markup, options)
  super

  if markup =~ Syntax
    @name, _attributes = $1, $2

    parse_attributes(_attributes)
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'model_form' - Valid syntax: model_form <content_type_slug>(, <attributes>)")
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 25

def name
  @name
end

Instance Method Details

#callbacks_html(options) ⇒ Object



63
64
65
66
67
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 63

def callbacks_html(options)
  options.slice(:success, :error).map do |(name, value)|
    html_tag :input, type: 'hidden', name: "#{name}_callback", value: value
  end.join('')
end

#content_type_html(name) ⇒ Object



53
54
55
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 53

def content_type_html(name)
  html_tag :input, type: 'hidden', name: 'content_type_slug', value: name
end

#csrf_html(context) ⇒ Object



57
58
59
60
61
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 57

def csrf_html(context)
  service = context.registers[:services].csrf_protection

  html_tag :input, type: 'hidden', name: service.field, value: service.token
end

#recaptcha_html(options) ⇒ Object



69
70
71
72
73
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 69

def recaptcha_html(options)
  return '' if options[:recaptcha] != true

  html_tag :input, type: 'hidden', name: 'g-recaptcha-response', id: 'g-recaptcha-response'
end

#render(context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 39

def render(context)
  @name = context[name]

  evaluate_attributes(context)

  form_attributes = prepare_form_attributes(context, attributes)

  (
    :form,
    content_type_html(name) + csrf_html(context) + callbacks_html(attributes) + recaptcha_html(attributes) + super,
    form_attributes
  )
end