lib/arbre/rails/forms.rb in arbre-1.0.0.rc4 vs lib/arbre/rails/forms.rb in arbre-1.0.0

- old
+ new

@@ -1,92 +1,95 @@ -module SplitOpenAndCloseOn +module Arbre + module Rails + module Forms - def split_open_and_close_on(string, match) - return "" unless string && match - @opening_tag = string.split(Regexp.new("#{match}\\z")).first - @closing_tag = match - end + class FormBuilderProxy < Arbre::Component + attr_reader :form_builder - def opening_tag - @opening_tag || "" - end + # Since label and select are Arbre Elements already, we must + # override it here instead of letting method_missing + # deal with it + def label(*args) + proxy_call_to_form :label, *args + end - def closing_tag - @closing_tag || "" - end + def select(*args) + proxy_call_to_form :select, *args + end -end + def respond_to?(name) + if form_builder && form_builder.respond_to?(name) + true + else + super + end + end -class FormBuilderProxy < Arbre::Component - attr_reader :form_builder + private - # Since label and select are Arbre Elements already, we must - # override it here instead of letting method_missing - # deal with it - def label(*args) - proxy_call_to_form :label, *args - end + def proxy_call_to_form(method, *args, &block) + text_node form_builder.send(method, *args, &block) + end - def select(*args) - proxy_call_to_form :select, *args - end + def method_missing(method, *args, &block) + if form_builder && form_builder.respond_to?(method) + proxy_call_to_form(method, *args, &block) + else + super + end + end - def respond_to?(name) - if form_builder && form_builder.respond_to?(name) - true - else - super - end - end + end - private + class FormForProxy < FormBuilderProxy + builder_method :form_for - def proxy_call_to_form(method, *args, &block) - text_node form_builder.send(method, *args, &block) - end + def build(resource, form_options = {}, &block) + form_string = helpers.form_for(resource, form_options) do |f| + @form_builder = f + end - def method_missing(method, *args, &block) - if form_builder && form_builder.respond_to?(method) - proxy_call_to_form(method, *args, &block) - else - super - end - end + @opening_tag, @closing_tag = split_string_on(form_string, "</form>") + super(&block) + end -end + def fields_for(*args, &block) + insert_tag FieldsForProxy, form_builder, *args, &block + end -class FormForProxy < FormBuilderProxy - builder_method :form_for - include SplitOpenAndCloseOn + def split_string_on(string, match) + return "" unless string && match + part_1 = string.split(Regexp.new("#{match}\\z")).first + [part_1, match] + end - def build(resource, form_options = {}, &block) - form_string = helpers.form_for(resource, form_options) do |f| - @form_builder = f - end + def opening_tag + @opening_tag || "" + end - split_open_and_close_on form_string, "</form>" + def closing_tag + @closing_tag || "" + end - super(&block) - end + end - def fields_for(*args, &block) - insert_tag FieldsForProxy, form_builder, *args, &block - end + class FieldsForProxy < FormBuilderProxy -end + def build(form_builder, *args, &block) + form_builder.fields_for(*args) do |f| + @form_builder = f + end -class FieldsForProxy < FormBuilderProxy + super(&block) + end - def build(form_builder, *args, &block) - form_builder.fields_for(*args) do |f| - @form_builder = f - end + def to_s + children.to_s + end - super(&block) - end + end - def to_s - children.to_s + end end - end +