Module: RailsForge::MailerGenerator
- Defined in:
- lib/railsforge/mailer_generator.rb
Overview
MailerGenerator module handles generating mailers
Defined Under Namespace
Classes: InvalidMailerNameError
Class Method Summary
collapse
Class Method Details
.find_rails_app_path ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/railsforge/mailer_generator.rb', line 92
def self.find_rails_app_path
path = Dir.pwd
10.times do
return path if File.exist?(File.join(path, "config", "application.rb"))
parent = File.dirname(path)
break if parent == path
path = parent
end
nil
end
|
.generate(mailer_name, with_spec: true, with_jobs: false) ⇒ Object
Generates a mailer file with views and optionally an RSpec test
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/railsforge/mailer_generator.rb', line 19
def self.generate(mailer_name, with_spec: true, with_jobs: false)
validate_mailer_name(mailer_name)
base_path = find_rails_app_path
unless base_path
raise "Not in a Rails application directory"
end
generate_mailer_file(base_path, mailer_name, with_jobs)
generate_view_files(base_path, mailer_name)
if with_spec
generate_spec_file(base_path, mailer_name)
end
"Mailer '#{mailer_name}' generated successfully!"
end
|
.generate_mailer_file(base_path, mailer_name, with_jobs = false) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/railsforge/mailer_generator.rb', line 37
def self.generate_mailer_file(base_path, mailer_name, with_jobs = false)
mailer_dir = File.join(base_path, "app", "mailers")
FileUtils.mkdir_p(mailer_dir)
file_name = "#{mailer_name.underscore}.rb"
file_path = File.join(mailer_dir, file_name)
if File.exist?(file_path)
puts " Skipping mailer (already exists)"
return file_path
end
File.write(file_path, mailer_template(mailer_name, with_jobs))
puts " Created app/mailers/#{file_name}"
file_path
end
|
.generate_spec_file(base_path, mailer_name) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/railsforge/mailer_generator.rb', line 75
def self.generate_spec_file(base_path, mailer_name)
spec_dir = File.join(base_path, "spec", "mailers")
FileUtils.mkdir_p(spec_dir)
file_name = "#{mailer_name.underscore}_spec.rb"
file_path = File.join(spec_dir, file_name)
if File.exist?(file_path)
puts " Skipping spec (already exists)"
return file_path
end
File.write(file_path, spec_template(mailer_name))
puts " Created spec/mailers/#{file_name}"
file_path
end
|
.generate_view_files(base_path, mailer_name) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/railsforge/mailer_generator.rb', line 54
def self.generate_view_files(base_path, mailer_name)
views_dir = File.join(base_path, "app", "views", mailer_name.underscore)
FileUtils.mkdir_p(views_dir)
methods = ["welcome_email", "notification_email"]
methods.each do |method|
html_path = File.join(views_dir, "#{method}.html.erb")
unless File.exist?(html_path)
File.write(html_path, view_html_template(mailer_name, method))
puts " Created app/views/#{mailer_name.underscore}/#{method}.html.erb"
end
text_path = File.join(views_dir, "#{method}.text.erb")
unless File.exist?(text_path)
File.write(text_path, view_text_template(mailer_name, method))
puts " Created app/views/#{mailer_name.underscore}/#{method}.text.erb"
end
end
end
|
.mailer_template(mailer_name, with_jobs = false) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/railsforge/mailer_generator.rb', line 103
def self.mailer_template(mailer_name, with_jobs = false)
<<~RUBY
# Mailer class for #{mailer_name}
# Handles sending emails related to #{mailer_name.underscore}
#
# Usage:
# #{mailer_name}.with(user: @user).welcome_email.deliver_later
# #{mailer_name}.with(user: @user).notification_email.deliver_now
class #{mailer_name} < ApplicationMailer
# Sends a welcome email to the user
# @param user [User] The user to send the welcome email to
# @return [Mail::Message] The sent mail message
def welcome_email(user)
mail(to: user.email, subject: "Welcome to Our App!")
end
# Sends a notification email to the user
# @param user [User] The user to send the notification to
# @param message [String] The notification message
# @return [Mail::Message] The sent mail message
def notification_email(user, message: "You have a new notification")
@user = user
@message = message
mail(to: user.email, subject: "Notification from Our App")
end
# Class method to queue email via ActiveJob
# Use with --jobs flag for background delivery
# def self.deliver_with_job(user, method: :welcome_email, **args)
# #{mailer_name}Job.perform_later(user, method: method, **args)
# end
end
RUBY
end
|
.spec_template(mailer_name) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/railsforge/mailer_generator.rb', line 163
def self.spec_template(mailer_name)
<<~RUBY
require "rails_helper"
RSpec.describe #{mailer_name}, type: :mailer do
describe "welcome_email" do
let(:user) { User.create!(name: "Test", email: "[email protected]") }
let(:mail) { described_class.welcome_email(user) }
it "renders the headers" do
expect(mail.subject).to eq("Welcome to Our App!")
expect(mail.to).to eq([user.email])
end
end
describe "notification_email" do
let(:user) { User.create!(name: "Test", email: "[email protected]") }
let(:mail) { described_class.notification_email(user, message: "Test") }
it "renders the body" do
expect(mail.body.encoded).to include("Test")
end
end
end
RUBY
end
|
.validate_mailer_name(name) ⇒ Object
Validates the mailer name to ensure it’s valid Ruby class name
8
9
10
11
12
13
14
15
16
|
# File 'lib/railsforge/mailer_generator.rb', line 8
def self.validate_mailer_name(name)
if name.nil? || name.strip.empty?
raise InvalidMailerNameError, "Mailer name cannot be empty"
end
unless name =~ /\A[A-Z][a-zA-Z0-9]*\z/
raise InvalidMailerNameError, "Mailer name must be in PascalCase (e.g., UserMailer)"
end
end
|
.view_html_template(mailer_name, method_name) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/railsforge/mailer_generator.rb', line 138
def self.view_html_template(mailer_name, method_name)
<<~ERB
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
<style>body { font-family: Arial, sans-serif; }</style>
</head>
<body>
<h1>#{method_name == 'welcome_email' ? 'Welcome!' : 'Notification'}</h1>
#{method_name == 'welcome_email' ? '<p>Welcome to our application!</p>' : '<p><%= @message %></p>'}
</body>
</html>
ERB
end
|
.view_text_template(mailer_name, method_name) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/railsforge/mailer_generator.rb', line 154
def self.view_text_template(mailer_name, method_name)
<<~TEXT
#{method_name == 'welcome_email' ? 'Welcome to our application!' : '<%= @message %>'}
--
Our App
TEXT
end
|