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



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
137
138
139
140
141
# File 'lib/fat_free_crm/callback.rb', line 110

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 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 = "".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