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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
class NodesController < ApplicationController
# Private
layout 'admin'
before_action :login_required
before_action :find_node, :only => [
:show,
:edit,
:update,
:destroy,
:publish,
:unlock,
:autosave
]
def index
@nodes = Node.root.descendants.includes(:head, :draft)
.order('id DESC')
.paginate(:page => params[:page], :per_page => 25)
end
def new
@node = Node.new node_params
@selected_kind = CccConventions::NODE_KINDS.key?(params[:kind]) ? params[:kind] : "generic"
if params.has_key?(:parent_id)
@parent_id = params[:parent_id]
@parent_name = Node.find(@parent_id).title
end
end
def create
params[:title] ||= ""
@node = Node.new
@node.parent_id = find_parent
@node.slug = slug_for(params[:title])
config = CccConventions::NODE_KINDS[params[:kind]]
if @node.save
@node.draft.update(:title => params[:title])
Array(config && config[:tags]).each { |t| @node.draft.tag_list.add(t) }
@node.draft.save!
@node.update!(default_template_name: config[:template]) if config && config[:template]
redirect_to(edit_node_path(@node))
else
render :new
end
end
def show
node = Node.find(params[:id])
node.wipe_draft!
@page = node.draft || node.head
end
def edit
begin
@draft = @node.find_or_create_draft( current_user )
rescue LockedByAnotherUser => e
flash[:error] = e.message
if request.referer
redirect_to request.referer || node_path(@node)
else
redirect_to node_path(@node)
end
end
end
def update
@node.update(node_params)
@node.autosave!( page_params.merge(:tag_list => params[:tag_list]), current_user )
@node.save_draft!(current_user)
flash[:notice] = "Draft has been saved: #{Time.now}"
if params[:commit] == "Save + Unlock + Exit"
@node.unlock!
redirect_to node_path(@node)
else
redirect_to edit_node_path(@node)
end
rescue LockedByAnotherUser => e
flash[:error] = e.message
redirect_to node_path(@node)
rescue ActiveRecord::RecordInvalid
@page = @node.autosave || @node.draft || @node.head
render :action => :edit
end
def autosave
@node.update(node_params)
@node.autosave!( page_params.merge(:tag_list => params[:tag_list]), 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
@node.destroy
end
def publish
@node.publish_draft!
flash[:notice] = "Draft has been published"
redirect_to node_path(@node)
end
def unlock
if @node.unlock!
flash[:notice] = "Node unlocked"
else
flash[:notice] = "Already unlocked"
end
redirect_to node_path(@node)
end
def generate_shared_preview
@node = Node.find(params[:id])
if @node.draft
@node.draft.ensure_preview_token!
flash[:notice] = "Shareable preview link created - see below."
else
flash[:notice] = "Create or edit a draft first - shared preview links are only available for pages with an active draft."
end
redirect_to node_path(@node)
end
def revoke_shared_preview
@node = Node.find(params[:id])
@node.draft.revoke_preview_token! if @node.draft
flash[:notice] = "Shareable preview link revoked."
redirect_to node_path(@node)
end
def parameterize_preview
render plain: slug_for(params[:title])
end
private
def slug_for(title)
title.to_s.parameterize
end
def node_params
params.fetch(:node, {}).permit(:slug, :parent_id, :staged_slug, :staged_parent_id)
end
def page_params
params.fetch(:page, {}).permit(:title, :abstract, :body, :template_name, :published_at, :user_id)
end
def find_node
@node = Node.find(params[:id])
end
def find_parent
case params[:kind]
when "generic"
if params[:parent_id] && Node.find(params[:parent_id])
params[:parent_id]
else
nil
end
else
config = CccConventions::NODE_KINDS[params[:kind]]
config && config[:parent] ? config[:parent].call.id : nil
end
end
end
|