Sha256: 27dc95faea1c951168e2c1e02b262f3010df49f5abf867bfa58447846b79fbee

Contents?: true

Size: 857 Bytes

Versions: 1

Compression:

Stored size: 857 Bytes

Contents

require 'activesupport'
require 'action_view'

module BlockHelpers
  
  class BlockHelper
    
    def self.inherited(klass)
      # Define the helper method
      # e.g. for a class:
      #   class HelloHelper < BlockHelpers::BlockHelper
      #     #.....
      #   end
      #
      # then we define a helper method 'hello_helper'
      #
      method_name = klass.name.split('::').last.underscore
      klass.parent.class_eval %(      
        def #{method_name}(*args, &block)
          renderer = #{klass.name}.new(*args)
          if renderer.public_methods(false).include? 'to_s'
            concat renderer.to_s(capture(renderer, &block))
          else
            block.call(renderer)
          end
        end
      )
      klass.class_eval do
        include ActionView::Helpers
        include klass.parent
      end
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markevans-block_helpers-0.2.0 lib/block_helpers.rb