Skip to content

Commit 9692e40

Browse files
committed
Add generator specs
1 parent 1cedfa5 commit 9692e40

File tree

11 files changed

+233
-16
lines changed

11 files changed

+233
-16
lines changed

Gemfile.lock

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ PATH
22
remote: .
33
specs:
44
supermail (0.1.0)
5-
actionmailer (>= 7.0)
5+
actionmailer (~> 7.0)
6+
activesupport (~> 7.0)
7+
railties (~> 7.0)
68

79
GEM
810
remote: https://rubygems.org/
911
specs:
12+
actioncable (7.2.2.2)
13+
actionpack (= 7.2.2.2)
14+
activesupport (= 7.2.2.2)
15+
nio4r (~> 2.0)
16+
websocket-driver (>= 0.6.1)
17+
zeitwerk (~> 2.6)
18+
actionmailbox (7.2.2.2)
19+
actionpack (= 7.2.2.2)
20+
activejob (= 7.2.2.2)
21+
activerecord (= 7.2.2.2)
22+
activestorage (= 7.2.2.2)
23+
activesupport (= 7.2.2.2)
24+
mail (>= 2.8.0)
1025
actionmailer (7.2.2.2)
1126
actionpack (= 7.2.2.2)
1227
actionview (= 7.2.2.2)
@@ -25,6 +40,13 @@ GEM
2540
rails-dom-testing (~> 2.2)
2641
rails-html-sanitizer (~> 1.6)
2742
useragent (~> 0.16)
43+
actiontext (7.2.2.2)
44+
actionpack (= 7.2.2.2)
45+
activerecord (= 7.2.2.2)
46+
activestorage (= 7.2.2.2)
47+
activesupport (= 7.2.2.2)
48+
globalid (>= 0.6.0)
49+
nokogiri (>= 1.8.5)
2850
actionview (7.2.2.2)
2951
activesupport (= 7.2.2.2)
3052
builder (~> 3.1)
@@ -34,6 +56,18 @@ GEM
3456
activejob (7.2.2.2)
3557
activesupport (= 7.2.2.2)
3658
globalid (>= 0.3.6)
59+
activemodel (7.2.2.2)
60+
activesupport (= 7.2.2.2)
61+
activerecord (7.2.2.2)
62+
activemodel (= 7.2.2.2)
63+
activesupport (= 7.2.2.2)
64+
timeout (>= 0.4.0)
65+
activestorage (7.2.2.2)
66+
actionpack (= 7.2.2.2)
67+
activejob (= 7.2.2.2)
68+
activerecord (= 7.2.2.2)
69+
activesupport (= 7.2.2.2)
70+
marcel (~> 1.0)
3771
activesupport (7.2.2.2)
3872
base64
3973
benchmark (>= 0.3)
@@ -76,6 +110,7 @@ GEM
76110
net-imap
77111
net-pop
78112
net-smtp
113+
marcel (1.1.0)
79114
mini_mime (1.1.5)
80115
mini_portile2 (2.8.9)
81116
minitest (5.25.5)
@@ -88,6 +123,7 @@ GEM
88123
timeout
89124
net-smtp (0.5.1)
90125
net-protocol
126+
nio4r (2.7.4)
91127
nokogiri (1.18.10)
92128
mini_portile2 (~> 2.8.2)
93129
racc (~> 1.4)
@@ -106,13 +142,37 @@ GEM
106142
rack (>= 3.0.0)
107143
rack-test (2.2.0)
108144
rack (>= 1.3)
145+
rackup (2.2.1)
146+
rack (>= 3)
147+
rails (7.2.2.2)
148+
actioncable (= 7.2.2.2)
149+
actionmailbox (= 7.2.2.2)
150+
actionmailer (= 7.2.2.2)
151+
actionpack (= 7.2.2.2)
152+
actiontext (= 7.2.2.2)
153+
actionview (= 7.2.2.2)
154+
activejob (= 7.2.2.2)
155+
activemodel (= 7.2.2.2)
156+
activerecord (= 7.2.2.2)
157+
activestorage (= 7.2.2.2)
158+
activesupport (= 7.2.2.2)
159+
bundler (>= 1.15.0)
160+
railties (= 7.2.2.2)
109161
rails-dom-testing (2.3.0)
110162
activesupport (>= 5.0.0)
111163
minitest
112164
nokogiri (>= 1.6)
113165
rails-html-sanitizer (1.6.2)
114166
loofah (~> 2.21)
115167
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
168+
railties (7.2.2.2)
169+
actionpack (= 7.2.2.2)
170+
activesupport (= 7.2.2.2)
171+
irb (~> 1.13)
172+
rackup (>= 1.0.0)
173+
rake (>= 12.2)
174+
thor (~> 1.0, >= 1.2.2)
175+
zeitwerk (~> 2.6)
116176
rake (13.3.0)
117177
rdoc (6.14.2)
118178
erb
@@ -134,17 +194,24 @@ GEM
134194
rspec-support (3.13.6)
135195
securerandom (0.4.1)
136196
stringio (3.1.7)
197+
thor (1.4.0)
137198
timeout (0.4.3)
138199
tzinfo (2.0.6)
139200
concurrent-ruby (~> 1.0)
140201
useragent (0.16.11)
202+
websocket-driver (0.8.0)
203+
base64
204+
websocket-extensions (>= 0.1.0)
205+
websocket-extensions (0.1.5)
206+
zeitwerk (2.7.3)
141207

