Class: Eksa::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/eksa/controller.rb

Direct Known Subclasses

PagesController, SeoController

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

Returns a new instance of 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

#flashObject

Returns the value of attribute flash.



6
7
8
# File 'lib/eksa/controller.rb', line 6

def flash
  @flash
end

#redirect_urlObject

Returns the value of attribute redirect_url.



6
7
8
# File 'lib/eksa/controller.rb', line 6

def redirect_url
  @redirect_url
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/eksa/controller.rb', line 5

def request
  @request
end

#statusObject

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



26
27
28
# File 'lib/eksa/controller.rb', line 26

def asset_path(path)
  path.start_with?('/') ? path : "/#{path}"
end

#javascript_tag(filename) ⇒ Object



22
23
24
# File 'lib/eksa/controller.rb', line 22

def javascript_tag(filename)
  "<script src='/js/#{filename}.js'></script>"
end

#paramsObject



14
15
16
# File 'lib/eksa/controller.rb', line 14

def params
  @request.params
end

#redirect_to(url, notice: nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/eksa/controller.rb', line 30

def redirect_to(url, notice: nil)
  @status = 302
  @redirect_url = url
  @flash[:notice] = notice if notice
  nil
end

#render(template_name, variables = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eksa/controller.rb', line 37

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")
  layout_path  = File.expand_path("./app/views/layout.html.erb")

  if File.exist?(content_path)
    @content = ERB.new(File.read(content_path)).result(binding)
    if File.exist?(layout_path)
      ERB.new(File.read(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:</p>
      <code style='display:block; background:rgba(0,0,0,0.2); padding:0.5rem; border-radius:0.5rem;'>#{content_path}</code>
    </div>"
  end
end

#stylesheet_tag(filename) ⇒ Object



18
19
20
# File 'lib/eksa/controller.rb', line 18

def stylesheet_tag(filename)
  "<link rel='stylesheet' href='/css/#{filename}.css'>"
end