Skip to content

Commit

Permalink
Refactoring the email preview setup (#41)
Browse files Browse the repository at this point in the history
* Refactoring the email preview setup to make it a little less confusing. Also helps to prevent error prone setup with non-uri compliant keys. Ref #23

* oops. forgot to remove this require after deciding to ditch it
  • Loading branch information
jwoertink authored May 10, 2021
1 parent 7bc5a89 commit 6dc3400
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,44 +87,44 @@ Breeze comes with a [Carbon](https://github.com/luckyframework/carbon) extension

1. Add the require to your `src/shards.cr` right below your `require "breeze"`:

```crystal
require "breeze"
require "breeze/extensions/carbon"
```
```crystal
require "breeze"
require "breeze/extensions/breeze_carbon"
```

2. Add your Email preview class to `src/emails/previews.cr`:

```crystal
class Emails::Previews < Carbon::EmailPreviews
def previews : Hash(String, Carbon::Email)
{
"welcome_email" => WelcomeEmail.new(UserQuery.first),
"password_reset" => PasswordResetRequestEmail.new(UserQuery.first),
} of String => Carbon::Email
end
end
```
```crystal
class Emails::Previews < Carbon::EmailPreviews
def previews : Array(Carbon::Email)
[
WelcomeEmail.new(UserQuery.first),
PasswordResetRequestEmail.new(UserQuery.first),
] of Carbon::Email
end
end
```

3. Update your Breeze config in `config/breeze.cr`:
3. Add the `BreezeCarbon` config to `config/breeze.cr`:

```crystal
BreezeCarbon.configure do |settings|
# Set this to the name of your preview class
settings.email_previews = Emails::Previews
end
Breeze.register BreezeCarbon
```

```crystal
Breeze.configure do |settings|
# ... other settings
# Set this to the name of your preview class
settings.email_previews = Emails::Previews
end
```

### Usage

Just visit `/breeze/emails` in your browser, and you'll see your emails. Click the `HTML` button to see the HTML version of your email, or the `TEXT` to see the plain text version.

### Configuration

By enabling this extension, Breeze will require a new setting `email_previews` which is the class that will contain your preview setup. This is so Breeze knows what you named the class.
`BreezeCarbon` requires setting the `email_previews` setting to the name of your email preview class.
Your email preview class should inherit from `Carbon::EmailPreviews`, and define an instance method `previews` which returns an `Array(Carbon::Email)`.

Your email preview class should inherit from `Carbon::EmailPreviews`, and define an instance method `previews` which returns a `Hash(String, Carbon::Email)`. The `String` key is a key that will be passed through the URL to locate which email Breeze will display. The value will be an instance of the `Carbon::Email` to be rendered. Since each email requires different arguments in order to be instantiated, it's up to you to define how those values are set.

## Extending Breeze

Expand Down
10 changes: 6 additions & 4 deletions src/breeze_carbon/email_previews.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# TODO: Move this in to Carbon directly
abstract class Carbon::EmailPreviews
alias EmailLookup = Hash(String, Carbon::Email)

abstract def previews : EmailLookup
abstract def previews : Array(Carbon::Email)

def self.find(key : String) : Carbon::Email
self.new.previews[key]? || raise "No Carbon::Email found with the key #{key}. Be sure to add it to your `previews` method"
email = self.new.previews.find do |carbon|
carbon.class.name == key
end

email || raise "No Carbon::Email found with the key #{key}. Be sure to add it to your `previews` method"
end
end
16 changes: 10 additions & 6 deletions src/breeze_carbon/pages/emails/index_page.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BreezeCarbon::Emails::IndexPage < Breeze::BreezeLayout
needs emails : Carbon::EmailPreviews::EmailLookup
needs emails : Array(Carbon::Email)

def page_title : String
"All Emails"
Expand All @@ -15,14 +15,14 @@ class BreezeCarbon::Emails::IndexPage < Breeze::BreezeLayout
h2 "Preview Emails", class: "text-xl"
end
ul class: "mt-1 divide-y divide-gray-200" do
emails.each do |key, email|
email_row(key, email)
emails.each do |email|
email_row(email)
end
end
end
end

def email_row(key : String, email : Carbon::Email)
def email_row(email : Carbon::Email)
li do
div class: "flex items-center px-4 py-4 sm:px-4" do
div class: "min-w-0 flex-1 flex items-center" do
Expand All @@ -33,14 +33,18 @@ class BreezeCarbon::Emails::IndexPage < Breeze::BreezeLayout
end
end
div do
link "HTML", to: Emails::Show.with(key, plain_format: false), class: format_button_styles
link "HTML", to: Emails::Show.with(slug_for_email(email), plain_format: false), class: format_button_styles
nbsp
link "TEXT", to: Emails::Show.with(key, plain_format: true), class: format_button_styles
link "TEXT", to: Emails::Show.with(slug_for_email(email), plain_format: true), class: format_button_styles
end
end
end
end

private def slug_for_email(email : Carbon::Email) : String
email.class.name
end

private def format_button_styles : String
"bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded"
end
Expand Down

0 comments on commit 6dc3400

Please sign in to comment.