142208
PLATFORMS
143209
arm64-darwin-24
144210
ruby
145211

146212
DEPENDENCIES
147213
irb
214+
rails (~> 7.0)
148215
rake (~> 13.0)
149216
rspec (~> 3.0)
150217
supermail!
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators/named_base'
4+
5+
module Supermail
6+
class EmailGenerator < Rails::Generators::NamedBase
7+
source_root File.expand_path('templates', __dir__)
8+
9+
desc "Generate a new email class"
10+
11+
def create_email_file
12+
template 'email.rb', "app/emails/#{file_path}_email.rb"
13+
end
14+
15+
private
16+
17+
def file_path
18+
name.underscore
19+
end
20+
21+
def class_name
22+
name.camelize
23+
end
24+
end
25+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class <%= class_name %>Email < ApplicationEmail
4+
def body = <<~PLAIN
5+
6+
PLAIN
7+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators/base'
4+
5+
module Supermail
6+
class InstallGenerator < Rails::Generators::Base
7+
source_root File.expand_path('templates', __dir__)
8+
9+
desc "Install Supermail in a Rails application"
10+
11+
def create_application_email
12+
template 'application_email.rb', 'app/emails/application_email.rb'
13+
end
14+
15+
def create_emails_directory
16+
empty_directory 'app/emails'
17+
end
18+
end
19+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class ApplicationEmail < Supermail::Base
2+
def from = "[email protected]"
3+
4+
def body
5+
<<~_
6+
#{yield if block_given?}
7+
8+
Best,
9+
10+
The Example.com Team
11+
_
12+
end
13+
end

lib/supermail.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66
module Supermail
77
class Error < StandardError; end
88

9-
module Rails
10-
class Base
11-
delegate :deliver, :deliver_now, :deliver_later, to: :message_delivery
9+
class Base
10+
delegate :deliver, :deliver_now, :deliver_later, to: :message_delivery
1211

13-
def to = nil
14-
def from = nil
15-
def subject = nil
16-
def body = ""
12+
def to = nil
13+
def from = nil
14+
def subject = nil
15+
def body = ""
1716

18-
def message
19-
message_delivery.message
20-
end
17+
def message
18+
message_delivery.message
19+
end
2120

