Class: ActiveAdmin::Renderer

Inherits:
Object
  • Object
show all
Includes:
ViewHelpers::RendererHelper, Arbre::HTML
Defined in:
lib/active_admin/renderer.rb

Direct Known Subclasses

Views::HeaderRenderer, Views::TabsRenderer

Constant Summary

Constants included from Arbre::HTML

Arbre::HTML::AUTO_BUILD_ELEMENTS, Arbre::HTML::HTML5_ELEMENTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arbre::HTML

#current_dom_context

Methods included from ViewHelpers::RendererHelper

#render

Constructor Details

#initialize(view_or_renderer) ⇒ Renderer

Returns a new instance of Renderer.



12
13
14
15
16
17
18
19
20
# File 'lib/active_admin/renderer.rb', line 12

def initialize(view_or_renderer)
  @view = view_or_renderer.is_a?(Renderer) ? view_or_renderer.view : view_or_renderer

  if view.respond_to?(:assigns)
    @assigns = view.assigns.each { |key, value| instance_variable_set("@#{key}", value) }
  else
    @assigns = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/active_admin/renderer.rb', line 22

def method_missing(name,*args, &block)
  if view.respond_to?(name)
    view.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#assignsObject

Returns the value of attribute assigns.



7
8
9
# File 'lib/active_admin/renderer.rb', line 7

def assigns
  @assigns
end

#viewObject Also known as: helpers

Returns the value of attribute view.



7
8
9
# File 'lib/active_admin/renderer.rb', line 7

def view
  @view
end

Instance Method Details

#call_method_or_proc_on(obj, symbol_or_proc) ⇒ Object (protected)

Many times throughout the views we want to either call a method on an object or instance_exec a proc passing in the object as the first parameter. This method takes care of this functionality.

call_method_or_proc_on(@my_obj, :size) same as @my_obj.size

OR

proc = Proc.new{|s| s.size }
call_method_or_proc_on(@my_obj, proc)


77
78
79
80
81
82
83
84
# File 'lib/active_admin/renderer.rb', line 77

def call_method_or_proc_on(obj, symbol_or_proc)
  case symbol_or_proc
  when Symbol, String
    obj.send(symbol_or_proc.to_sym)
  when Proc
    instance_exec(obj, &symbol_or_proc)
  end
end

#haml(template) ⇒ Object



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

def haml(template)
  begin
    require 'haml' unless defined?(Haml)
  rescue LoadError
    raise LoadError, "Please install the HAML gem to use the HAML method with ActiveAdmin"
  end

  # Find the first non whitespace character in the template
  indent = template.index(/\S/)

  # Remove the indent if its greater than 0
  if indent > 0
    template = template.split("\n").collect do |line|
      line[indent..-1]
    end.join("\n")
  end

  # Render it baby
  Haml::Engine.new(template).render(self)
end

#set_ivar_on_view(name, value) ⇒ Object (protected)

Although we make a copy of all the instance variables on the way in, it doesn’t mean that we can set new instance variables that are stored in the context of the view. This method allows you to do that. It can be useful when trying to share variables with a layout.



64
65
66
# File 'lib/active_admin/renderer.rb', line 64

def set_ivar_on_view(name, value)
  view.instance_variable_set(name, value)
end

#to_html(*args) ⇒ Object



30
31
# File 'lib/active_admin/renderer.rb', line 30

def to_html(*args)
end

#to_s(*args) ⇒ Object



33
34
35
# File 'lib/active_admin/renderer.rb', line 33

def to_s(*args)
  to_html(*args)
end