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

Inherits:
Solid::Block
  • Object
show all
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 %

Instance Method Summary collapse

Instance Method Details

#callbacks_html(options) ⇒ Object



44
45
46
47
48
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 44

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



34
35
36
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 34

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

#csrf_htmlObject



38
39
40
41
42
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 38

def csrf_html
  service = current_context.registers[:services].csrf_protection

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

#display(*options, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/locomotive/steam/liquid/tags/model_form.rb', line 23

def display(*options, &block)
  name    = options.shift
  options = options.shift || {}

  form_attributes = prepare_form_attributes(options)

   :form,
    content_type_html(name) + csrf_html + callbacks_html(options) + yield,
    form_attributes
end