Class: I18nFeedback::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_feedback/middleware.rb

Overview

Runs on every request to (1) flip key-marking on for the duration of the request when a proofreader has the tool switched on, and (2) inject the widget into HTML responses so the host needs no layout changes. Both are strictly gated by I18nFeedback.available?, so nothing happens in a disabled environment.

Constant Summary collapse

'i18n_feedback'

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



13
14
15
# File 'lib/i18n_feedback/middleware.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n_feedback/middleware.rb', line 17

def call(env)
  request = Rack::Request.new(env)
  available = I18nFeedback.available?(request)

  # A "?i18n_feedback=true|false" in the URL is a command: it overrides the
  # cookie for this request and is remembered so the rest of the app stays in
  # (or out of) suggest mode without the parameter.
  override = available ? toggle_override(request) : nil
  marking = available && (override.nil? ? cookie_on?(request) : override)
  Marking.enabled = marking

  status, headers, body = @app.call(env)

  headers = persist_choice(headers, override) if available && !override.nil?

  if available && I18nFeedback.config.auto_inject && html?(headers) && !request.xhr?
    status, headers, body = inject(status, headers, body, marking)
  end

  [status, headers, body]
ensure
  Marking.enabled = false
end