Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

AV extensions

  • Loading branch information...
commit 10d235170e24aac748a47a5f13a8a8d0e6ee6a68 1 parent ff4aaf4
@paul authored
Showing with 45 additions and 8 deletions.
  1. +4 −8 lib/mold.rb
  2. +41 −0 lib/mold/action_view_extensions/form_helper.rb
View
12 lib/mold.rb
@@ -1,16 +1,12 @@
-
-$LOAD_PATH.unshift File.dirname(__FILE__)
-
-require 'mold/util'
-require 'mold/builder'
-require 'mold/helpers'
+require 'actionview'
+require 'mold/action_view_extensions/form_helper'
module Mold
- Version = VERSION = "0.2.0"
+ Version = VERSION = "0.3.0"
def version
Version
end
-
+ autoload :FormBuilder, "mold/form_builder"
end
View
41 lib/mold/action_view_extensions/form_helper.rb
@@ -0,0 +1,41 @@
+module Mold
+ module ActionViewExtensions
+ module FormHelper
+
+ @@default_error_proc = nil
+
+ FIELD_ERROR_PROC = proc { |html_tag, _| html_tag }
+
+ def with_custom_field_error_proc(&block)
+ @@default_field_error_proc = ::ActionView::Base.field_error_proc
+ ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
+ result = yield
+ ::ActionView::Base.field_error_proc = @@default_field_error_proc
+ result
+ end
+
+ %w[form_for fields_for].each do |helper|
+ class_eval <<-METHOD, __FILE__, __LINE__
+ def mold_#{helper}(record_or_name_or_array, *args, &block)
+ options = args.extract_options!
+ options[:builder] = Mold::FormBuilder
+ css_class = case record_or_name_or_array
+ when String, Symbol then record_or_name_or_array.to_s
+ when Array then dom_class(record_or_name_or_array.last)
+ else dom_class(record_or_name_or_array)
+ end
+ options[:html] ||= {}
+ options[:html][:class] = "mold_form \#{css_class} \#{options[:html][:class]}".strip
+
+ with_custom_field_error_proc do
+ #{helper}(record_or_name_or_array, *(args << options), &block)
+ end
+ end
+ METHOD
+ end
+ end
+ end
+end
+
+::ActionView::Base.send :include, Mold::ActionViewExtensions::FormHelper
+
Please sign in to comment.
Something went wrong with that request. Please try again.