Sha256: 987cf7091f28f6bc6211d86eb8bc3c5304c318995800ad22e481f07c998eff5b

Contents?: true

Size: 1.97 KB

Versions: 28

Compression:

Stored size: 1.97 KB

Contents

module Spree
  module ThemeSupport

    # Listener class used for views hooks.
    class HookListener
      include Singleton

      attr_accessor :hook_modifiers

      def initialize
        @hook_modifiers = []
      end

      def modifiers_for_hook(hook_name)
        hook_modifiers.select{|hm| hm.hook_name == hook_name}
      end


      # Replace contents of hook_name using supplied render args or string returned from block
      def self.replace(hook_name, options = {}, &block)
        add_hook_modifier(hook_name, :replace, options, &block)
      end

      # Insert before existing contents of hook_name using supplied render args or string returned from block
      def self.insert_before(hook_name, options = {}, &block)
        add_hook_modifier(hook_name, :insert_before, options, &block)
      end

      # Insert after existing contents of hook_name using supplied render args or string returned from block
      def self.insert_after(hook_name, options = {}, &block)
        add_hook_modifier(hook_name, :insert_after, options, &block)
      end

      # Clear contents of hook_name
      def self.remove(hook_name)
        add_hook_modifier(hook_name, :replace)
      end


      private

        def self.add_hook_modifier(hook_name, action, options = {}, &block)
          if block
            renderer = lambda do |template, locals|
              template.controller.render_to_string(:inline => yield, :locals => locals)
            end
          else
            if options.empty?
              renderer = nil
            else
              renderer = lambda do |template, locals|
                render_args = [options]
                if options.is_a?(Hash)
                  options[:locals] = locals
                else
                  render_args << locals
                end
                template.render(*render_args)
              end
            end
          end
          instance.hook_modifiers << HookModifier.new(hook_name, action, renderer)
        end

    end

  end
end

Version data entries

28 entries across 28 versions & 6 rubygems

Version Path
apispree_core-0.0.0 lib/spree_core/theme_support/hook_listener.rb
My-Commerce_core-1.1.0 lib/spree_core/theme_support/hook_listener.rb
My-Commerce_core-1.0.0 lib/spree_core/theme_support/hook_listener.rb
MyCommerceapi-1.0.0 core/lib/spree_core/theme_support/hook_listener.rb
MyCommerce-0.0.3 core/lib/spree_core/theme_support/hook_listener.rb
rfcommerce_core-0.0.3 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.6 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.5 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.30.2 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.40.4 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.50.4 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.4 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.50.3 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.3 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.2 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.1 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.0 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.60.0.RC1 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.50.2 lib/spree_core/theme_support/hook_listener.rb
spree_core-0.50.1 lib/spree_core/theme_support/hook_listener.rb