1
2
3
4
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
class PageTranslationsController < ApplicationController
layout 'admin'
before_action :login_required
before_action :find_node
before_action :find_locale, :only => [:show, :edit, :update, :autosave, :destroy]
def show
@page = @node.draft || @node.head
@translation = @page.translations.find_by(:locale => @locale)
@default_translation = @page.translations.find_by(:locale => I18n.default_locale)
end
def edit
@node.lock_for_editing!(current_user)
@page = @node.draft || @node.head
@translation = @page.translations.find_by(:locale => @locale)
rescue LockedByAnotherUser => e
flash[:error] = e.message
redirect_to node_path(@node)
end
def update
Globalize.with_locale(@locale) { @node.autosave!(translation_params, current_user) }
@node.save_draft!(current_user)
flash[:notice] = "#{@locale.upcase} translation saved. Publish the draft to make it live."
if params[:commit] == "Save + Unlock + Exit"
@node.unlock!
redirect_to node_path(@node)
else
redirect_to edit_node_translation_path(@node, @locale)
end
rescue LockedByAnotherUser => e
flash[:error] = e.message
redirect_to node_path(@node)
end
def autosave
Globalize.with_locale(@locale) { @node.autosave!(translation_params, current_user) }
head :ok
rescue LockedByAnotherUser => e
render plain: e.message, status: :locked
rescue ActiveRecord::RecordInvalid => e
render plain: e.message, status: :unprocessable_entity
rescue StandardError => e
render plain: "Autosave failed", status: :internal_server_error
end
def destroy
base = @node.draft || @node.head
unless base && base.translated_locales.include?(@locale)
flash[:error] = "No #{@locale.to_s.upcase} translation exists to remove."
return redirect_to node_path(@node)
end
if (base.translated_locales - [@locale]).empty?
flash[:error] = "Can't remove the only remaining translation."
return redirect_to node_path(@node)
end
draft = ensure_editable_draft
draft.translations.where(:locale => @locale).delete_all
draft.reload
flash[:notice] = "#{@locale.upcase} translation removed from the draft. Publish to make this permanent."
redirect_to node_path(@node)
rescue LockedByAnotherUser => e
flash[:error] = e.message
redirect_to node_path(@node)
end
private
def find_node
@node = Node.find(params[:node_id])
end
# Every remaining action's :translation_locale is already
# router-constrained to non-default locales, so this should never
# actually fire in practice -- it's a deliberate second check, kept
# as a drift detector in case the route constraint and this list
# (both driven by Page.non_default_locales) ever disagree.
def find_locale
candidate = params[:translation_locale].to_s.to_sym
unless Page.non_default_locales.include?(candidate)
raise ActionController::RoutingError, "Unknown or default locale"
end
@locale = candidate
end
# Never mutates head directly. Locks first (same guard as the
# primary editor), then reuses an existing draft or creates one --
# deliberately not going through the autosave buffer: this is a
# plain, explicit Save, not a live-typing surface.
def ensure_editable_draft
@node.lock_for_editing!(current_user)
@node.draft || @node.create_new_draft(current_user)
end
def translation_params
params.fetch(:page, {}).permit(:title, :abstract, :body)
end
end
|