Skip to content

Commit 9d9a7f2

Browse files
Introduce suspenders:tasks generator
Relates to #1159 For now, this generator simply creates `dev.rake` which contains [dev:prime][]. Note that we'll want this generator to run **after** `suspenders:factories`. 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
1 parent c16e593 commit 9d9a7f2

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Unreleased
1111
* Introduce `suspenders:rake` generator
1212
* Introduce `suspenders:views` generator
1313
* Introduce `suspenders:setup` generator
14+
* Introduce `suspenders:tasks` generator
1415

1516
20230113.0 (January, 13, 2023)
1617

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ A holistic setup script.
142142
bin/setup
143143
```
144144

145+
### Tasks
146+
147+
Creates local Rake tasks for development
148+
149+
```sh
150+
bin/rails dev:prime
151+
```
152+
145153
## Contributing
146154

147155
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Suspenders
2+
module Generators
3+
class TasksGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/tasks", __FILE__)
5+
desc <<~TEXT
6+
Creates local Rake tasks for development
7+
8+
bin/rails dev:prime # Sample data for local development environment
9+
TEXT
10+
11+
def create_dev_rake
12+
if Bundler.rubygems.find_name("factory_bot").any?
13+
copy_file "dev.rake", "lib/tasks/dev.rake"
14+
else
15+
say "This generator requires Factory Bot"
16+
end
17+
end
18+
end
19+
end
20+
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if Rails.env.development? || Rails.env.test?
2+
require "factory_bot"
3+
4+
namespace :dev do
5+
desc "Sample data for local development environment"
6+
task prime: "db:setup" do
7+
include FactoryBot::Syntax::Methods
8+
9+
# create(:user, email: "[email protected]", password: "password")
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "test_helper"
2+
require "generators/suspenders/tasks_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class TasksGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::TasksGenerator
10+
destination Rails.root
11+
teardown :restore_destination
12+
13+
test "creates dev.rake file" do
14+
Bundler.rubygems.stubs(:find_name).with("factory_bot").returns([true])
15+
16+
expected = dev_rake
17+
18+
run_generator
19+
20+
assert_file app_root("lib/tasks/dev.rake") do |file|
21+
assert_equal expected, file
22+
end
23+
end
24+
25+
test "returns early if factory_bot_rails is not installed" do
26+
output = run_generator
27+
28+
assert_match /This generator requires Factory Bot/, output
29+
assert_no_file app_root("lib/tasks/dev.rake")
30+
end
31+
32+
test "has a custom description" do
33+
assert_no_match(/Description:/, generator_class.desc)
34+
end
35+
36+
private
37+
38+
def dev_rake
39+
File.read("./lib/generators/templates/tasks/dev.rake")
40+
end
41+
42+
def restore_destination
43+
remove_file_if_exists "lib/tasks/dev.rake"
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)