Skip to content

Commit 25c985f

Browse files
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
1 parent c16e593 commit 25c985f

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed
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 FactoryBot"
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

lib/suspenders/generators.rb

+5
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@ def api_only_app?
4848
.match?(/^\s*config\.api_only\s*=\s*true/i)
4949
end
5050
end
51+
52+
module Unsupported
53+
class Error < StandardError
54+
end
55+
end
5156
end
5257
end

test/generators/suspenders/rake_generator_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RakeGeneratorTest < Rails::Generators::TestCase
1515
run_generator
1616

1717
assert_file app_root("Rakefile") do |file|
18-
assert_match /task default: "suspenders:rake"/, file
18+
assert_match(/task default: "suspenders:rake"/, file)
1919
end
2020
end
2121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
expected = dev_rake
15+
16+
run_generator
17+
18+
assert_file app_root("lib/tasks/dev.rake") do |file|
19+
assert_equal expected, file
20+
end
21+
end
22+
23+
test "returns early if FactoryBot is not installed" do
24+
output = run_generator
25+
26+
assert_match /This generator requires FactoryBot/i, output
27+
assert_no_file app_root("lib/tasks/dev.rake")
28+
end
29+
30+
test "has a custom description" do
31+
assert_no_match(/Description:/, generator_class.desc)
32+
end
33+
34+
private
35+
36+
def dev_rake
37+
File.read("./lib/generators/templates/tasks/dev.rake")
38+
end
39+
40+
def restore_destination
41+
remove_file_if_exists "lib/tasks/dev.rake"
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)