class Locomotive::Steam::Middlewares::EntrySubmission

Submit a content entry and persist it

Constants

CONTENT_TYPE_PARAM
ENTRY_SUBMISSION_REGEXP
HTTP_REGEXP
SUBMITTED_PARAM
SUBMITTED_TYPE_PARAM

Public Instance Methods

_call() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 17
def _call
  # we didn't go through the locale middleware yet,
  # so set the locale manually. Needed to build a localized
  # version of the entry + error messages (if present).
  with_locale do
    if slug = get_content_type_slug
      entry = create_entry(slug)
      navigation_behavior(entry)
    else
      fetch_entry
    end
  end
end
navigation_behavior(entry) click to toggle source

Render or redirect depending on:

  • the status of the content entry (valid or not)

  • the presence of a callback or not

  • the type of response asked by the browser (html or json)

navigation_error(entry) click to toggle source
navigation_html_error(entry) click to toggle source
navigation_success(entry) click to toggle source

Private Instance Methods

create_entry(slug) click to toggle source

Create a content entry with a minimal validation.

@param [ String ] slug The slug (or permalink) of the content type

# File lib/locomotive/steam/middlewares/entry_submission.rb, line 144
def create_entry(slug)
  if !is_recaptcha_valid?(slug, params[:'g-recaptcha-response'])
    build_invalid_recaptcha_entry(slug, entry_attributes)
  elsif entry = entry_submission.submit(slug, entry_attributes)
    entry
  else
    raise %{Unknown content type "#{slug}" or public_submission_enabled property not true}
  end
end
csrf_field() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 183
def csrf_field
  services.csrf_protection.field
end
entry_attributes() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 175
def entry_attributes
  HashConverter.to_sym(params[:entry] || params[:content] || {})
end
entry_submission() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 179
def entry_submission
  services.entry_submission
end
entry_submissions_path?() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 126
def entry_submissions_path?
  !(request.path_info =~ ENTRY_SUBMISSION_REGEXP).nil?
end
entry_to_query_string(entry) click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 78
def entry_to_query_string(entry)
  service_params = [
    csrf_field,
    CONTENT_TYPE_PARAM,
    SUBMITTED_TYPE_PARAM,
    SUBMITTED_PARAM,
    'success_callback',
    'error_callback',
    'content',
    'entry'
  ]

  [].tap do |list|
    params.each do |key, value|
      next if service_params.include?(key)
      list << "#{key}=#{value}"
    end

    list << "#{SUBMITTED_TYPE_PARAM}=#{entry.content_type_slug}"
    list << "#{SUBMITTED_PARAM}=#{entry._slug}"
  end.join('&')
end
error_location() click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 114
def error_location; location(:error); end
fetch_entry() click to toggle source

Get the content entry from the params.

# File lib/locomotive/steam/middlewares/entry_submission.rb, line 156
def fetch_entry
  if (type_slug = params[SUBMITTED_TYPE_PARAM]) && (slug = params[SUBMITTED_PARAM])
    if entry = entry_submission.find(type_slug, slug)
      store_in_liquid(entry)
    end
  end
end
get_content_type_slug() click to toggle source

Get the slug (or permalink) of the content type either from the PATH_INFO variable (old way) or from the presence of the content_type_slug param (model_form tag).

# File lib/locomotive/steam/middlewares/entry_submission.rb, line 133
def get_content_type_slug
  if request.post? && (request.path_info =~ ENTRY_SUBMISSION_REGEXP || params[CONTENT_TYPE_PARAM])
    $1 || params[CONTENT_TYPE_PARAM]
  end
end
json_response(entry, status = 200) click to toggle source

Build the JSON response

@param [ Integer ] status The HTTP return code

@return [ Array ] The rack response depending on the validation status and the requested format

# File lib/locomotive/steam/middlewares/entry_submission.rb, line 170
def json_response(entry, status = 200)
  json = entry_submission.to_json(entry)
  render_response(json, status, 'application/json')
end
location(state, query = '') click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 116
def location(state, query = '')
  location = params[:"#{state}_callback"] || (entry_submissions_path? ? '/' : request.path_info)

  if query.blank?
    location
  else
    location += (location.include?('?') ? '&' : '?') + query
  end
end
store_in_liquid(entry) click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 74
def store_in_liquid(entry)
  liquid_assigns[entry.content_type_slug.singularize] = entry
end
success_location(query) click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 113
def success_location(query); location(:success, query); end
with_locale(&block) click to toggle source
# File lib/locomotive/steam/middlewares/entry_submission.rb, line 101
def with_locale(&block)
  locale = default_locale || params[:locale]

  if request.path_info =~ /^\/(#{site.locales.join('|')})+(\/|$)/
    locale = $1
  end

  services.locale = locale

  I18n.with_locale(locale, &block)
end