Sha256: b9c0260eb301fa9a5b3543628308e97f021de855c346d2f0c2b5234d6bc8a17b

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 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 errors(name)
        object.errors[name].to_a
      end

      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.6 lib/trestle/form/builder.rb
trestle-0.8.5 lib/trestle/form/builder.rb
trestle-0.8.4 lib/trestle/form/builder.rb