Permalink
Browse files

added MongoMapper-specific reflection capability

  • Loading branch information...
1 parent c6c6d2b commit e64a36a4973b770d7e6cdd1dafcbe59ea16b779a @ijonas ijonas committed Jun 20, 2011
View
6 lib/formtastic/helpers/reflection.rb
@@ -5,7 +5,11 @@ module Reflection
# If an association method is passed in (f.input :author) try to find the
# reflection object.
def reflection_for(method) #:nodoc:
- @object.class.reflect_on_association(method) if @object.class.respond_to?(:reflect_on_association)
+ if @object.class.respond_to?(:reflect_on_association)
+ @object.class.reflect_on_association(method)
+ elsif @object.class.respond_to?(:associations) # MongoMapper uses the 'associations(method)' instead
+ @object.class.associations(method)
+ end
end
def association_macro_for_method(method) #:nodoc:
View
2 spec/helpers/inputs_helper_spec.rb
@@ -566,7 +566,7 @@ def generate_form
end
end
it "should throw PolymorphicInputWithoutCollectionError on sub_posts" do
- ::MongoPost.should_receive(:associations).exactly(3).times
+ ::MongoPost.should_receive(:associations).at_least(3).times
expect { generate_form }.to raise_error(Formtastic::PolymorphicInputWithoutCollectionError)
end
end
View
32 spec/helpers/reflection_helper_spec.rb
@@ -0,0 +1,32 @@
+# encoding: utf-8
+require 'spec_helper'
+
+describe 'Formtastic::Helpers::Reflection' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ class ReflectionTester
+ include Formtastic::Helpers::Reflection
+ def initialize(model_object)
+ @object = model_object
+ end
+ end
+
+ context 'with an ActiveRecord object' do
+ it "should return association details on an ActiveRecord association" do
+ @reflection_tester = ReflectionTester.new(@new_post)
+ @reflection_tester.reflection_for(:sub_posts).should_not be_nil
+ end
+ it "should return association details on a MongoMapper association" do
+ @reflection_tester = ReflectionTester.new(@new_mm_post)
+ @reflection_tester.reflection_for(:sub_posts).should_not be_nil
+ end
+ end
+
+
+end

0 comments on commit e64a36a

Please sign in to comment.