expectedbehavior / active_mailer Goto Github PK
View Code? Open in Web Editor NEWEmail with business logic and history
License: MIT License
Email with business logic and history
License: MIT License
(rdbg) itme.recipients << "[email protected]" # ruby
eval error: EmailUser(#17760) expected, got "[email protected]" which is an instance of String(#3440)
/Users/esquivalient/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/association.rb:299:in `raise_on_type_mismatch!'
/Users/esquivalient/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/has_many_through_association.rb:17:in `block in concat'
/Users/esquivalient/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/has_many_through_association.rb:16:in `each'
/Users/esquivalient/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/has_many_through_association.rb:16:in `concat'
/Users/esquivalient/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/associations/collection_proxy.rb:1032:in `<<'
(rdbg)//Users/esquivalient/Dropbox/code/burndown/test/models/invite_team_member_email_test.rb:1:in `block in <class:InviteTeamMemberEmailTest>'
nil
doesn't work, but
(rdbg) itme.recipients = ["[email protected]"] # ruby
[#<ActiveMailer::EmailUser:0x000000010efb6558
id: 9,
email_address: "[email protected]",
created_at: Thu, 04 Aug 2022 19:34:23.486894000 UTC +00:00,
updated_at: Thu, 04 Aug 2022 19:34:23.486894000 UTC +00:00>]
(rdbg)
does. That's a needless paper cut.
While there's nothing technically wrong with app/views/active_mailer/base/default_action_mailer/
it's not an intuitive, doesn't conform to reasonable expectations, and a bit tedious to access a path that deep. ActiveMailer views should be at app/views/emails
by default.
If your mail has text and HTML parts, the Mail::Message
created in send is multipart, so to_s
is effectively a no-op. I'm not sure exactly what the best practice would be here, but sent_mail.parts.map(&:to_s).join
works.
It currently only creates a migration, leaving the user to create the model and view on their own.
The migration generator for ActiveMailer doesn't include the ActiveRecord version number, which causes this error when the migration is executed
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Active Record release the migration was written for:
class CreateInviteTeamMemberEmails < ActiveRecord::Migration[7.0]
/path/to/rails_root/db/migrate/20220804162528_create_invite_team_member_emails.rb:1:in `<main>'
The generator should include the current ActiveRecord number for the project when creating the ActiveMailer migration.
Make an ActiveMailer email with subject and sender, but no recipient, and send!
it. You'll see
Error:
InviteTeamMemberEmailTest#test_can_send_valid_invitation_email:
ArgumentError: SMTP To address may not be blank: []
test/models/invite_team_member_email_test.rb:7:in `block in <class:InviteTeamMemberEmailTest>'
instead of the nice message from ActiveMailer::Base that should like like "You have to have at least one recipient in the to, cc, or bcc fields" from this code
validate :must_have_at_least_one_recipient_of_some_kind
def must_have_at_least_one_recipient_of_some_kind
if self.recipients.blank? and self.cc.blank? and self.bcc.blank?
self.errors[:base] << "You have to have at least one recipient in the to, cc, or bcc fields"
end
end
I think this is because the Rails errors interface has changed and ActiveMailer needs to be updated to add errors the correct way. I think something like this
class Comment
include ActiveModel::Validations
validate do |comment|
comment.must_be_friends
end
def must_be_friends
errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
end
end
From here:
There's only a partial generator. In the mean time, making a new ActiveMailer class can be done like this.
It's easier to have more collaborators if the tests are run automatically and the results can be seen from the PR.
create_active_mailer_table automatically makes
# t.integer :sender_id
# t.timestamp :sent_at
# t.string :subject
and they're also included in the generated migration. That results in errors like
you can't define an already defined column 'sent_at'.
/Users/esquivalient/Dropbox/code/burndown/db/migrate/20220804162528_create_invite_team_member_emails.rb:8:in `block in change'
/Users/esquivalient/Dropbox/code/burndown/db/migrate/20220804162528_create_invite_team_member_emails.rb:3:in `change'
class ResetPasswordEmail < ActiveMailer::Base
before_send :do_a_thing
def do_a_thing
end
end
class InviteUserEmail < ActiveMailer::Base
end
InviteUserEmail.before_send_actions
=> [:do_a_thing]
ActiveMailer can do a lot. It would be more convenient for users if the newly generated model included comments and commented code demonstrating common use cases like
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.