Sha256: 8aaa475f96d970901983e68090520493260f85d9c58f5cb936da1a5bd00b5894

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module Trestle
  class Form
    class Builder < ActionView::Helpers::FormBuilder
      # The #display method is defined on Kernel. Undefine it so that the
      # Builder instance will not respond_to?(:display) allowing the method to
      # be dispatched to the template helpers instead.
      undef_method :display

      cattr_accessor :fields
      self.fields = {}

      def self.register(name, klass)
        rename_existing_helper_method(name)
        self.fields[name] = klass
      end

    protected
      def respond_to_missing?(name, include_all=false)
        self.class.fields.has_key?(name) || super
      end

      def method_missing(name, *args, &block)
        if field = self.class.fields[name]
          field.new(self, @template, *args, &block).render
        else
          super
        end
      end

      def self.rename_existing_helper_method(name)
        # Check if a method exists with the given name
        return unless method_defined?(name)

        # Prevent a method from being aliased twice
        return if method_defined?(:"raw_#{name}")

        alias_method :"raw_#{name}", name
        undef_method name
      end
    end
  end
end

# Load all form fields
Trestle::Form::Fields.eager_load!

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.8.3 lib/trestle/form/builder.rb
trestle-0.8.2 lib/trestle/form/builder.rb
trestle-0.8.0 lib/trestle/form/builder.rb