Module: Jun::ActionController::Rendering

Included in:
Base
Defined in:
lib/jun/action_controller/rendering.rb

Instance Method Summary collapse

Instance Method Details

#render(options, extra_options = {}) ⇒ Object



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
# File 'lib/jun/action_controller/rendering.rb', line 11

def render(options, extra_options = {})
  return if response_rendered?

  template_name = nil
  if options.is_a?(Symbol) || options.is_a?(String)
    template_name = options
    options = extra_options
  else
    template_name = options[:template]
  end

  options[:layout] = true unless options.key?(:layout)

  if content_type = options[:content_type]
    response.content_type = content_type.to_s
  end

  if status = options[:status]
    response.status = status
  end

  if template_name
    filepath = views_path.join("#{template_name}.html.erb")
    template = Tilt::ERBTemplate.new(filepath)
    context = Jun::ActionView::Base.new(self)
    body = template.render(context, options[:locals])

    if options[:layout]
      layout_name = options[:layout].is_a?(String) ? options[:layout] : "application"
      layout_filepath = layouts_path.join("#{layout_name}.html.erb")
      layout_template = Tilt::ERBTemplate.new(layout_filepath)

      body = layout_template.render(context) { body }
    end

    response.write(body)
    response.content_type ||= "text/html"
  elsif options[:text]
    response.write(options[:text])
    response.content_type ||= "text/plain"
  elsif options[:json]
    json = options[:json].is_a?(String) ? options[:json] : JSON.generate(options[:json])
    response.write(json)
    response.content_type ||= "application/json"
  elsif options[:nothing]
    response.write("")
    response.content_type ||= "text/plain"
  end

  @_response_rendered = true
end

#view_assignsObject



63
64
65
66
67
68
69
70
# File 'lib/jun/action_controller/rendering.rb', line 63

def view_assigns
  reserved_variables = %i[@request @response @_response_rendered]
  variables = instance_variables - reserved_variables

  variables.reduce({}) do |object, name|
    object.merge(name[1..-1] => instance_variable_get(name))
  end
end