Add --spec option for data_update generator (#11245)

This commit is contained in:
Michael Kohl 2020-11-04 00:26:08 +07:00 committed by GitHub
parent c7c6764933
commit 9ad57a8d28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 2 deletions

View file

@ -40,6 +40,26 @@ module DataUpdateScripts
end
```
The generator will also automatically create the corresponding spec file.
```ruby
require "rails_helper"
require Rails.root.join(
"20201103042915_backfill_column_for_articles.rb",
)
describe DataUpdateScripts::BackfillColumnForArticles do
pending "add some examples to (or delete) #{__FILE__}"
end
```
While we encourage adding tests for data update scripts, you can skip spec
creation by adding the `--no-spec` option to the `rails generate` command:
```
rails generate data_update BackfillColumnForArticles --no-spec
```
Once your script is in place then you can either run `rails data_updates:run`
manually or you can let our setup script handle it. In our local
[bin/setup](https://github.com/forem/forem/blob/master/bin/setup) script you

View file

@ -1,11 +1,22 @@
class DataUpdateGenerator < Rails::Generators::NamedBase
source_root File.expand_path("templates", __dir__)
class_option :spec, type: :boolean, default: true
def create_data_update_file
template(
"data_update.rb.tt",
File.join("lib", "data_update_scripts", class_path,
"#{Time.current.utc.strftime('%Y%m%d%H%M%S')}_#{file_name}.rb"),
File.join("lib", "data_update_scripts", class_path, script_name),
)
return unless options["spec"]
template(
"data_update_spec.rb.tt",
File.join("spec", "lib", "data_update_scripts", class_path, "#{file_name}_spec.rb"),
)
end
def script_name
@script_name ||= "#{Time.current.utc.strftime('%Y%m%d%H%M%S')}_#{file_name}.rb"
end
end

View file

@ -0,0 +1,8 @@
require "rails_helper"
require Rails.root.join(
"<%= script_name %>",
)
describe DataUpdateScripts::<%= class_name %> do
pending "add some examples to (or delete) #{__FILE__}"
end