Permalink
Browse files

get right input class when inheriting form builders

* this unfortunately does not work on Ruby 1.8
  when cache_classes is on
  • Loading branch information...
1 parent ae1ddfb commit 10cd0e5de27900955f5b23b4689335789be944bc @mikz committed Feb 21, 2013
Showing with 19 additions and 10 deletions.
  1. +6 −9 lib/formtastic/helpers/input_helper.rb
  2. +13 −1 spec/helpers/input_helper_spec.rb
@@ -324,15 +324,15 @@ def input_class(as)
Rails.application.config.cache_classes ? input_class_with_const_defined(as) : input_class_by_trying(as)
end
end
-
+
# prevent exceptions in production environment for better performance
def input_class_with_const_defined(as)
input_class_name = custom_input_class_name(as)
if ::Object.const_defined?(input_class_name)
input_class_name.constantize
elsif input_class_namespace.const_defined?(input_class_name)
- namespaced_input_class_name(as).constantize
+ input_class_namespace.const_get(input_class_name)
elsif Formtastic::Inputs.const_defined?(input_class_name)
standard_input_class_name(as).constantize
else
@@ -342,8 +342,10 @@ def input_class_with_const_defined(as)
# use auto-loading in development environment
def input_class_by_trying(as)
- input_class = begin; custom_input_class_name(as).constantize; rescue NameError; end
- input_class ||= begin; namespaced_input_class_name(as).constantize; rescue NameError; end
+ input_class_name = custom_input_class_name(as)
+
+ input_class = begin; input_class_name.constantize; rescue NameError; end
+ input_class ||= begin; input_class_namespace.const_get(input_class_name); rescue NameError; end
input_class ||= begin; standard_input_class_name(as).constantize; rescue NameError; end
input_class or raise Formtastic::UnknownInputError, "Unable to find input class for #{as}"
@@ -359,11 +361,6 @@ def custom_input_class_name(as)
input_class_name(as)
end
- # :as => :string # => Formtastic::FormBuilder::StringInput
- def namespaced_input_class_name(as)
- "#{input_class_namespace}::#{input_class_name(as)}"
- end
-
# :as => :string # => Formtastic::Inputs::StringInput
def standard_input_class_name(as)
"Formtastic::Inputs::#{input_class_name(as)}"
@@ -888,7 +888,7 @@ def length_should_be_required(options)
let(:output) { mock('input', :to_html => 'some HTML') }
let(:helper) { builder.extend(Formtastic::Helpers::InputHelper) }
- let(:custom_form_builder) { stub_const('CustomFormBuiler', Module.new) }
+ let(:custom_form_builder) { stub_const('CustomFormBuiler', Class.new) }
let(:namespaced_class) { stub_const("#{custom_form_builder}::StringInput", mock('namespaced class')) }
let(:top_level_class) { stub_const('StringInput', mock('top level class')) }
@@ -918,6 +918,18 @@ def length_should_be_required(options)
input.should == output.to_html
end
+
+ context "and custom form builder is inherited twice" do
+ let(:inherited_custom_form_builder) { stub_const('InhertitedCustomFormBuilder', Class.new(custom_form_builder)) }
+ it 'should instantitate namespaced class' do
+ namespaced_class.should_receive(:new).and_return(output)
+ formtastic_class.should_not_receive(:new)
+
+ helper.should_receive(:class){ inherited_custom_form_builder }.at_least(:once)
+
+ input.should == output.to_html
+ end
+ end
end
context 'when a customized top-level class exists' do

0 comments on commit 10cd0e5

Please sign in to comment.