Skip to content

Commit 4559a7d

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 4559a7d

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
copy_file "dev.rake", "lib/tasks/dev.rake"
13+
end
14+
end
15+
end
16+
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

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,38 @@
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:prime task" 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 "has a custom description" do
24+
assert_no_match(/Description:/, generator_class.desc)
25+
end
26+
27+
private
28+
29+
def dev_rake
30+
File.read("./lib/generators/templates/tasks/dev.rake")
31+
end
32+
33+
def restore_destination
34+
remove_file_if_exists "lib/tasks/dev.rake"
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)