Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom rails generators #136

Open
dkniffin opened this issue Nov 15, 2018 · 1 comment
Open

Add custom rails generators #136

dkniffin opened this issue Nov 15, 2018 · 1 comment
Labels
New Rails Config New functionality to be added to the generated Rails app

Comments

@dkniffin
Copy link
Contributor

For example, customize our model generator so that it generates specs with all the right it { is_expected.to respond_to(:attribute) } and such

@dkniffin dkniffin added the New Rails Config New functionality to be added to the generated Rails app label Nov 15, 2018
@dkniffin
Copy link
Contributor Author

dkniffin commented Nov 30, 2018

One nice thing would be if generated files got # frozen_string_literal: true at the top by default

Edit: Here's the code to do that:

# https://gist.github.com/ta1kt0me/6a7058d16621785d4f7038bde6cd3b98

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\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

It can go in config/initializers/generators/frozen_string_literal.rb

@dkniffin dkniffin added racxob and removed racxob labels May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Rails Config New functionality to be added to the generated Rails app
Projects
None yet
Development

No branches or pull requests

1 participant