This repository was archived by the owner on Jan 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathpages_controller.rb
121 lines (99 loc) · 3.2 KB
/
pages_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
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
class ApiV1::PagesController < ApiV1::APIController
before_filter :load_page, :only => [:show, :update, :reorder, :watch, :unwatch, :destroy]
def index
authorize! :show, @current_project||current_user
context = if @current_project
@current_project.pages.where(api_scope)
else
Page.joins(:project).where(:project_id => current_user.project_ids, :projects => {:archived => false}).where(api_scope)
end
@pages = context.except(:order).
where(['pages.is_private = ? OR (pages.is_private = ? AND watchers.user_id = ?)', false, true, current_user.id]).
joins("LEFT JOIN watchers ON (pages.id = watchers.watchable_id AND watchers.watchable_type = 'Page') AND watchers.user_id = #{current_user.id}").
where(api_range('pages')).
limit(api_limit).
order('pages.id DESC').
includes([:project, :user])
api_respond @pages, :include => :slots, :references => true
end
def create
authorize! :make_pages, @current_project
@page = @current_project.new_page(current_user,params)
if @page.save
handle_api_success(@page, :is_new => true)
else
handle_api_error(@page)
end
end
def show
authorize! :show, @page
api_respond @page, :references => true, :include => :slots
end
def update
authorize! :update, @page
@page.updating_user = current_user
if @page.update_attributes(params)
handle_api_success(@page)
else
handle_api_error(@page)
end
end
def reorder
authorize! :update, @page
order = params[:slots].collect { |id| id.to_i }
current = @page.slots.map { |slot| slot.id }
# Handle orphaned elements
orphans = (current - order).map { |o|
idx = current.index(o)
oid = idx == 0 ? -1 : current[idx-1]
[@page.slots[idx], oid]
}
# Insert orphans back into order list
orphans.each { |o| order.insert(o[1], (order.index(o[0]) || -1)+1) }
@page.slots.each do |slot|
slot.position = order.index(slot.id)
slot.save!
end
handle_api_success(@page)
end
def resort
authorize! :reorder_objects, @current_project
order = params[:pages].map(&:to_i)
@current_project.pages.each do |page|
page.suppress_activity = true
page.position = order.index(page.id)
page.save
end
handle_api_success(@page)
end
def destroy
authorize! :destroy, @page
@page.destroy
handle_api_success(@page)
end
def watch
authorize! :watch, @page
@page.add_watcher(current_user)
handle_api_success(@page)
end
def unwatch
@page.remove_watcher(current_user)
handle_api_success(@page)
end
protected
def load_page
@page = if @current_project
@current_project.pages.find_by_id(params[:id])
else
Page.where(:project_id => current_user.project_ids).find_by_id(params[:id])
end
api_error :not_found, :type => 'ObjectNotFound', :message => 'Page not found' unless @page
end
def api_scope
conditions = {}
unless params[:user_id].nil?
conditions[:user_id] = params[:user_id].to_i
end
conditions
end
end