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'})
rails generate scaffold some_model_name
.{model: 'SomeModelName'}
should change & match correctly with the model name being generated.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):
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)
I finally solved it by the following. Hopefully, it would be helpful to anyone:
For Step 1 (Overriding the javascript file):
The problem was the override-path is not
lib/templates/gem_name/generator_name/template_file_name.rb
but
lib/templates/module_name/generator_name/template_file_name.rb
This I verified after looking at Coffe-Rails Generator Code and then trying out the following (which worked: the scaffold-generated javascript file now gets overriden after running rails generate my_gem_name:install
then followed by the scaffold generator rails generate scaffold some_model_name
)
# I temporarily hardcoded `coffee` as the `javascript_engine` to demonstrate, but `js` also works.
copy_file "javascript.coffee", "lib/templates/coffee/assets/javascript.coffee"
For Step 2 (Dynamic javascript file content):
<%= ... %>
inside the javascript file, but then rename the template into a ruby
file extension. The template
seemingly gets rendered like how Rails views work when you insert <%= ... %>
inside the file.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}.rb", "lib/templates/#{javascript_engine}/assets/javascript.#{javascript_engine}"
end
private
def javascript_engine
options[:javascript_engine]
end
end
end
lib/generators/my_gem_name/templates/javascript.coffee.rb
# 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: '<%= singular_table_name.camelcase %>'})
Then, on terminal:
rails generate my_gem_name:install
Running via Spring preloader in process 42586
create lib/templates/coffee/assets/javascript.coffee
rails generate scaffold user email first_name last_name
Running via Spring preloader in process 43054
invoke active_record
create db/migrate/20170721152013_create_users.rb
create app/models/user.rb
...
invoke assets
invoke coffee
create app/assets/javascripts/users.coffee
...
cat app/assets/javascripts/users.coffee
# 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: 'User'})