Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Last active May 29, 2022 13:33
Show Gist options
  • Select an option

  • Save ta1kt0me/6a7058d16621785d4f7038bde6cd3b98 to your computer and use it in GitHub Desktop.

Select an option

Save ta1kt0me/6a7058d16621785d4f7038bde6cd3b98 to your computer and use it in GitHub Desktop.

Revisions

  1. ta1kt0me renamed this gist Mar 4, 2017. 1 changed file with 0 additions and 0 deletions.
  2. ta1kt0me revised this gist Mar 4, 2017. No changes.
  3. ta1kt0me created this gist Mar 4, 2017.
    37 changes: 37 additions & 0 deletions AddFrozenStringLiteralComment.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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