-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
suspenders:tasks
generator
Relates to #1159 For now, this generator simply creates `dev.rake` which contains [dev:prime][]. If future application specific tasks need to be added, we can use this generator. However, this should not be confused with the existing pattern of creating tasks under the suspenders namespace, such as the existing `lib/tasks/suspenders.rake`. Tasks under this namespace cannot be edited by the consumer, whereas tasks generated by `suspenders:tasks` are intended to be edited by the consumer. [dev:prime]: https://thoughtbot.com/blog/priming-the-pump
- Loading branch information
1 parent
c16e593
commit 25c985f
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 FactoryBot" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]", password: "password") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 | ||
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 FactoryBot is not installed" do | ||
output = run_generator | ||
|
||
assert_match /This generator requires FactoryBot/i, 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 |