lib/matchers/associations.rb in mongoid-rspec-4.0.0 vs lib/matchers/associations.rb in mongoid-rspec-4.0.1

- old
+ new

@@ -1,17 +1,31 @@ -require 'mongoid/relations' +if Mongoid::Compatibility::Version.mongoid7_or_newer? + require 'mongoid/association' +else + require 'mongoid/relations' +end module Mongoid module Matchers module Associations - HAS_MANY = Mongoid::Relations::Referenced::Many - HAS_AND_BELONGS_TO_MANY = Mongoid::Relations::Referenced::ManyToMany - HAS_ONE = Mongoid::Relations::Referenced::One - BELONGS_TO = Mongoid::Relations::Referenced::In - EMBEDS_MANY = Mongoid::Relations::Embedded::Many - EMBEDS_ONE = Mongoid::Relations::Embedded::One - EMBEDDED_IN = Mongoid::Relations::Embedded::In + if Mongoid::Compatibility::Version.mongoid7_or_newer? + HAS_MANY = Mongoid::Association::Referenced::HasMany + HAS_AND_BELONGS_TO_MANY = Mongoid::Association::Referenced::HasAndBelongsToMany + HAS_ONE = Mongoid::Association::Referenced::HasOne + BELONGS_TO = Mongoid::Association::Referenced::BelongsTo + EMBEDS_MANY = Mongoid::Association::Embedded::EmbedsMany + EMBEDS_ONE = Mongoid::Association::Embedded::EmbedsOne + EMBEDDED_IN = Mongoid::Association::Embedded::EmbeddedIn + else + HAS_MANY = Mongoid::Relations::Referenced::Many + HAS_AND_BELONGS_TO_MANY = Mongoid::Relations::Referenced::ManyToMany + HAS_ONE = Mongoid::Relations::Referenced::One + BELONGS_TO = Mongoid::Relations::Referenced::In + EMBEDS_MANY = Mongoid::Relations::Embedded::Many + EMBEDS_ONE = Mongoid::Relations::Embedded::One + EMBEDDED_IN = Mongoid::Relations::Embedded::In + end class HaveAssociationMatcher def initialize(name, association_type) @association = {} @association[:name] = name.to_s @@ -114,11 +128,15 @@ return false else @positive_result_message = "association named #{@association[:name]}" end - relation = metadata.relation + relation = if Mongoid::Compatibility::Version.mongoid7_or_newer? + metadata.class + else + metadata.relation + end if relation != @association[:type] @negative_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}" return false else @positive_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}" @@ -220,14 +238,14 @@ @positive_result_message = "#{@positive_result_message} which is stored as #{metadata.store_as}" end end if @association[:index] - if metadata.index != true + if metadata.indexed? + @positive_result_message = "#{@positive_result_message} which set index" + else @negative_result_message = "#{@positive_result_message} which did not set index" return false - else - @positive_result_message = "#{@positive_result_message} which set index" end end if @association[:foreign_key] if metadata.foreign_key != @association[:foreign_key]