Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add frozen string literal comment into generated files in rails v5.1.0
module AddFrozenStringLiteralComment
def add_frozen_string_literal_comment(dist)
if File.exist?(dist) && File.extname(dist) == '.rb'
File.open(dist, 'r') do |f|
body = f.read
File.open(dist, 'w') do |new_f|
new_f.write("# frozen_string_literal: true\n" + body)
end
end
end
end
end
module GeneratorPrepend
include AddFrozenStringLiteralComment
def invoke!
res = super
add_frozen_string_literal_comment(existing_migration)
res
end
end
module TemplatePrepend
include AddFrozenStringLiteralComment
def template(source, *args, &block)
res = super
add_frozen_string_literal_comment(args.first)
res
end
end
Rails::Generators::Migration
Rails::Generators::Actions::CreateMigration.send :prepend, GeneratorPrepend
Rails::Generators::NamedBase.send :prepend, TemplatePrepend
@patbl

This comment has been minimized.

Copy link

patbl commented Mar 1, 2019

I stuck mine in config/initializers and wrapped it in if defined?(::Rails::Generators) so that it doesn't unnecessarily load generators. Dunno if there's a better way to accomplish this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.