Class: Eksa::CmsController

Inherits:
Controller show all
Defined in:
lib/eksa/cms_controller.rb

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

Constructor Details

This class inherits a constructor from Eksa::Controller

Instance Method Details

#delete_postObject



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

#editObject



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

#indexObject



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_statusObject



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')
    
    # Simple string replacement for published status in front matter
    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
      # If no published tag exists, it's implicitly true, so we set it to false
      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_postObject



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

  # Existing metadata fetching to preserve unmodified keys like 'date' if not handling them
  # We parse form params
  title = params['title']
  category = params['category']
  author = params['author']
  image = params['image']
  content = params['content']
  
  # We'll re-read the original to preserve keys we aren't allowing to edit (like published status and date)
  original_content = File.read(post_path, encoding: 'utf-8')
   = {}
  
  if original_content =~ /\A(---\s*\r?\n.*?\r?\n)^(---\s*\r?\n?)/m
     = YAML.safe_load($1, permitted_classes: [Time]) || {}
  end
  
  # Update metadata with form values
  ['title'] = title
  ['category'] = category
  ['author'] = author
  ['image'] = image unless image.nil? || image.strip.empty?
  
  # Dump new YAML
  new_yaml = YAML.dump()
  
  # Join with content
  new_file_content = "#{new_yaml}---\n\n#{content}"
  
  # Write changes
  File.write(post_path, new_file_content, encoding: 'utf-8')
  
  redirect_to "/cms", notice: "Postingan '#{title}' berhasil diperbarui."
end