Class: Eksa::CmsController
Instance Attribute Summary
Attributes inherited from Controller
#flash, #redirect_url, #request, #status
Instance Method Summary
collapse
Methods inherited from Controller
#asset_path, #current_user, #initialize, #javascript_tag, #params, #redirect_to, #render, #render_internal, #require_auth, #session, #stylesheet_tag
Instance Method Details
#delete_post ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/eksa/cms_controller.rb', line 96
def delete_post
return unless require_auth
slug = params['slug']
post_path = File.join(Eksa::MarkdownPost::POSTS_DIR, "#{slug}.md")
if File.exist?(post_path)
File.delete(post_path)
redirect_to "/cms", notice: "Postingan berhasil dihapus secara permanen."
else
redirect_to "/cms", notice: "Error: Postingan tidak ditemukan."
end
end
|
#edit ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/eksa/cms_controller.rb', line 10
def edit
return unless require_auth
slug = params['slug']
@post = Eksa::MarkdownPost.find(slug)
if @post
render_internal 'cms/edit'
else
redirect_to "/cms", notice: "Error: Postingan tidak ditemukan."
end
end
|
#index ⇒ Object
3
4
5
6
7
8
|
# File 'lib/eksa/cms_controller.rb', line 3
def index
return unless require_auth
@posts = Eksa::MarkdownPost.all(include_unpublished: true)
render_internal 'cms/index'
end
|
#toggle_status ⇒ Object
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
|
# File 'lib/eksa/cms_controller.rb', line 67
def toggle_status
return unless require_auth
slug = params['slug']
post_path = File.join(Eksa::MarkdownPost::POSTS_DIR, "#{slug}.md")
if File.exist?(post_path)
content = File.read(post_path, encoding: 'utf-8')
if content.match?(/^published:\s*false/m)
new_content = content.sub(/^published:\s*false/m, "published: true")
status = "diaktifkan"
elsif content.match?(/^published:\s*true/m)
new_content = content.sub(/^published:\s*true/m, "published: false")
status = "dinonaktifkan"
else
new_content = content.sub(/\A(---\r?\n)/) { "#{$1}published: false\n" }
status = "dinonaktifkan"
end
File.write(post_path, new_content)
redirect_to "/cms", notice: "Postingan #{File.basename(post_path)} berhasil #{status}."
else
redirect_to "/cms", notice: "Error: Postingan tidak ditemukan."
end
end
|
#update_post ⇒ Object
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
|
# File 'lib/eksa/cms_controller.rb', line 23
def update_post
return unless require_auth
slug = params['slug']
post_path = File.join(Eksa::MarkdownPost::POSTS_DIR, "#{slug}.md")
unless File.exist?(post_path)
return redirect_to "/cms", notice: "Error: Postingan tidak ditemukan."
end
title = params['title']
category = params['category']
author = params['author']
image = params['image']
content = params['content']
original_content = File.read(post_path, encoding: 'utf-8')
metadata = {}
if original_content =~ /\A(---\s*\r?\n.*?\r?\n)^(---\s*\r?\n?)/m
metadata = YAML.safe_load($1, permitted_classes: [Time]) || {}
end
metadata['title'] = title
metadata['category'] = category
metadata['author'] = author
metadata['image'] = image unless image.nil? || image.strip.empty?
new_yaml = YAML.dump(metadata)
new_file_content = "#{new_yaml}---\n\n#{content}"
File.write(post_path, new_file_content, encoding: 'utf-8')
redirect_to "/cms", notice: "Postingan '#{title}' berhasil diperbarui."
end
|