-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathcontent_entries_controller.rb
More file actions
124 lines (99 loc) · 3.42 KB
/
content_entries_controller.rb
File metadata and controls
124 lines (99 loc) · 3.42 KB
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
module Locomotive
class ContentEntriesController < BaseController
account_required & within_site
localized
before_filter :back_to_default_site_locale, only: [:new, :create]
before_filter :load_content_type
before_filter :load_content_entry, only: [:show, :show_in_form, :clone, :edit, :update, :destroy]
before_filter :store_location, only: [:edit, :update]
respond_to :json, only: [:index, :sort]
respond_to :csv, only: [:export]
helper 'Locomotive::CustomFields'
def index
authorize ContentEntry
@content_entries = service.all(list_params)
respond_with @content_entries
end
def clone
authorize @content_entry
@clone_content_entry = service.entry_clone(@content_entry)
redirect_to edit_content_entry_path(current_site, @content_type.slug, @clone_content_entry.id)
end
def export
authorize ContentEntry, :index?
@content_entries = @content_type.ordered_entries
respond_with @content_entries, {
filename: @content_type.slug,
col_sep: ';',
content_type: @content_type,
host: request.host_with_port
}
end
def show_in_form
authorize @content_entry, :show?
_content_type = current_site.content_types.where(slug: params[:parent_slug]).first
@field = _content_type.entries_custom_fields.find(params[:field_id])
render partial: 'entry', locals: {
slug: params[:slug],
item: @content_entry,
field: @field
}
end
def new
@content_entry = @content_type.entries.build(params[:content_entry] ? content_entry_params : {})
respond_with @content_entry
end
def create
authorize ContentEntry
@content_entry = service.create(content_entry_params)
respond_with @content_entry, location: -> { location_after_persisting }
end
def edit
authorize @content_entry
respond_with @content_entry
end
def update
authorize @content_entry
service.update(@content_entry, content_entry_params)
respond_with @content_entry, location: -> { location_after_persisting }
end
def sort
authorize ContentEntry, :update?
service.sort(params[:entries])
respond_with @content_type, location: content_entries_path(current_site, @content_type.slug)
end
def destroy
authorize @content_entry
service.destroy(@content_entry)
respond_with @content_entry, location: content_entries_path(current_site, @content_type.slug)
end
private
def load_content_type
@content_type ||= current_site.content_types.where(slug: params[:slug]).first!
end
def load_content_entry
@content_entry = @content_type.entries.find(params[:id])
end
def service
@service ||= Locomotive::ContentEntryService.new(load_content_type, current_locomotive_account)
end
def list_params
if @content_type.order_manually?
params.slice(:q, :where).merge(no_pagination: true)
else
params.slice(:page, :per_page, :q, :where)
end
end
def content_entry_params
params.require(:content_entry).permit(service.permitted_attributes)
end
def location_after_persisting
default = edit_content_entry_path(current_site, @content_type.slug, @content_entry)
if params[:_location].present?
last_saved_location!(default)
else
default
end
end
end
end