page_templates_controller.rb
3.16 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
module Kanjai
class Admin::PageTemplatesController < AdminController
before_action :set_s3_direct_post, only: [:new, :update, :edit, :update]
after_action :unzip_content, only: [:create, :update]
def index
@collection = PageTemplate.order(:title)
end
def new
@page_template = PageTemplate.new
end
def create
@page_template = PageTemplate.new(permitted_params[:page_template])
if @page_template.save
render json: {status: 'ok'}
end
end
def edit
@page_template = PageTemplate.find(params[:id])
@url = @page_template.file_expiring_url
lang = params[:lang]
if @page_template.unzip
exist_markers = @page_template.page_content_markers.where(lang: lang).collect(&:marker)
@page_template.template_statics.where.not(marker: exist_markers).each do |item|
obj = @page_template.page_content_markers.create(
marker: item.marker,
marker_type: item.marker_type,
marker_name: item.marker_name,
lang: lang
)
end
@markers = @page_template.page_content_markers.where(lang: lang)
end
end
def update
@page_template = PageTemplate.find(params[:id])
@page_template.unzip = false
if @page_template.update(permitted_params[:page_template])
render json: {status: 'ok'}
end
end
def destroy
@page_template = PageTemplate.find(params[:id])
@page_template.destroy
render :json => {:status => 'ok'}.to_json
end
def marker
lang = params[:lang]
@page_template = PageTemplate.find(params[:id])
if @page_template.unzip
exist_markers = @page_template.page_content_markers.where(lang: lang).collect(&:marker)
@page_template.template_statics.where.not(marker: exist_markers).each do |item|
@page_template.page_content_markers.create(
marker: item.marker,
marker_type: item.marker_type,
marker_name: item.marker_name,
lang: lang
)
end
@markers = @page_template.page_content_markers.where(lang: lang)
else
flash[:notice] = "Template must be unzip before edit static element"
redirect_to action: :index
end
end
def update_marker
@page_template = PageTemplate.find(params[:id])
if @page_template.update(permitted_params[:page_template])
redirect_to :action => :index
else
redirect_to :action => :marker
end
end
private
def permitted_params
params.permit(:page_template => [:title, :attachment_file_name, :notice,
page_content_markers_attributes: [:id, :page_content_id, :marker, :text_value, :attachment_file_name]
])
end
def unzip_content
if @page_template.unzip == false
@page_template.unzip_content
end
end
def set_s3_direct_post
@s3_direct_post = S3_BUCKET.presigned_post(key: "tmp/templates/${filename}", success_action_status: '201', acl: 'private')
end
end
end