Module: Deface::ActionViewExtensions::DefacedTemplate

Defined in:
lib/deface/action_view_extensions.rb

Instance Method Summary collapse

Instance Method Details

#encode!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deface/action_view_extensions.rb', line 17

def encode!
  return super unless Rails.application.config.deface.enabled

  # Before Rails 6 encode! returns nil
  source = Deface.before_rails_6? ? (super; @source) : super
  syntax = Deface::ActionViewExtensions.determine_syntax(@handler)
  overrides = Deface::Override.find(
    locals: @locals,
    format: @format,
    variant: @variant,
    virtual_path: @virtual_path,
  )

  if syntax && overrides.any?
    # Prevents any caching by rails in development mode.
    @updated_at = Time.now if Deface.before_rails_6?
    @handler = ActionView::Template::Handlers::ERB

    # Modify the existing string instead of returning a copy
    new_source = Deface::Override.apply_overrides(
      Deface::Override.convert_source(source, syntax: syntax),
      overrides: overrides
    )

    if Deface.before_rails_6?
      @source.replace new_source
    else
      source.replace new_source
    end
  end

  source
end