Class: Locomotive::Steam::Middlewares::EntrySubmission

Inherits:
ThreadSafe
  • Object
show all
Includes:
Concerns::Helpers, Concerns::Recaptcha
Defined in:
lib/locomotive/steam/middlewares/entry_submission.rb

Overview

Submit a content entry and persist it

Constant Summary collapse

HTTP_REGEXP =
/^https?:\/\//o
ENTRY_SUBMISSION_REGEXP =
/^\/entry_submissions\/(\w+)/o
SUBMITTED_TYPE_PARAM =
'submitted_type_slug'
SUBMITTED_PARAM =
'submitted_entry_slug'
CONTENT_TYPE_PARAM =
'content_type_slug'

Constants included from Concerns::Helpers

Concerns::Helpers::CACHE_HEADERS, Concerns::Helpers::HTML_CONTENT_TYPE, Concerns::Helpers::HTML_MIME_TYPES

Instance Attribute Summary

Attributes inherited from ThreadSafe

#env

Instance Method Summary collapse

Methods included from Concerns::Recaptcha

#build_invalid_recaptcha_entry, #is_recaptcha_required?, #is_recaptcha_valid?, #is_recaptcha_verified?

Methods included from Concerns::Helpers

#html?, #inject_cookies, #json?, #log, #make_local_path, #modify_path, #mounted_on, #redirect_to, #render_response

Methods inherited from ThreadSafe

#call, #decorate_entry, #default_liquid_context, #default_locale, #liquid_assigns, #live_editing?, #locale, #locales, #merge_with_params, #next, #page, #params, #path, #repositories, #request, #services, #session, #site

Instance Method Details

#_callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

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)



36
37
38
39
40
41
42
43
44
# File 'lib/locomotive/steam/middlewares/entry_submission.rb', line 36

def navigation_behavior(entry)
  if entry.nil?
    raise 'TODO'
  elsif entry.errors.empty?
    navigation_success(entry)
  else
    navigation_error(entry)
  end
end


54
55
56
57
58
59
60
# File 'lib/locomotive/steam/middlewares/entry_submission.rb', line 54

def navigation_error(entry)
  if html?
    navigation_html_error(entry)
  elsif json?
    json_response(entry, 422)
  end
end


62
63
64
65
66
67
68
69
70
# File 'lib/locomotive/steam/middlewares/entry_submission.rb', line 62

def navigation_html_error(entry)
  if error_location =~ HTTP_REGEXP
    redirect_to error_location
  else
    env['PATH_INFO'] = make_local_path(error_location)
    store_in_liquid(entry)
    self.next
  end
end


46
47
48
49
50
51
52
# File 'lib/locomotive/steam/middlewares/entry_submission.rb', line 46

def navigation_success(entry)
  if html?
    redirect_to success_location(entry_to_query_string(entry))
  elsif json?
    json_response(entry)
  end
end