Class: Markdowndocs::PreferencesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/markdowndocs/preferences_controller.rb

Instance Method Summary collapse

Instance Method Details

#updateObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/markdowndocs/preferences_controller.rb', line 5

def update
  mode = params[:mode].to_s

  unless Markdowndocs.config.modes.include?(mode)
    head :unprocessable_entity
    return
  end

  saver = Markdowndocs.config.user_mode_saver
  if saver.respond_to?(:call)
    begin
      saver.call(self, mode)
    rescue => e
      Rails.logger.warn("Markdowndocs: user_mode_saver failed: #{e.message}")
    end
  end

  cookies[:markdowndocs_mode] = {
    value: mode,
    expires: 1.year.from_now,
    httponly: true
  }

  current_path = params[:current_path]
  target = smart_nav_target(mode, current_path)
  mode_name = I18n.t("markdowndocs.modes.#{mode}", default: mode.titleize)

  flash[:notice] = if redirected_to_index_from_doc?(target, current_path)
    I18n.t(
      "markdowndocs.no_counterpart_announcement",
      mode: mode_name,
      default: "No %{mode} version of this page — showing the %{mode} documentation."
    )
  else
    I18n.t(
      "markdowndocs.mode_announcement",
      mode: mode_name,
      default: "Now viewing %{mode}."
    )
  end

  redirect_to(target, status: :see_other)
end