Class: Eksa::Controller
- Inherits:
-
Object
show all
- Defined in:
- lib/eksa/controller.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(request) ⇒ Controller
8
9
10
11
12
|
# File 'lib/eksa/controller.rb', line 8
def initialize(request)
@request = request
@status = 200
@flash = {}
end
|
Instance Attribute Details
#flash ⇒ Object
Returns the value of attribute flash.
6
7
8
|
# File 'lib/eksa/controller.rb', line 6
def flash
@flash
end
|
#redirect_url ⇒ Object
Returns the value of attribute redirect_url.
6
7
8
|
# File 'lib/eksa/controller.rb', line 6
def redirect_url
@redirect_url
end
|
#request ⇒ Object
Returns the value of attribute request.
5
6
7
|
# File 'lib/eksa/controller.rb', line 5
def request
@request
end
|
#status ⇒ Object
Returns the value of attribute status.
6
7
8
|
# File 'lib/eksa/controller.rb', line 6
def status
@status
end
|
Instance Method Details
#asset_path(path) ⇒ Object
43
44
45
|
# File 'lib/eksa/controller.rb', line 43
def asset_path(path)
path.start_with?('/') ? path : "/#{path}"
end
|
#current_user ⇒ Object
22
23
24
25
|
# File 'lib/eksa/controller.rb', line 22
def current_user
return @current_user if defined?(@current_user)
@current_user = session['user_id'] ? Eksa::User.find(session['user_id']) : nil
end
|
#javascript_tag(filename) ⇒ Object
39
40
41
|
# File 'lib/eksa/controller.rb', line 39
def javascript_tag(filename)
"<script src='/js/#{filename}.js'></script>"
end
|
#params ⇒ Object
14
15
16
|
# File 'lib/eksa/controller.rb', line 14
def params
@request.params
end
|
#redirect_to(url, notice: nil) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/eksa/controller.rb', line 47
def redirect_to(url, notice: nil)
@status = 302
@redirect_url = url
@flash[:notice] = notice if notice
nil
end
|
#render(template_name, variables = {}) ⇒ Object
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
|
# File 'lib/eksa/controller.rb', line 54
def render(template_name, variables = {})
variables.each { |k, v| instance_variable_set("@#{k}", v) }
content_path = File.expand_path("./app/views/#{template_name}.html.erb")
internal_content_path = File.expand_path("../../eksa/views/#{template_name}.html.erb", __FILE__)
layout_path = File.expand_path("./app/views/layout.html.erb")
internal_layout_path = File.expand_path("../../eksa/views/layout.html.erb", __FILE__)
actual_content_path = File.exist?(content_path) ? content_path : internal_content_path
actual_layout_path = File.exist?(layout_path) ? layout_path : internal_layout_path
if File.exist?(actual_content_path)
@content = ERB.new(File.read(actual_content_path)).result(binding)
if File.exist?(actual_layout_path)
ERB.new(File.read(actual_layout_path)).result(binding)
else
@content
end
else
"<div class='glass' style='padding: 2rem; border-radius: 1rem; color: #ff5555; background: rgba(255,0,0,0.1); backdrop-filter: blur(10px);'>
<h2 style='margin-top:0;'>⚠️ View Error</h2>
<p>Template <strong>#{template_name}</strong> tidak ditemukan di app/views atau internal eksa/views.</p>
</div>"
end
end
|
#render_internal(template_name, variables = {}) ⇒ Object
81
82
83
|
# File 'lib/eksa/controller.rb', line 81
def render_internal(template_name, variables = {})
render(template_name, variables)
end
|
#require_auth ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/eksa/controller.rb', line 27
def require_auth
unless current_user
redirect_to "/auth/login", notice: "Anda harus login untuk mengakses halaman ini."
return false
end
true
end
|
#session ⇒ Object
18
19
20
|
# File 'lib/eksa/controller.rb', line 18
def session
@request.session
end
|
#stylesheet_tag(filename) ⇒ Object
35
36
37
|
# File 'lib/eksa/controller.rb', line 35
def stylesheet_tag(filename)
"<link rel='stylesheet' href='/css/#{filename}.css'>"
end
|