class Locomotive::Steam::Liquid::Tags::ModelForm
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 %}
Constants
- Syntax
Attributes
name[R]
Public Class Methods
new(tag_name, markup, options)
click to toggle source
Calls superclass method
# 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
Public Instance Methods
callbacks_html(options)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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) html_content_tag( :form, content_type_html(name) + csrf_html(context) + callbacks_html(attributes) + recaptcha_html(attributes) + super, form_attributes ) end
Private Instance Methods
action_url(context, options)
click to toggle source
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 101 def action_url(context, options) url = options[:action] if url.blank? if options[:json] url = context['path'].blank? ? '/' : context['path'] url + (url.ends_with?('/') ? 'index.json' : '.json') else nil end else url = '/' + url unless url.starts_with?('/') url_builder(context).prefix(url) end end
html_content_tag(name, content, options = {})
click to toggle source
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 77 def html_content_tag(name, content, options = {}) "<#{name} #{inline_options(options)}>#{content}</#{name}>" end
html_tag(name, options = {})
click to toggle source
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 81 def html_tag(name, options = {}) "<#{name} #{inline_options(options)} />" end
inline_options(options = {})
click to toggle source
Write options (Hash) into a string according to the following pattern: <key1>=“<value1>”, <key2>=“<value2”, …etc
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 87 def inline_options(options = {}) return '' if options.empty? (options.stringify_keys.to_a.collect { |a, b| "#{a}=\"#{b}\"" }).join(' ') end
prepare_form_attributes(context, options)
click to toggle source
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 92 def prepare_form_attributes(context, options) url = action_url(context, options) attributes = options.slice(:id, :class, :name, :novalidate) { method: 'POST', enctype: 'multipart/form-data' }.merge(attributes).tap do |_attributes| _attributes[:action] = url if url end end
url_builder(context)
click to toggle source
# File lib/locomotive/steam/liquid/tags/model_form.rb, line 117 def url_builder(context) context.registers[:services].url_builder end