Sha256: 449c9c364722526cabaa8e0c20606c067572f6526a8a7bd5391079b488935fe4

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

module AutoError
  class HelperContext
    attr_accessor :env

    def initialize( env )
      @env = env

      AutoError::Config.helpers.each do |mod_name|
        # grab the module by name we were given
        # it is available in the 'parent' Rails.application
        mod = Rails.application.class.qualified_const_get(mod_name)

        class_eval do
          # include that module in this HelperContext
          send( :include, mod )

          # but go through all of its instance methods
          # and alias-method-chain style twiddle them so we can bind
          # them to the correct environment inside auto_error
          mod.instance_methods.each do |imeth|
            alias :"#{imeth}_without_env" :"#{imeth}"
            send( :define_method, :"#{imeth}" ) do |*args|
              method( :"#{imeth}_without_env" ).to_proc.bind( @env ).call(*args)
            end
          end

          # also include rails helpers and ActionView helpers
          send( :include, ActionView::Helpers )
          send( :include, Rails.application.routes.url_helpers )
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
auto_error-0.0.14 lib/auto_error/helper_context.rb
auto_error-0.0.13 lib/auto_error/helper_context.rb
auto_error-0.0.12 lib/auto_error/helper_context.rb
auto_error-0.0.10 lib/auto_error/helper_context.rb
auto_error-0.0.9 lib/auto_error/helper_context.rb
auto_error-0.0.8 lib/auto_error/helper_context.rb
auto_error-0.0.7 lib/auto_error/helper_context.rb
auto_error-0.0.6 lib/auto_error/helper_context.rb
auto_error-0.0.5 lib/auto_error/helper_context.rb
auto_error-0.0.4 lib/auto_error/helper_context.rb