class Object

Public Class Methods

new(source, identifier, handler, details) click to toggle source
# File lib/deface/action_view_extensions.rb, line 4
def initialize(source, identifier, handler, details)
  syntax = determine_syntax(handler)

  if Rails.application.config.deface.enabled && should_be_defaced?(syntax)

    processed_source = Deface::Override.apply(source, details, true, syntax)

    # force change in handler before continuing to original Rails method
    # as we've just converted some other template language into ERB!
    #
    if [:slim, :haml].include?(syntax) && processed_source != source
      handler = ActionView::Template::Handlers::ERB
    end
  else
    processed_source = source
  end

  initialize_without_deface(processed_source, identifier, handler, details)
end

Public Instance Methods

determine_syntax(handler) click to toggle source
# File lib/deface/action_view_extensions.rb, line 64
def determine_syntax(handler)
  if handler.to_s == "Haml::Plugin"
    :haml
  elsif handler.class.to_s == "Slim::RailsTemplate"
    :slim
  elsif handler.to_s.demodulize == "ERB" || handler.class.to_s.demodulize == "ERB"
    :erb
  else
    :unknown
  end
end
method_name() click to toggle source

inject deface hash into compiled view method name used to determine if recompilation is needed

# File lib/deface/action_view_extensions.rb, line 51
def method_name
  deface_hash = Deface::Override.digest(:virtual_path => @virtual_path)

  #we digest the whole method name as if it gets too long there's problems
  "_#{Deface::Digest.hexdigest("#{deface_hash}_#{method_name_without_deface}")}"
end
render(view, locals, buffer=nil, &block) click to toggle source

refresh view to get source again if view needs to be recompiled

# File lib/deface/action_view_extensions.rb, line 29
def render(view, locals, buffer=nil, &block)

  if view.is_a?(ActionView::CompiledTemplates)
    mod = ActionView::CompiledTemplates
  else
    mod = view.singleton_class
  end

  if @compiled && !mod.instance_methods.map(&:to_s).include?(method_name)
    @compiled = false
    @source = refresh(view).source
  end
  render_without_deface(view, locals, buffer, &block)
end
should_be_defaced?(syntax) click to toggle source
# File lib/deface/action_view_extensions.rb, line 60
def should_be_defaced?(syntax)
  syntax != :unknown
end