Sha256: be7de0053164baed4067fe64ad108eabf1d5f9ba21a80bed803f22d93ed4f24e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Markaby
  module Rails
    class RailsBuilder < Markaby::Builder
      def form_for(*args, &block)
        @template.form_for(*args) do |__form_for_variable|
          yield(FormHelperProxy.new(self, __form_for_variable))
        end
      end

      # This is used for the block variable given to form_for.  Typically, an erb template looks as so:
      #
      #   <% form_for :foo do |f|
      #     <%= f.text_field :bar %>
      #   <% end %>
      #
      # form_for adds the form tag to the input stream, and assumes that later the user will append
      # the <input> tag to the input stream himself (in erb, this is done with the <%= %> tags).
      #
      # We could do the following in Markaby:
      #
      #   form_for :foo do |f|
      #     text f.text_field(:bar)
      #   end
      #
      # But this is ugly.  This is prettier:
      #
      #   form_for :foo do |f|
      #     f.text_field :bar
      #   end
      class FormHelperProxy
        def initialize(builder, proxied_object)
          @builder = builder
          @proxied_object = proxied_object
        end

        def respond_to?(sym, include_private = false)
          @proxied_object.respond_to?(sym, include_private) || super
        end

      private

        def method_missing(sym, *args, &block)
          result = @proxied_object.__send__(sym, *args, &block)
          @builder.text(result) if result.is_a?(String)
          result
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markaby-0.6.6 lib/markaby/rails/rails_builder.rb