Skip to content

Don't raise when a layout is set for only one of html/text - #65

Open
pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/single-format-layout
Open

pcbeingused333 wants to merge 1 commit into
Rails-Designer:mainfrom
pcbeingused333:fix/single-format-layout

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

The bug

class WelcomeEmail < Courrier::Email
  layout html: "<html>%{content}</html>"
  # ...
end

WelcomeEmail.new(from: "a@example.com", to: "b@example.com")
# => TypeError: wrong element type NilClass at 1 (expected array)
#    lib/courrier/email/layouts.rb:23:in `to_h`

Layouts#layouts maps [:html, :text] through to_h and nexts past a format with no layout:

FORMATS.map(&:to_sym).to_h do |format|
  template = @email.class.layouts[format]
  next if template.nil?
  [format, render(template)]
end

A next inside a to_h block yields nil, and to_h expects every block result to be a 2-element array, so it raises. The email fails in its constructor.

Every layout example in the README sets a single format:

layout text: "%{content}\n\nThanks for your order!"
layout html: :html_layout
layout html: OrderLayout

so this is the normal case.

The fix

filter_map { ... }.to_h — drop the missing format before building the hash. The wrap side (Options#wrap) already skips a missing format, so only the build path needed the guard.

Tests

Added test_html_only_layout and test_text_only_layout (with fixtures). Both raise TypeError on main and pass with the fix. bundle exec rake and bundle exec standardrb are green (161 runs, 0 failures).

🤖 Generated with Claude Code

`Layouts#layouts` maps `[:html, :text]` through `to_h { ... }` and `next`s
past a format that has no layout. A `next` inside a `to_h` block yields
`nil`, so `to_h` raises `TypeError: wrong element type NilClass`, and the
email blows up in its constructor.

Every `layout` example in the README sets a single format
(`layout text:`, `layout html:`), so this is the common case, not an edge
one. `filter_map { ... }.to_h` drops the missing format instead.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant