I’ve spent the last fortnight or so writing a Rails LiquidMailer. The Rails docs
are useful if you were to design a mailer with Rails’s built in ActionMailer however
the project I was working on involved using LiquidMailer. So this is what
I’ll cover here.
LiquidMailer is great for building Mailers in instances where you don’t require the files generated
with an ActionMailer. Rails provides you with the MVC structure with ActionMailer whereas LiquidMailer
you create what you need. What you don’t get is the Rails plumbing i.e. the MVC (the views, the application_mailer, the tests)
so you build what you need and plug it into your codebase.
Breaking it down into steps this was the flow:
- Create a Mailer inheriting from LiquidMailer
- Create a spec file in Rails’ spec directory
- Create a view in Rails’ view directory
This is pretty much all you will require. Make sure you have the naming right as always with Rails, things break if the naming is
incorrect or does not follow convention. The part I found difficult to find was testing the sending of emails. Here Rails’
ActionMailer::Preview is quite handy. With LiquidMailer you have to do this manually in the console. It comes with LetterOpener built in
so you will be able to check if the send functionality is there. However unless you are testing from production where the backend is configured
to a email server it won’t actually send the email.
So to do this:
- Run Rails console
- ‘Send’ your email using the command
- This should open up a rich.html page with how your email will appear
Other commands that may be useful:
- To check what the delivery method Rails is using
- To change the delivery_method to smtp use
Remember to call the Mailer in the appropriate file once you’ve finished making it!
I’ve been working through the Ruby track on Exercism.io. I’ve found this to be an incredibly useful resource to practice writing cleaner code and for the mentoring aspect. The other helpful aspect of this resource is the community solutions. There’s a dozen ways to write a solution to a coding problem. Seeing how others approach the same problem was valuable and a great learning resource in itself.
Here are some of the Array manipulation methods I found the most useful and referring to most often when solving the ruby track problems.
-
The splat operator for passing “catch-all” arguments
Perfect for passing an array into a function expecting multiple arguments to a method. This blogpost was helpful: https://www.honeybadger.io/blog/ruby-splat-array-manipulation-destructuring/
-
.map and .each
Great for looping through arrays. Main difference being that .each returns the original array whereas the .map returns a new array modified with your code. https://www.freecodecamp.org/news/six-ruby-array-methods-you-need-to-know-5f81c1e268ce/
-
.join
I’ve found myself trying to google for turning the result of a method returning the contents of an array into a single string or multiple strings. This was the perfect way of achieving this. https://www.freecodecamp.org/news/six-ruby-array-methods-you-need-to-know-5f81c1e268ce/
-
.flatten
I’m referring to this in context of arrays, however it can be useful for hashes too.
Useful for flattening multiple nested arrays into one by recursively flattening it. https://apidock.com/ruby/Array/flatten
-
.take()
Takes enumerable and returns the first n element(s) from the array in a new array. If you pass .take(0) it returns #=> [].
-
.each_char
A string method - for passing each character in a string to a block.
-
.include?
A string method - returns true/false if a string contains a character or string.
-
.all?
Returns true if arguments arent false or nil. Just be careful with passing empty arrays as an argument - it returns true.
-
.scan
-
Regex (/\w/) vs. [[:alpha:]] or [a-zA-Z]
This is based on feedback from Exercism.io for an Acronym problem involving abbreviating the arguments. Using [[:alpha:]] or [a-zA-Z] is more useful in real world scenarios to explicitly exclude ‘_’ as a character.
Resources I found useful:
- http://www.korenlc.com/nested-arrays-hashes-loops-in-ruby/
- https://dev.to/molly_struve/level-up-your-ruby-skillz-working-with-arrays-hnn#chaining
- https://www.rubyguides.com/2015/06/ruby-regex/
- http://zetcode.com/lang/rubytutorial/regex/
- http://ruby-for-beginners.rubymonstas.org/advanced/regular_expressions.html