supermail 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53951b27c057ea4419f1ffbbc3666189712d30449af65e539148d7df306de7d3
4
- data.tar.gz: 789c9f9844880bae6611b9c5cdb958a4fb6b2d7c59888db016c503ebabec3573
3
+ metadata.gz: 5007ee69916bd3c91239105a78c65ac3fc6729adb206e914881166480cca8d63
4
+ data.tar.gz: 903ba6a76b34ec3f64d8dc0f4bab2d8782a3ea90c727dbec5647d8e8b99a3376
5
5
  SHA512:
6
- metadata.gz: 4de9d3383587ea03cd2eb302c6db564d88972535f286ed3ed2181146a56cc815d893e2a84543435854f92f39b767598bd5cef348516865f6706d290961f38d27
7
- data.tar.gz: 6364025594980a3ca8776970cd20be025cbe044cc69dc55766f89e874842f691e0d2b244a0dba773f92976ca6d69ab28bc0956233d25195e1afbbed67723d956
6
+ metadata.gz: d696046239e5c7bbb875b291ca99fc2533127f72babac2991ace61520522dc095788506857c1b8cc56c91f74e5c5f24de9db88a3046421ea10e9a677ac6fb4c1
7
+ data.tar.gz: c2e1303de634498310018e9edbecbb1f0718b545698f56b378d172b4a1fc11389d572ab35019a6bf419faf345b8554b27f95181f4c2ec4e4b0ddca4c81da6c78
data/README.md CHANGED
@@ -1,6 +1,29 @@
1
1
  # Supermail
2
2
 
3
- Supermail is a slightly more intuitive way of organizing Emails in a Rails application.
3
+ Organize emails with plain 'ol Ruby objects in a Rails application, like this:
4
+
5
+ ```ruby
6
+ # ./app/email/user/welcome.rb
7
+ class User::WelcomeEmail < ApplicationEmail
8
+ def initialize(person:)
9
+ @person = person
10
+ end
11
+
12
+ def to = @person.email
13
+ def subject = "Welcome to Beautiful Ruby"
14
+ def body
15
+ super do
16
+ <<~_
17
+ Hi #{@person.name},
18
+
19
+ You're going to learn a ton at https://beautifulruby.com.
20
+ _
21
+ end
22
+ end
23
+ end
24
+ ```
25
+
26
+ Contrast that with rails ActionMailer, where you will spend 20 minutes trying to figure out how to send an email. I created this gem because I got tired of digging through Rails docs to understand how to intialize an email and send it. PORO's FTW!
4
27
 
5
28
  ## Installation
6
29
 
@@ -16,22 +39,21 @@ Then install it in Rails.
16
39
  rails generate supermail:install
17
40
  ```
18
41
 
19
- This creates the `app/emails/application_email.rb` file that you can customize as the base for all emails.
42
+ This creates the `ApplicationEmail` class at `app/emails/application_email.rb` where you can customize the base for all emails, including setting defaults like the `from` address.
20
43
 
21
44
  ```ruby
22
- class ApplicationEmail < Supermail::Email
23
- def from = "Supermail <noreply@supermail.com>"
24
-
25
- class HTML
26
- def after_template
27
- p { "Best, The Supermail Team" }
28
- end
29
- end
30
-
31
- class Text
32
- def after_template
33
- "Best,\nThe Supermail Team"
34
- end
45
+ class ApplicationEmail < Supermail::Rails::Base
46
+ def from = "website@example.com"
47
+ def to = nil
48
+ def subject = nil
49
+ def body
50
+ <<~_
51
+ #{yield if block_given?}
52
+
53
+ Best,
54
+
55
+ The Example.com Team
56
+ _
35
57
  end
36
58
  end
37
59
  ```
@@ -44,27 +66,34 @@ To generate a new email, run the following command:
44
66
  rails generate supermail:email User::Welcome
45
67
  ```
46
68
 
47
- This will create a new email class in `app/mailers/user/welcome.rb`.
69
+ This will create a new email class in `app/mailers/user/welcome_email.rb`.
48
70
 
49
71
  ```ruby
50
72
  # ./app/email/user/welcome.rb
51
- class User::Welcome < ApplicationEmail
52
- def initialize(user:)
53
- @user = user
54
- end
73
+ class User::WelcomeEmail < ApplicationEmail
74
+ def body = <<~PLAIN
75
+ Hello there!
76
+ PLAIN
77
+ end
78
+ ```
55
79
 
56
- def subject = "Welcome to Supermail!"
57
80
 
58
- class HTML
59
- def view_template
60
- h1 { "Welcome, #{@user.name}!" }
61
- p { "We're excited to have you on board." }
62
- end
81
+ ```ruby
82
+ # ./app/email/user/welcome.rb
83
+ class User::WelcomeEmail < ApplicationEmail
84
+ def initialize(person:)
85
+ @person = person
63
86
  end
64
87
 
65
- class Text
66
- def view_template
67
- "Welcome, #{@user.name}!\n\nWe're excited to have you on board."
88
+ def to = @person.email
89
+ def subject = "Welcome to the website"
90
+ def body
91
+ super do
92
+ <<~_
93
+ Hi #{@person.name},
94
+
95
+ Welcome to the website We're excited to have you on board.
96
+ _
68
97
  end
69
98
  end
70
99
  end
@@ -82,6 +111,7 @@ If you want to tweak the message on the fly, you can modify the message, then de
82
111
  User::Welcome.new(user: User.first).message.tap do
83
112
  it.to << "another@example.com"
84
113
  end.deliver_now
114
+ ```
85
115
 
