-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
features_controller.rb
90 lines (84 loc) · 2.48 KB
/
features_controller.rb
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
class FeaturesController < ApplicationController
before_action :require_user, except: [:embed]
def index
@features = Node.where(type: 'feature')
.paginate(page: params[:page])
end
def embed
@node = Node.find_by(title: params[:id])
render layout: false
end
def new
unless current_user.admin?
flash[:warning] = 'Only admins may edit features.'
redirect_to '/features'
end
end
def edit
if !current_user.admin?
flash[:warning] = 'Only admins may edit features.'
redirect_to '/features'
else
@node = Node.find params[:id]
end
end
def create
if !current_user.admin?
flash[:warning] = 'Only admins may edit features.'
redirect_to '/features?_=' + Time.now.to_i.to_s
else
@node = Node.new(uid: current_user.id,
title: params[:title],
type: 'feature')
if @node.valid?
saved = true
@revision = false
ActiveRecord::Base.transaction do
@node.save!
@revision = @node.new_revision(uid: current_user.id,
title: params[:title],
body: params[:body])
if @revision.valid?
@revision.save!
@node.vid = @revision.vid
@node.save!
else
saved = false
@node.destroy
end
end
end
if saved
flash[:notice] = 'Feature saved.'
redirect_to '/features?_=' + Time.now.to_i.to_s
else
render template: 'features/new'
end
end
end
def update
if !current_user.admin?
flash[:warning] = 'Only admins may edit features.'
redirect_to '/features?_=' + Time.now.to_i.to_s
else
@node = Node.find(params[:id])
@revision = @node.new_revision(uid: current_user.uid)
@revision.title = params[:title] || @node.latest.title
@revision.body = params[:body] if params[:body]
if @revision.valid?
ActiveRecord::Base.transaction do
@revision.save
@node.vid = @revision.vid
@node.title = @revision.title
@node.save
end
ActionController::Base.new.expire_fragment("feature_#{params[:title]}")
flash[:notice] = 'Edits saved and cache cleared.'
redirect_to '/features?_=' + Time.now.to_i.to_s
else
flash[:error] = 'Your edit could not be saved.'
render action: 'edit'
end
end
end
end