-
Notifications
You must be signed in to change notification settings - Fork 16
/
entries_controller.rb
88 lines (70 loc) · 1.56 KB
/
entries_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
class Admin::EntriesController < Admin::AdminController
layout 'admin'
respond_to :json, :html
def index
@page = Page.find params[:page_id]
respond_to do |format|
format.html do
@columns = entry_presenter_class.columns
@options = {
sort_field: sort_field,
sort_direction: sort_direction
}
end
format.json do
render json: fetch_entries.as_json
end
end
end
private
def fetch_entries
presented paginated searched sorted raw_entries
end
def presented entries
entries.dup.tap do |entries|
entries.each_with_index do |entry, index|
entries[index] = entry_presenter_class.new entry
end
end
end
def paginated entries
if sort_field != :position
params[:per_page] = 50 unless params.include?(:per_page)
params[:per_page] = params[:per_page].to_i
entries.paginate params
else
entries.paginate per_page: entry_class.count
end
end
def searched entries
if params[:search].present?
entries.text_search params[:search]
else
entries
end
end
def sorted entries
entries.send sort_direction, sort_field
end
def raw_entries
entry_class.entries
end
def sort_direction
set_slice.sort_direction
end
def sort_field
set_slice.sort_field
end
def entry_class
entry_type.to_s.camelize.constantize
end
def entry_type
@page.entry_types.first
end
def set_slice
@page.sets.first
end
def entry_presenter_class
presenter_class entry_class
end
end