Module: Glib::TimeFreezableMailer

Defined in:
lib/glib/time_freezable_mailer.rb

Constant Summary collapse

@@__mailer_freeze_time =
false

Instance Method Summary collapse

Instance Method Details

#allow_time_freeze?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/glib/time_freezable_mailer.rb', line 5

def allow_time_freeze?
  Rails.env.development? || Rails.env.test?
end

#freeze_time(year, month, day) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/glib/time_freezable_mailer.rb', line 18

def freeze_time(year, month, day)
  return unless allow_time_freeze?

  if @@__mailer_freeze_time
    example_code = %{
      Rails.application.config.to_prepare do
        class ::Rails::MailersController
          prepend Glib::TimeReturningMailer
        end
      end
    }

    raise "Please create `config/initializers/mailer_preview_setup.rb` and add the following: #{example_code}"
  end

  @@__mailer_freeze_time = true
  Timecop.freeze(DateTime.new(year, month, day))
end

#return_time_if_frozenObject



9
10
11
12
13
14
15
16
# File 'lib/glib/time_freezable_mailer.rb', line 9

def return_time_if_frozen
  return unless allow_time_freeze?
  return unless @@__mailer_freeze_time

  @@__mailer_freeze_time = false

  Timecop.return # Allows mailer preview to use Timecop.freeze()
end