Sha256: 514ae79f121a8bdd34f4a586742e2289eddfc1903f98f1cf4b6320117b4a94c3

Contents?: true

Size: 913 Bytes

Versions: 3

Compression:

Stored size: 913 Bytes

Contents

# This mechanism is used by 3rd party plugins.
# Normally you'd use partials from your own app
module ComfortableMexicanSofa::ViewHooks

  # Array of declared hooks
  def self.hooks
    @@hooks ||= { }
  end

  # Renders hook content
  def self.render(name, template, options = {})
    out = ''
    (self.hooks[name.to_sym] || []).each do |path|
      out += template.render({:partial => path.first}.merge(options))
    end
    return out.html_safe
  end

  # Will declare a partial that will be rendered for this hook
  # Example:
  # ComfortableMexicanSofa::ViewHooks.add(:navigation, 'shared/navigation')
  def self.add(name, partial_path, position = 0)
    self.hooks[name.to_sym] ||= []
    self.hooks[name.to_sym] << [partial_path, position]
    self.hooks[name.to_sym].sort_by! { |hook| hook.last }
  end

  # Removing previously declared hook
  def self.remove(name)
    self.hooks.delete(name)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-2.0.2 lib/comfortable_mexican_sofa/view_hooks.rb
comfortable_mexican_sofa-2.0.1 lib/comfortable_mexican_sofa/view_hooks.rb
comfortable_mexican_sofa-2.0.0 lib/comfortable_mexican_sofa/view_hooks.rb