How to write a rails generator that would override the default scaffold javascript file?

Viewed 842

Background:

  • Currently writing a gem
  • One of its features is that when rails generate my_gem_name:install is run, it (should) override the default javascript scaffold generated file (which is produced by rails generate scaffold some_model_name):

    • app/assets/javascripts/some_model_name.coffee

      • From:

        # Place all the behaviors and hooks related to the matching controller here.
        # All this logic will automatically be available in application.js.
        # You can use CoffeeScript in this file: http://coffeescript.org/
        
      • To:

        # Place all the behaviors and hooks related to the matching controller here.
        # All this logic will automatically be available in application.js.
        # You can use CoffeeScript in this file: http://coffeescript.org/
        
        SomeJavascriptCode.doSomething({model: 'SomeModelName'})
        

Question:

  • How to write the said Rails generator (from above) that would override the default scaffold javascript file that is created by rails generate scaffold some_model_name.
  • Take note that the desired javascript file (as shown above), has dynamic contents; that is {model: 'SomeModelName'} should change & match correctly with the model name being generated.

Attempts:

  • I realised that there are two steps to solve this:

    • simply just being able to override the scaffold-generated javascript file

    • then, do something that would make the contents of said generated javascript file, to have "dynamic" contents.

  • 1st step:

    • Trying to write a generator that would copy my template file into Rails' AssetsGenerator template, eventually (hoping) to override it.

      # lib/generators/my_gem_name/install_generator.rb
      module MyGemName
        class InstallGenerator < Rails::Generators::Base
          source_root File.expand_path('../templates', __FILE__)
      
          class_option :javascript_engine, desc: 'JS engine to be used: [js, coffee].'
      
          def copy_js_scaffold_template
            copy_file "javascript.#{javascript_engine}", "lib/templates/coffee-rails/assets/javascript.#{javascript_engine}"
          end
      
          private
      
          def javascript_engine
            options[:javascript_engine]
          end
        end
      end
      
      # lib/generators/my_gem_name/templates/javascript.coffee
      console.log('JUST TESTING!!!!!!!')
      
      • I temporarily used the path lib/templates/coffee-rails/assets/javascript.coffee just to test it out as the default javascript_engine is coffee. Probably that should change depending on --javascript_engine), but can't seem to make it work. I got this path from this LINK and then by referencing THIS:

        It seems that the pattern is flowing: lib/templates/gem_name/generator_name/template_file_name.rb

      • From this LINK, I also tried using the paths (but didnt work):

        • lib/templates/rails/assets/javascript.coffee
        • lib/templates/rails/assets/coffee/javascript.coffee
    • 2nd Step: No attempts so far yet, as I'd like to make 1st Step above to work first.

All of my attempts above doesn't work: that is after both rails generate my_gem_name:install is ran, then running rails generate scaffold some_model_name still produces the original default javascript file, and not the expected console.log('JUST TESTING!!!!!!!') content (as I described above)

1 Answers
Related