Permalink
Browse files

Moved some helpers around

  • Loading branch information...
ddemaree committed Mar 18, 2009
1 parent a576a29 commit 8af1cd9060540b4db62aacbaf77ae963797a3cb4
View
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,20 @@
+module Practical
+ module Labeled
+ module FormBuilder
+
+ def method_missing_with_label(method_name, *args, &block)
+ if method_name.to_s =~ /^labeled_/
+ @template.send(method_name, @object_name, *args, &block)
+ else
+ method_missing_without_label(method_name,*args)
+ end
+ end
+
+ def field_wrapper(method_name,*args,&block)
+ @template.field_wrapper(@object_name, method_name, *args, &block)
+ end
+
+ end
+ end
+end
+
@@ -0,0 +1,80 @@
+module Practical
+ module Labeled
+ module FormHelper
+
+ def method_missing_with_label(method_name,*args,&block)
+ if method_name.to_s =~ /^labeled_/
+ options = args.extract_options!
+ label_text = options.delete(:label)
+ args << options
+
+ object_name = args.shift
+ method = args.shift
+
+ tag_name = (options.delete(:tag) || 'div')
+ field_html = send(method_name.to_s.sub(/^labeled_/,""), object_name, method,*args)
+ label_html = label(object_name, method, label_text, {})
+
+ if block_given?
+ concat(field_wrapper(object_name,method))
+ concat(label_html)
+ concat(content_tag(:p, field_html))
+ yield
+ concat("</#{tag_name}>")
+ else
+ content_tag(tag_name, "#{label_html} #{field_html}", :class => "field")
+ end
+ else
+ method_missing_without_label(method_name,*args)
+ end
+ end
+
+ def field_wrapper(object_name, method, options={}, &block)
+ tag_name = (options.delete(:tag) || 'div')
+
+ options.reverse_merge!({
+ :class => "field"
+ })
+
+ object = instance_variable_get("@#{object_name}")
+
+ if object.errors.on(method)
+ options[:class] << " hasErrors"
+ end
+
+ html_tag = tag(tag_name, options, true)
+
+ if block_given?
+ concat(html_tag)
+ yield
+
+ if object.respond_to?(:errors) && object.errors.on(method.to_sym)
+ concat(error_messages(object, method.to_sym))
+ end
+
+ concat("</#{tag_name}>")
+ else
+ html_tag
+ end
+ end
+
+ def error_messages(object, field)
+ return "" unless object.errors.on(field)
+ errors = Array(object.errors.on(field))
+
+ returning("") do |output|
+ output << tag(:ul, {:class => 'form-errors'}, true)
+
+ human_attr_name = object.class.human_attribute_name(field.to_s)
+
+ errors.each do |error|
+ output << content_tag(:li, "#{human_attr_name} #{error}")
+ end
+
+ output << '</ul>'
+ end
+ end
+
+ end
+ end
+end
@@ -1,61 +1,50 @@
-require 'action_view'
-require 'action_view/helpers'
+require 'practical/labeled/form_builder'
+require 'practical/labeled/form_helper'
-module ActionView
- module Helpers
- module FormHelper
-
- def method_missing_with_label(method_name,*args,&block)
- if method_name.to_s =~ /^labeled_/
- options = args.extract_options!
- label_text = options.delete(:label)
- args << options
+ActionView::Helpers::FormHelper.send(:include, Practical::Labeled::FormHelper)
+ActionView::Helpers::FormHelper.alias_method_chain :method_missing, :label
- object_name = args.shift
- method = args.shift
+ActionView::Base.send(:include, Practical::Labeled::FormHelper)
- label_tag = label(object_name, method, label_text)
- field_tag = send(method_name.to_s.sub(/^labeled_/,""), object_name, method,*args)
-
- if block_given?
- concat(tag(:div, {:class => 'field'}, true))
- concat(label_tag)
- concat(content_tag(:p, field_tag))
- yield
- concat('</div>')
- else
- content_tag(:p, "#{label_tag}\n#{field_tag}", :class => "field")
- end
- else
- method_missing_without_label(method_name,*args)
- end
- end
- alias_method_chain :method_missing, :label
+ActionView::Helpers::FormBuilder.send(:include, Practical::Labeled::FormBuilder)
+ActionView::Helpers::FormBuilder.alias_method_chain :method_missing, :label
- def field_with_label(object, field_name, options={}, &block)
- label_text = options.delete(:label)
- concat(tag(:div, {:class => 'field'}, true))
- concat(label(object,field_name,label_text))
- yield
- concat('</div>')
- end
- end
-
- class FormBuilder
- def method_missing_with_label(method_name, *args, &block)
- if method_name.to_s =~ /^labeled_/
- @template.send(method_name, @object, *args, &block)
- else
- method_missing_without_label(method_name,*args)
- end
- end
- alias_method_chain :method_missing, :label
- def field_with_label(method_name,*args,&block)
- @template.field_with_label(@object,method_name,*args,&block)
- end
-
- end
- end
-end
+# require 'action_view'
+# require 'action_view/helpers'
+#
+# module ActionView
+# module Helpers
+# module FormHelper
+#
+#
+# alias_method_chain :method_missing, :label
+#
+# def field_with_label(object, field_name, options={}, &block)
+# label_text = options.delete(:label)
+#
+# concat(tag(:div, {:class => 'field'}, true))
+# concat(label(object,field_name,label_text))
+# yield
+# concat('</div>')
+# end
+# end
+#
+# class FormBuilder
+# def method_missing_with_label(method_name, *args, &block)
+# if method_name.to_s =~ /^labeled_/
+# @template.send(method_name, @object, *args, &block)
+# else
+# method_missing_without_label(method_name,*args)
+# end
+# end
+# alias_method_chain :method_missing, :label
+#
+# def field_with_label(method_name,*args,&block)
+# @template.field_with_label(@object,method_name,*args,&block)
+# end
+#
+# end
+# end
+# end
@@ -1,3 +1,8 @@
# PracticalFormHelpers
-require 'practical/labeled_form_builder'
+require 'practical/labeled_form_builder'
+
+
+
+#ActionView::Helpers::FormHelper.send(:include, CalendarDateSelect::FormHelper)
+#ActionView::Base.send(:include, CalendarDateSelect::FormHelper)
@@ -1,6 +1,6 @@
require 'test_helper'
-class PracticalFormHelpersTest < ActiveSupport::TestCase
+class PracticalFormHelpersTest < ActionView::TestCase
# Replace this with your real tests.
test "the truth" do
assert true

0 comments on commit 8af1cd9

Please sign in to comment.