Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Last active May 29, 2022 13:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ta1kt0me/6a7058d16621785d4f7038bde6cd3b98 to your computer and use it in GitHub Desktop.
Save ta1kt0me/6a7058d16621785d4f7038bde6cd3b98 to your computer and use it in GitHub Desktop.
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
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.

@dkniffin
Copy link

dkniffin commented May 7, 2019

@ta1kt0me Thank you for publishing this. It saved me the time to figure it out myself. One question: What does the Rails::Generators::Migration line do? My linter is complaining about Lint/Void: Variable Migration used in void context., but if I remove it, the generator fails. Is there a method this is calling that I could call directly?

Edit: it looks like it's triggering rails's autoload functionality. Removing it and adding this at the top of the file worked: require "rails/generators/migration"

@thornomad
Copy link

I had to modify it slightly to work with Rails 6: https://gist.github.com/thornomad/4e2f0905e2a4a6eefbc4be5772dfd4f7

Thank you!

@tleish
Copy link

tleish commented Nov 19, 2020

A simpler version of this might be

return unless defined?(::Rails::Generators)

module RailsGeneratorFrozenStringLiteralPrepend
  RUBY_EXTENSIONS = %w[.rb .rake]

  def render
    return super unless RUBY_EXTENSIONS.include? File.extname(self.destination)
    "# frozen_string_literal: true\n\n" + super
  end
end

Thor::Actions::CreateFile.prepend RailsGeneratorFrozenStringLiteralPrepend

@zachGl
Copy link

zachGl commented Feb 12, 2021

A simpler version of this might be

return unless defined?(::Rails::Generators)

module RailsGeneratorFrozenStringLiteralPrepend
  RUBY_EXTENSIONS = %w[.rb .rake]

  def render
    return super unless RUBY_EXTENSIONS.include? File.extname(self.destination)
    "# frozen_string_literal: true\n\n" + super
  end
end

Thor::Actions::CreateFile.prepend RailsGeneratorFrozenStringLiteralPrepend

Where did you that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment