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

Introduce suspenders:tasks generator #1160

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Unreleased
* Introduce `suspenders:rake` generator
* Introduce `suspenders:views` generator
* Introduce `suspenders:setup` generator
* Introduce `suspenders:tasks` generator

20230113.0 (January, 13, 2023)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ A holistic setup script.
bin/setup
```

### Tasks

Creates local Rake tasks for development

```sh
bin/rails dev:prime
```

## Contributing

See the [CONTRIBUTING] document.
Expand Down
20 changes: 20 additions & 0 deletions lib/generators/suspenders/tasks_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Suspenders
module Generators
class TasksGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/tasks", __FILE__)
desc <<~TEXT
Creates local Rake tasks for development
bin/rails dev:prime # Sample data for local development environment
TEXT

def create_dev_rake
if Bundler.rubygems.find_name("factory_bot").any?
copy_file "dev.rake", "lib/tasks/dev.rake"
else
say "This generator requires Factory Bot"
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/generators/templates/tasks/dev.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if Rails.env.development? || Rails.env.test?
require "factory_bot"

namespace :dev do
desc "Sample data for local development environment"
task prime: "db:setup" do
include FactoryBot::Syntax::Methods

# create(:user, email: "user@example.com", password: "password")
end
end
end
47 changes: 47 additions & 0 deletions test/generators/suspenders/tasks_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "test_helper"
require "generators/suspenders/tasks_generator"

module Suspenders
module Generators
class TasksGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::TasksGenerator
destination Rails.root
teardown :restore_destination

test "creates dev.rake file" do
Bundler.rubygems.stubs(:find_name).with("factory_bot").returns([true])

expected = dev_rake

run_generator

assert_file app_root("lib/tasks/dev.rake") do |file|
assert_equal expected, file
end
end

test "returns early if factory_bot_rails is not installed" do
output = run_generator

assert_match /This generator requires Factory Bot/, output
assert_no_file app_root("lib/tasks/dev.rake")
end

test "has a custom description" do
assert_no_match(/Description:/, generator_class.desc)
end

private

def dev_rake
File.read("./lib/generators/templates/tasks/dev.rake")
end

def restore_destination
remove_file_if_exists "lib/tasks/dev.rake"
end
end
end
end