Class: RSpec::ActionView::ERBTemplateEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-action_view/erb_template_inline.rb

Defined Under Namespace

Modules: Locals

Constant Summary collapse

ERBHandler =
::ActionView::Template::Handlers::ERB
EXCLUDE_IVARS =
%w{
  @_assertion_wrapped
  @_result
  @controller
  @layouts
  @locals
  @method_name
  @output_buffer
  @partials
  @rendered
  @request
  @routes
  @templates
  @test_passed
  @view
  @view_context_class
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeERBTemplateEngine

Returns a new instance of ERBTemplateEngine.



12
13
14
15
# File 'lib/rspec-action_view/erb_template_inline.rb', line 12

def initialize
  @output_buffer = ActiveSupport::SafeBuffer.new
  @rendered = ''
end

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer.



10
11
12
# File 'lib/rspec-action_view/erb_template_inline.rb', line 10

def output_buffer
  @output_buffer
end

#renderedObject

Returns the value of attribute rendered.



10
11
12
# File 'lib/rspec-action_view/erb_template_inline.rb', line 10

def rendered
  @rendered
end

Instance Method Details

#_assignsObject



72
73
74
75
76
77
78
# File 'lib/rspec-action_view/erb_template_inline.rb', line 72

def _assigns
  _instance_variables.inject({}) do |hash, var|
    name = var[1..-1].to_sym
    hash[name] = instance_variable_get(var)
    hash
  end
end

#_get_viewObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec-action_view/erb_template_inline.rb', line 32

def _get_view
  view = ::ActionView::Base.new                                  

  # ???
  # view.singleton_class.send :include, _helpers

  view.extend(Locals)
  view.locals = self.locals
  view.send :"output_buffer=", self.output_buffer
  view
end

#_instance_variablesObject



68
69
70
# File 'lib/rspec-action_view/erb_template_inline.rb', line 68

def _instance_variables
  instance_variables.map(&:to_s) - EXCLUDE_IVARS
end

#localsObject



28
29
30
# File 'lib/rspec-action_view/erb_template_inline.rb', line 28

def locals
  @locals ||= {}
end

#render(options = {}, local_assigns = {}, &block) ⇒ Object



17
18
19
20
21
22
# File 'lib/rspec-action_view/erb_template_inline.rb', line 17

def render(options = {}, local_assigns = {}, &block)
  view.assign(_assigns)
  output = view.render(options, local_assigns, &block)
  @rendered << output
  output
end

#run_template(options = {}, local_assigns = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
# File 'lib/rspec-action_view/erb_template_inline.rb', line 86

def run_template options = {}, local_assigns = {}, &block

  raise ArgumentError, "Must take template as a block argument" if !block        
  options.merge!(:inline => block.call)
  render options, local_assigns
end

#run_template_locals(locals = {}, local_assigns = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
84
# File 'lib/rspec-action_view/erb_template_inline.rb', line 80

def run_template_locals locals = {}, local_assigns = {}, &block
  raise ArgumentError, "Must take template as a block argument" if !block
  options = {:inline => block.call}.merge(:locals => locals)
  render options, local_assigns
end

#viewObject Also known as: _view



44
45
46
# File 'lib/rspec-action_view/erb_template_inline.rb', line 44

def view
  @view ||= _get_view
end