Permalink
Browse files

Fix for #740 - Mongoid belongs_to select raises NoMethodError and alw…

…ays returns empty results
  • Loading branch information...
1 parent ce77365 commit a09a6db9b3f1fab5185a4a0ee8c67ebafdb00b9b @rgarner committed Nov 11, 2011
Showing with 24 additions and 5 deletions.
  1. +9 −4 lib/formtastic/inputs/base/collections.rb
  2. +15 −1 spec/spec_helper.rb
@@ -54,18 +54,23 @@ def collection_from_options
def collection_from_association
if reflection
- raise PolymorphicInputWithoutCollectionError.new("A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations.") if reflection.options && reflection.options[:polymorphic] == true
+ if reflection.respond_to?(:options)
+ raise PolymorphicInputWithoutCollectionError.new(
+ "A collection must be supplied for #{method} input. Collections cannot be guessed for polymorphic associations."
+ ) if reflection.options[:polymorphic] == true
+ end
find_options_from_options = options[:find_options] || {}
conditions_from_options = find_options_from_options[:conditions] || {}
- conditions_from_reflection = reflection.options && reflection.options[:conditions] || {}
+ conditions_from_reflection = (reflection.respond_to?(:options) && reflection.options[:conditions]) || {}
conditions_from_reflection = conditions_from_reflection.call if conditions_from_reflection.is_a?(Proc)
+ scope_conditions = conditions_from_reflection.empty? ? nil : {:conditions => conditions_from_reflection}
if conditions_from_options.any?
- reflection.klass.scoped(:conditions => conditions_from_reflection).where(conditions_from_options)
+ reflection.klass.scoped(scope_conditions).where(conditions_from_options)
else
find_options_from_options.merge!(:include => group_by) if self.respond_to?(:group_by) && group_by
- reflection.klass.scoped(:conditions => conditions_from_reflection).where(find_options_from_options)
+ reflection.klass.scoped(scope_conditions).where(find_options_from_options)
end
end
end
View
@@ -137,6 +137,18 @@ class ::PostModel
include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
end
+ ##
+ # We can't mock :respond_to?, so we need a concrete class override
+ class ::MongoidReflectionMock < RSpec::Mocks::Mock
+ def initialize(name=nil, stubs_and_options={})
+ super name, stubs_and_options
+ end
+
+ def respond_to?(sym)
+ sym == :options ? false : super
+ end
+ end
+
def _routes
url_helpers = mock('url_helpers')
url_helpers.stub!(:hash_for_posts_path).and_return({})
@@ -282,7 +294,9 @@ def new_author_path(*args); "/authors/new"; end
when :main_post
mock('reflection', :options => {}, :klass => ::Post, :macro => :belongs_to)
when :mongoid_reviewer
- mock('reflection', :options => nil, :klass => ::Author, :macro => :referenced_in, :foreign_key => "reviewer_id") # custom id
+ ::MongoidReflectionMock.new('reflection',
+ :options => Proc.new { raise NoMethodError, "Mongoid has no reflection.options" },
+ :klass => ::Author, :macro => :referenced_in, :foreign_key => "reviewer_id") # custom id
end
end
::Post.stub!(:find).and_return([@freds_post])

0 comments on commit a09a6db

Please sign in to comment.