Module: FatFreeCRM::Callback::Helper

Defined in:
lib/fat_free_crm/callback.rb

Overview

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


Instance Method Summary collapse

Instance Method Details

#hook(method, caller, context = {}, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fat_free_crm/callback.rb', line 109

def hook(method, caller, context = {}, &block)
  is_view_hook = caller.is_haml?

  # If a block was given, hooks are able to replace, append or prepend view content.
  if block_given? and is_view_hook
    hooks = FatFreeCRM::Callback.view_hook(method, caller, context)
    # Add content to the view in the following order:
    # -- before
    # -- replace || original block
    # -- after
    view_data = []
    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 << capture(&block)
    else
      hooks[:replace].each{|data| view_data << data }
    end
    hooks[:after].each{|data| view_data << data }

    view_data.join.html_safe

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