Sha256: 6fe50b40ca16277a2a37f346b745908e7c51b41d891ccf5ff4db305161df0e97

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module MagicLamp
  class FixtureCreator
    include Callbacks

    attr_accessor :render_arguments

    def generate_template(controller_class, &block)
      execute_before_each_callback
      controller = new_controller(controller_class, &block)
      munged_arguments = munge_arguments(render_arguments)
      rendered = controller.render_to_string(*munged_arguments)
      execute_after_each_callback
      rendered
    end

    def new_controller(controller_class, &block)
      controller = controller_class.new
      controller.request = ActionDispatch::TestRequest.new
      redefine_render(controller)
      controller.instance_eval(&block)
      controller
    end

    def munge_arguments(arguments)
      first_arg, second_arg = arguments

      if first_arg.is_a?(Hash)
        first_arg[:layout] ||= false
      elsif second_arg.is_a?(Hash)
        second_arg[:layout] ||= false
      else
        arguments << { layout: false }
      end
      arguments
    end

    private

    def redefine_render(controller)
      fixture_creator = self
      controller.singleton_class.send(:define_method, :render) do |*args|
        fixture_creator.render_arguments = args
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magic_lamp-1.0.0 lib/magic_lamp/fixture_creator.rb