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

Inherits:
ThreadSafe
  • Object
show all
Includes:
Helpers
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'

Instance Attribute Summary

Attributes inherited from ThreadSafe

#env

Instance Method Summary collapse

Methods included from Helpers

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

Methods inherited from ThreadSafe

#call, #default_locale, #liquid_assigns, #live_editing?, #locale, #next, #page, #params, #path, #request, #services, #site

Instance Method Details

#_callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/locomotive/steam/middlewares/entry_submission.rb', line 16

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)



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

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


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

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


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

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


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

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