22-
def message_delivery
23-
ActionMailer::Base.mail(to:, from:, subject:, body:)
24-
end
21+
def message_delivery
22+
ActionMailer::Base.mail(to:, from:, subject:, body:)
2523
end
2624
end
2725
end
26+
27+
# Load generators only when Rails is available
28+
if defined?(::Rails)
29+
require 'rails'
30+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "generators/supermail/email/email_generator"
5+
6+
RSpec.describe Supermail::EmailGenerator, type: :generator do
7+
tests Supermail::EmailGenerator
8+
9+
EMAIL_DESTINATION_PATH = Pathname.new(__dir__).join("../tmp/generators")
10+
destination EMAIL_DESTINATION_PATH.to_s
11+
12+
before { prepare_destination }
13+
after { FileUtils.rm_rf(EMAIL_DESTINATION_PATH) }
14+
15+
describe "with namespaced email" do
16+
before { run_generator ["User::Welcome"] }
17+
18+
describe "app/emails/user/welcome_email.rb" do
19+
subject { File.read(EMAIL_DESTINATION_PATH.join("app/emails/user/welcome_email.rb")) }
20+
it { is_expected.to match(/class User::WelcomeEmail < ApplicationEmail/) }
21+
it { is_expected.to match(/def body = <<~PLAIN/) }
22+
end
23+
end
24+
25+
describe "with simple email" do
26+
before { run_generator ["Welcome"] }
27+
28+
describe "app/emails/welcome_email.rb" do
29+
subject { File.read(EMAIL_DESTINATION_PATH.join("app/emails/welcome_email.rb")) }
30+
it { is_expected.to match(/class WelcomeEmail < ApplicationEmail/) }
31+
it { is_expected.to match(/def body = <<~PLAIN/) }
32+
end
33+
end
34+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
require "generators/supermail/install/install_generator"
5+
6+
RSpec.describe Supermail::InstallGenerator, type: :generator do
7+
tests Supermail::InstallGenerator
8+
9+
INSTALL_DESTINATION_PATH = Pathname.new(__dir__).join("../tmp/generators")
10+
destination INSTALL_DESTINATION_PATH.to_s
11+
12+
before { prepare_destination }
13+
after { FileUtils.rm_rf(INSTALL_DESTINATION_PATH) }
14+
15+
describe "after running generator" do
16+
before { run_generator }
17+
18+
describe "app/emails/application_email.rb" do
19+
subject { File.read(INSTALL_DESTINATION_PATH.join("app/emails/application_email.rb")) }
20+
it { is_expected.to match(/class ApplicationEmail < Supermail::Base/) }
21+
it { is_expected.to match(/def from = "[email protected]"/) }
22+
it { is_expected.to match(/The Example.com Team/) }
23+
end
24+
25+
describe "app/emails directory" do
26+
subject { File.directory?(INSTALL_DESTINATION_PATH.join("app/emails")) }
27+
it { is_expected.to be true }
28+
end
29+
end
30+
end

spec/spec_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
require 'rails/all'
4+
require 'rails/generators/test_case'
5+
require 'minitest/assertions'
36
require "supermail"
47

58
RSpec.configure do |config|
@@ -13,3 +16,15 @@
1316
c.syntax = :expect
1417
end
1518
end
19+
20+
require "rails/generators/testing/behavior"
21+
require "rails/generators/testing/setup_and_teardown"
22+
require "rails/generators/testing/assertions"
23+
24+
RSpec.configure do |config|
25+
config.include Rails::Generators::Testing::Behavior, type: :generator
26+
config.include Rails::Generators::Testing::SetupAndTeardown, type: :generator
27+
config.include Rails::Generators::Testing::Assertions, type: :generator
28+
config.include Minitest::Assertions, type: :generator
29+
config.include FileUtils, type: :generator
30+
end

spec/supermail_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "spec_helper"
44

5-
class ExampleMailer < Supermail::Rails::Base
5+
class ExampleMailer < Supermail::Base
66
def initialize(to:, from:, subject:, body:)
77
@to = to
88
@from = from

0 commit comments

Comments
 (0)