Module: MaquinaNewsletters::ApplicationHelper

Defined in:
app/helpers/maquina_newsletters/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#icon_for(name, **options) ⇒ Object



107
108
109
110
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 107

def icon_for(name, **options)
  css = options[:class]
  (:span, name.to_s, class: ["icon", css].compact.join(" "), data: {icon: name})
end

#newsletter_attachment_url_envObject

Rack env (http_host/https) used to resolve Active Storage attachment URLs when rendering a body. Prefers the live request; falls back to the mailer’s default_url_options when rendering an email (no request). Returns {} when neither is available — the host must set default_url_options for emails.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 62

def newsletter_attachment_url_env
  req = request if respond_to?(:request)
  if req.try(:host).present?
    {http_host: req.host_with_port, https: req.ssl?}
  else
    opts = ::ActionMailer::Base.default_url_options || {}
    return {} if opts[:host].blank?

    host = opts[:port] ? "#{opts[:host]}:#{opts[:port]}" : opts[:host]
    {http_host: host, https: opts[:protocol].to_s == "https"}
  end
end

#newsletter_body(newsletter) ⇒ Object

Renders a newsletter’s rich-text body for display.

Action Text attachments backed by Active Storage render through the host’s active_storage/blobs/_blob partial, which calls ‘image_tag blob.representation(…)`. That resolves the variant into a URL via `polymorphic_url`, which only works against a route set that defines the rails_blob/representation routes — the HOST application’s. During a request Action Text renders content with the current controller’s view context as its renderer; because this engine is ‘isolate_namespace`d that context uses the ENGINE’s route set, where those routes don’t exist, so the render raises “Can’t resolve image into URL: undefined method ‘to_model’”.

Action Text’s own ‘action_controller_renderer` is built on a bare ActionController::Base, whose routes are the application’s. Rendering the body through it makes attachment URLs resolve correctly.

That renderer has no incoming request, so without an explicit host it builds URLs against ActionDispatch’s placeholder host (“example.org”), which breaks the images. We give it the right host per context: the live request’s host on the web, and the mailer’s configured host (default_url_options) in emails — so the show page uses the real host and delivered emails get absolute URLs.



48
49
50
51
52
53
54
55
56
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 48

def newsletter_body(newsletter)
  renderer = ActionText::Content.action_controller_renderer
  env = newsletter_attachment_url_env
  renderer = renderer.new(env) if env.present?

  ActionText::Content.with_renderer(renderer) do
    newsletter.body.to_s.html_safe
  end
end

#newsletter_status_label(status) ⇒ Object

Localized display label for a newsletter status (Newsletter::STATUSES).



21
22
23
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 21

def newsletter_status_label(status)
  t("maquina_newsletters.statuses.#{status}")
end

#newsletter_time_optionsObject

Time-of-day options for the schedule dropdown: 8:00 AM to 8:00 PM in 30-minute steps. Returns [label, value] pairs (“8:00 AM” => “08:00”) for options_for_select / f.select; the value is the HH:MM stored as scheduled_time and composed into scheduled_at.



79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 79

def newsletter_time_options
  (480..1200).step(30).map do |minutes|
    hour, minute = minutes.divmod(60)
    value = format("%02d:%02d", hour, minute)
    period = (hour < 12) ? "AM" : "PM"
    hour12 = hour % 12
    hour12 = 12 if hour12.zero?
    ["#{hour12}:#{format("%02d", minute)} #{period}", value]
  end
end

#status_badge_variant(newsletter) ⇒ Object

Maps a newsletter’s status to a maquina_components badge variant. Per spec §4 / theming knowledge:

draft               -> :secondary
approved/scheduled  -> :default
sending             -> :warning
sent                -> :success
failed              -> :destructive


10
11
12
13
14
15
16
17
18
# File 'app/helpers/maquina_newsletters/application_helper.rb', line 10

def status_badge_variant(newsletter)
  case newsletter.status
  when "sent" then :success
  when "sending" then :warning
  when "failed" then :destructive
  when "approved", "scheduled" then :default
  else :secondary
  end
end