lib/matchers/indexes.rb in mongoid-rspec-1.13.0 vs lib/matchers/indexes.rb in mongoid-rspec-2.0.0.rc1
- old
+ new
@@ -11,16 +11,34 @@
end
def matches?(klass)
@klass = klass.is_a?(Class) ? klass : klass.class
@errors = []
- unless @klass.index_options[@index_fields]
- @errors.push "no index for #{@index_fields}"
+
+ if @klass.respond_to?(:index_options)
+ # Mongoid 3
+ unless @klass.index_options[@index_fields]
+ @errors.push "no index for #{@index_fields}"
+ else
+ if !@options.nil? && !@options.empty?
+ @options.each do |option, option_value|
+ if denormalising_options(@klass.index_options[@index_fields])[option] != option_value
+ @errors.push "index for #{@index_fields.inspect} with options of #{@klass.index_options[@index_fields].inspect}"
+ end
+ end
+ end
+ end
else
- if !@options.nil? && !@options.empty?
- @options.each do |option, option_value|
- if denormalising_options(@klass.index_options[@index_fields])[option] != option_value
- @errors.push "index for #{@index_fields.inspect} with options of #{@klass.index_options[@index_fields].inspect}"
+ # Mongoid 4
+ unless @klass.index_specifications.map(&:key).include?(@index_fields)
+ @errors.push "no index for #{@index_fields}"
+ else
+ if !@options.nil? && !@options.empty?
+ index_options = @klass.index_specifications.select { |is| is.key == @index_fields }.first.options
+ @options.each do |option, option_value|
+ if index_options[option] != option_value
+ @errors.push "index for #{@index_fields.inspect} with options of #{index_options.inspect}"
+ end
end
end
end
end