module FatFreeCRM::Callback::Helper

This makes it possible to call hook() without FatFreeCRM::Callback prefix. Returns stringified data when called from within templates, and the actual data otherwise.

Public Instance Methods

hook(method, caller, context = {}, &block) click to toggle source
# File lib/fat_free_crm/callback.rb, line 110
def hook(method, caller, context = {}, &block)

  # In a view template context, hooks are able to replace, append or prepend content.
  if caller.is_a?(ActionView::Base)
    hooks = FatFreeCRM::Callback.view_hook(method, caller, context)
    # Add content to the view in the following order:
    # -- before
    # -- replace || original block
    # -- after
    view_data = "".html_safe
    hooks[:before].each { |data| view_data << data }
    # Only render the original view block if there are no pending :replace operations
    if hooks[:replace].empty?
      view_data << if block_given?
                     capture(&block)
                   else
                     # legacy view hooks
                     FatFreeCRM::Callback.hook(method, caller, context)
        end
    else
      hooks[:replace].each { |data| view_data << data }
    end
    hooks[:after].each { |data| view_data << data }

    view_data

  else
    # Hooks called without blocks are either controller or legacy view hooks
    FatFreeCRM::Callback.hook(method, caller, context)
  end
end