Permalink
Browse files

add the new gem files so it now only has the field method

  • Loading branch information...
1 parent 3b7ea26 commit 0e536a84300c26fb03eacb0ec4b866cb39b40499 @23inhouse committed Feb 21, 2012
Showing with 84 additions and 10 deletions.
  1. +1 −0 erb_form.gemspec
  2. +2 −10 lib/erb_form.rb
  3. +81 −0 lib/erb_form/builder.rb
View
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
+ s.add_dependency('simple_form', '>=2.0.0.rc')
end
View
@@ -1,18 +1,10 @@
require "erb_form/version"
-require 'action_view'
-require 'erb_form/template'
-require 'erb_form/action_view_extensions/builder'
-require 'erb_form/action_view_extensions/form_helper'
+require 'simple_form'
+require 'erb_form/builder'
module ErbForm
# Default form and report template paths
mattr_accessor :forms_path
@@forms_path = 'forms'
-
- # Default way to setup ErbForm. Run rails generate erb_form:install
- # to create a fresh initializer with all configuration values.
- def self.setup
- yield self
- end
end
@@ -0,0 +1,81 @@
+module ErbForm
+ class Builder < ::SimpleForm::FormBuilder
+ def field(attribute_name, options = {})
+ raise ErbForm::DoubleRenderError.new(clean_backtrace(caller)), nil, clean_backtrace(caller) if recursing?
+ @prevent_recursion = true
+ @field_layout = options.delete(:layout)
+ return render_field(field_template_path(attribute_name), attribute_name, options)
+ rescue ActionView::MissingTemplate => e
+ raise ErbForm::MissingTemplate.new(field_layouts(attribute_name)), nil, clean_backtrace(e.backtrace)
+ end
+
+ private
+
+ def recursing?
+ !!@prevent_recursion
+ end
+
+ def field_layouts(attribute_name)
+ file = @field_layout.nil? ? attribute_name.to_s : @field_layout
+ [
+ [ErbForm.forms_path, @field_layout, attribute_name.to_s+'_field'], # forms/custom_layout/attribute_name_field.html.erb
+ [template.controller_path, file+'_field'], # resourse_path/attribute_name_field.html.erb
+ [ErbForm.forms_path, @field_layout, 'field'], # forms/custom_layout/field.html.erb
+ [ErbForm.forms_path, 'default', file+'_field'], # forms/attribute_name_field.html.erb
+ [ErbForm.forms_path, 'default', 'field'] # forms/attribute_name/field.html.erb
+ ].delete_if { |template_array|
+ template_array.include? nil
+ }.map { |template_array|
+ template_array.join('/')
+ }
+ end
+
+ def render_field(field_template_path, attribute_name, options)
+ output = template.render(:file => '/' + (field_template_path || 'a_non_existant_file_to_force_a_missing_template_error'), :locals => {
+ :form => self,
+ :attribute_name => attribute_name,
+ :options => options
+ })
+ @prevent_recursion = false
+ output
+ end
+
+ def field_template_path(attribute_name)
+ @field_template_path ||= field_layouts(attribute_name).detect { |template_file|
+ template.view_paths.exists?(template_file, '', false, {
+ :locale => [template.locale],
+ :formats => template.formats,
+ :handlers => [:erb, :rjs, :builder, :rhtml, :rxml]
+ })
+ }
+ end
+
+ def clean_backtrace(backtrace)
+ unless backtrace[0].scan(__FILE__).size > 0
+ backtrace = backtrace.collect { |line| line.scan(__FILE__).size > 0 ? nil : line }.compact!
+ end
+ end
+ end
+
+ class DoubleRenderError < StandardError;
+ def initialize(caller)
+ @caller = caller
+ end
+
+ def message
+ %(Called `field' from #{@caller.first})
+ end
+ end
+
+ class MissingTemplate < StandardError;
+ def initialize(failed_templates)
+ @failed_templates = failed_templates
+ end
+
+ def message
+ %(Missing Template, tried:\n - #{@failed_templates.join("\n - ")})
+ end
+ end
+end
+
+ActionView::Base.default_form_builder = ErbForm::Builder

0 comments on commit 0e536a8

Please sign in to comment.