0
votes

I want to monkey-patch the rspec-rails generator to generates a _form.html_spec.rb template. I know how do that, but not where I should put this patch!

What I've done:

# rspec_generator_patch.rb
module Rspec
  module Generators
    class ScaffoldGenerator < Base
      def generate_views_specs_with_form *args, &block
        generate_views_specs_without_form
        copy_view :_form
      end
      alias_method_chain :generate_views_specs, :form
    end
  end
end

Where I put this file? Thank you.

EDIT:

Ok, I think problem is almost solved. Instead of monkey-patch, I've inherited the specific generators and edited the method. There's the solution:

# lib/generators/rspec_modded/scaffold/scaffold_generator.rb
require 'generators/rspec/scaffold/scaffold_generator.rb'
module RspecModded
  module Generators
    class ScaffoldGenerator < Rspec::Generators::ScaffoldGenerator
      def generate_view_specs
        super
        copy_view :_form
      end
    end
  end
end

If I do rails g rspec_modded:scaffold is in list and actually work if called manually (rails g rspec_modded:scaffold test).

# config/application.rb
# ...
config.generators do |g|
  g.test_framework :rspec_modded, :fixture => false, fixture_replacement: nil
  g.fallbacks[:rspec_modded] = :rspec
end

For what I know, every hook_for :test_framework should call rspec_modded generator and rspec should manage the rest (fallbacks). But it doesn't work: for some reason the unit_test generator kick in! What the matter? I really don't understand...

1

1 Answers

0
votes

You can put your monkey patch anywhere, as long as it gets loaded. With rails, you'd usually put it in the /lib folder.