86
116
  ## Development
87
117
 
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,13 @@
1
+ class ApplicationEmail < Supermail::Base
2
+ def from = "website@example.com"
3
+
4
+ def body
5
+ <<~_
6
+ #{yield if block_given?}
7
+
8
+ Best,
9
+
10
+ The Example.com Team
11
+ _
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Supermail
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/supermail.rb CHANGED
@@ -1,85 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "supermail/version"
4
-
5
- require 'mail'
6
- require "supermail/version"
4
+ require 'action_mailer'
7
5
 
8
6
  module Supermail
9
7
  class Error < StandardError; end
10
8
 
11
- # A simple Email builder that wraps Mail::Message
12
- class Email
13
- # Hook to ensure default initialization runs before any subclass initialize
14
- module Defaults
15
- def initialize(*args, &block)
16
- # default fields
17
- @cc = []
18
- @bcc = []
19
- @headers = {}
20
- @attachments = []
21
- super
22
- end
23
- end
24
-
25
- # Whenever subclassing, prepend Defaults to ensure it wraps initialize
26
- def self.inherited(subclass)
27
- subclass.prepend Defaults
28
- super
29
- end
9
+ class Base
10
+ delegate :deliver, :deliver_now, :deliver_later, to: :message_delivery
30
11
 
31
- attr_accessor :from, :to, :cc, :bcc, :subject,
32
- :reply_to, :return_path, :date, :message_id,
33
- :in_reply_to, :references, :headers,
34
- :text_body, :html_body, :attachments
12
+ def to = nil
13
+ def from = nil
14
+ def subject = nil
15
+ def body = ""
35
16
 
36
- # Builds a Mail::Message from this Email's attributes
37
- # @return [Mail::Message]
38
17
  def message
39
- mail = Mail.new
40
- mail.from = Array(from) if from
41
- mail.to = Array(to)
42
- mail.cc = cc if cc.any?
43
- mail.bcc = bcc if bcc.any?
44
- mail.reply_to = Array(reply_to) if reply_to
45
- mail.return_path = return_path if return_path
46
- mail.date = date if date
47
- mail.message_id = message_id if message_id
48
- mail.in_reply_to = in_reply_to if in_reply_to
49
- mail.references = references if references
50
-
51
- # Custom headers
52
- headers.each { |key, value| mail.header[key] = value }
53
-
54
- mail.subject = subject if subject
55
-
56
- # Bodies
57
- if text_body
58
- mail.text_part = Mail::Part.new do
59
- body text_body
60
- end
61
- end
62
-
63
- if html_body
64
- mail.html_part = Mail::Part.new do
65
- content_type 'text/html; charset=UTF-8'
66
- body html_body
67
- end
68
- end
69
-
70
- # Attachments (each as a hash: { filename:, content: })
71
- attachments.each do |att|
72
- mail.add_file filename: att[:filename], content: att[:content]
73
- end
74
-
75
- mail
18
+ message_delivery.message
76
19
  end
77
20
 
78
- # Delivers the built Mail::Message via its configured delivery_method
79
- # @return [Mail::Message]
80
- def deliver
81
- raise Error, "`to` address is required" unless to
82
- message.deliver!
21
+ def message_delivery
22
+ ActionMailer::Base.mail(to:, from:, subject:, body:)
83
23
  end
84
24
  end
85
25
  end
26
+
27
+ # Load generators only when Rails is available
28
+ if defined?(::Rails)
29
+ require 'rails'
30
+ end
metadata CHANGED
@@ -1,28 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supermail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-17 00:00:00.000000000 Z
10
+ date: 2025-09-26 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
- name: mail
13
+ name: actionmailer
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "~>"
16
+ - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '2.0'
18
+ version: '7.0'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
- - - "~>"
23
+ - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: '2.0'
25
+ version: '7.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.0'
26
40
  description: Build emails with plain ol' Ruby objects.
27
41
  email:
28
42
  - bradgessler@gmail.com
@@ -33,16 +47,20 @@ files:
33
47
  - ".rspec"
34
48
  - README.md
35
49
  - Rakefile
50
+ - lib/generators/supermail/email/email_generator.rb
51
+ - lib/generators/supermail/email/templates/email.rb.tt
52
+ - lib/generators/supermail/install/install_generator.rb
53
+ - lib/generators/supermail/install/templates/application_email.rb.tt
36
54
  - lib/supermail.rb
37
55
  - lib/supermail/version.rb
38
56
  - sig/supermail.rbs
39
- homepage: https://github.com/rubymonolith/supermail
57
+ homepage: https://github.com/beautifulruby/supermail
40
58
  licenses: []
41
59
  metadata:
42
60
  allowed_push_host: https://rubygems.org
43
- homepage_uri: https://github.com/rubymonolith/supermail
44
- source_code_uri: https://github.com/rubymonolith/supermail
45
- changelog_uri: https://github.com/rubymonolith/supermail
61
+ homepage_uri: https://github.com/beautifulruby/supermail
62
+ source_code_uri: https://github.com/beautifulruby/supermail
63
+ changelog_uri: https://github.com/beautifulruby/supermail
46
64
  rdoc_options: []
47
65
  require_paths:
48
66
  - lib