module Mongoid::History::Trackable::RelationMethods

Public Instance Methods

embeds_many?(field) click to toggle source

Indicates whether there is an Embedded::Many relation for the given embedded field.

@param [ String | Symbol ] field The name of the embedded field.

@return [ Boolean ] true if there is an Embedded::Many relation for the given embedded field.

# File lib/mongoid/history/trackable.rb, line 384
def embeds_many?(field)
  relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
                          Mongoid::Association::Embedded::EmbedsMany::Proxy
                        else
                          Mongoid::Relations::Embedded::Many
                        end
end
embeds_one?(field) click to toggle source

Indicates whether there is an Embedded::One relation for the given embedded field.

@param [ String | Symbol ] embed The name of the embedded field.

@return [ Boolean ] true if there is an Embedded::One relation for the given embedded field.

# File lib/mongoid/history/trackable.rb, line 371
def embeds_one?(field)
  relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
                          Mongoid::Association::Embedded::EmbedsOne::Proxy
                        else
                          Mongoid::Relations::Embedded::One
                        end
end
relation_alias(embed) click to toggle source

Retrieves the database representation of an embedded field name, in case the :store_as option is used.

@param [ String | Symbol ] embed The name or alias of the embedded field.

@return [ String ] The database name of the embedded field.

# File lib/mongoid/history/trackable.rb, line 397
def relation_alias(embed)
  relation_aliases[embed]
end
relation_class_of(field) click to toggle source

Returns a relation class for the given field.

@param [ String | Symbol ] field The name of the field.

@return [ nil | Constant ] Class being related.

# File lib/mongoid/history/trackable.rb, line 361
def relation_class_of(field)
  meta = meta_of(field)
  return meta.class_name.constantize if meta
end

Protected Instance Methods

meta_of(field) click to toggle source

Return the reflected metadata for a relation.

@param [ String ] field The database field name for a relation.

@return [ nil | Mongoid::Relations::Metadata ]

# File lib/mongoid/history/trackable.rb, line 408
def meta_of(field)
  @meta_of ||= {}
  return @meta_of[field] if @meta_of.key?(field)
  @meta_of[field] = reflect_on_association(relation_alias(field))
end
relation_aliases() click to toggle source

Retrieves the memoized hash of embedded aliases and their associated database representations.

@return [ Hash < String, String > ] hash of embedded aliases (keys) to database representations (values)

# File lib/mongoid/history/trackable.rb, line 427
def relation_aliases
  @relation_aliases ||= relations.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
    store_as = Mongoid::Compatibility::Version.mongoid7_or_newer? ? v.store_as : v[:store_as]
    h[store_as || k] = k
    h
  end
end
relation_of(field) click to toggle source

Returns a relation for the given field.

@param [ String | Symbol ] field The name of the field.

@return [ nil | Constant ] Type of relation.

# File lib/mongoid/history/trackable.rb, line 419
def relation_of(field)
  meta = meta_of(field)
  meta ? meta.relation : nil
end