Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  [:form_for, :remote_form_for].each do |method|
    module_eval <<-end_src, __FILE__, __LINE__
      def cute_#{method}(object_name, *args, &proc)
        options = args.last.is_a?(Hash) ? args.pop : {}
        options.update(:builder => CuteFormBuilder)
        #{method}(object_name, *(args << options), &proc)
      end
    end_src
  end

  class CuteFormBuilder < ActionView::Helpers::FormBuilder
    include ActionView::Helpers::TextHelper
    def fieldset(legend, &block)
      raise ArgumentError, "Missing block" unless block_given?
      concat("<fieldset><legend>#{legend}</legend><ul>", proc.binding)
      yield
      concat('</ul></fieldset>', proc.binding)
    end
    def label(info, method, &block)
      raise ArgumentError, "Missing block" unless block_given?
      concat(label_top(info, method), proc.binding)
      yield
      concat('</li>', proc.binding)
    end
    [:password_field, :text_field, :text_area].each do |method|
      class_eval <<-end_src, __FILE__, __LINE__
        def #{method}(info, method, options={})
          label_string(info, method) do
            super(method, options)
          end
        end
      end_src
    end
    def submit(value)
      %(<fieldset class="submit"><input class="submit" type="submit" value="#{value}" /></fieldset>)
    end
    private
    def label_string(info, method, &block)
      raise ArgumentError, "Missing block" unless block_given?
      label_top(info, method) + yield + '</li>'
    end
    def label_top(info, method)
      str = %(<li><label for="#{self.object.class.to_s.underscore}_#{method.to_s.underscore}">#{info})
      err = self.object.errors.on(method)
      str += "<strong>"+(err.is_a?(Array) ? "hej" : "eh?")+"</strong>" if err
      str + "</label>"
    end
  end