Sha256: d9a2cb5738729eeda98e9fc1116994c81b86887a700b2d5ce2976a4a07a90e6e
Contents?: true
Size: 1.62 KB
Versions: 5
Compression:
Stored size: 1.62 KB
Contents
module MagicLamp class FixtureCreator include Callbacks attr_accessor :render_arguments def generate_template(controller_class, extensions, &block) execute_before_each_callback controller = new_controller(controller_class, extensions, &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, extensions, &block) controller = controller_class.new redefine_view_context(controller, extensions) extensions.each { |extension| controller.extend(extension) } 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_view_context(controller, extensions) controller.singleton_class.send(:define_method, :view_context) do |*args, &block| view_context = super(*args, &block) extensions.each { |extension| view_context.extend(extension) } view_context end end 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
5 entries across 5 versions & 1 rubygems