class Bridgetown::Resource::Relations

Attributes

resource[R]

@return [Bridgetown::Resource::Base]

site[R]

@return [Bridgetown::Site]

Public Class Methods

new(resource) click to toggle source

@param resource [Bridgetown::Resource::Base]

# File lib/bridgetown-core/resource/relations.rb, line 13
def initialize(resource)
  @resource = resource
  @site = resource.site
end

Public Instance Methods

method_missing(type, *args) click to toggle source
Calls superclass method
# File lib/bridgetown-core/resource/relations.rb, line 51
def method_missing(type, *args)
  return super unless type.to_s.in?(relation_types)

  resources_for_type(type)
end
relation_schema() click to toggle source

@return [HashWithDotAccess::Hash]

# File lib/bridgetown-core/resource/relations.rb, line 19
def relation_schema
  resource.collection.metadata.relations
end
relation_types() click to toggle source

@return [Array<String>]

# File lib/bridgetown-core/resource/relations.rb, line 24
def relation_types
  @relation_types ||= begin
    types = []
    relation_schema&.each do |_relation_type, collections|
      types << collections
      types << Array(collections).map { |item| ActiveSupport::Inflector.pluralize(item) }
    end
    types.flatten.uniq
  end
end
resources_for_type(type) click to toggle source

@param type [Symbol] @return [Bridgetown::Resource::Base, Array<Bridgetown::Resource::Base>]

# File lib/bridgetown-core/resource/relations.rb, line 37
def resources_for_type(type)
  relation_kind = kind_of_relation_for_type(type)
  return [] unless relation_kind

  case relation_kind.to_sym
  when :belongs_to
    belongs_to_relation_for_type(type)
  when :has_many
    has_many_relation_for_type(type)
  when :has_one
    has_one_relation_for_type(type)
  end
end
respond_to_missing?(type, *_args) click to toggle source
# File lib/bridgetown-core/resource/relations.rb, line 57
def respond_to_missing?(type, *_args)
  type.to_s.in?(relation_types)
end
to_liquid() click to toggle source
# File lib/bridgetown-core/resource/relations.rb, line 61
def to_liquid
  @to_liquid ||= Drops::RelationsDrop.new(self)
end

Private Instance Methods

belongs_to_relation_for_type(type) click to toggle source

@param type [Symbol] @return [Bridgetown::Resource::Base, Array<Bridgetown::Resource::Base>]

# File lib/bridgetown-core/resource/relations.rb, line 95
def belongs_to_relation_for_type(type)
  if resource.data[type].is_a?(Array)
    other_collection_for_type(type).resources.select do |other_resource|
      other_resource.data.slug.in?(resource.data[type])
    end
  else
    other_collection_for_type(type).resources.find do |other_resource|
      other_resource.data.slug == resource.data[type]
    end
  end
end
collection_labels() click to toggle source

@return [Array<String>]

# File lib/bridgetown-core/resource/relations.rb, line 86
def collection_labels
  [
    resource.collection.label,
    ActiveSupport::Inflector.singularize(resource.collection.label),
  ]
end
has_many_relation_for_type(type) click to toggle source

@param type [Symbol] @return [Array<Bridgetown::Resource::Base>]

# File lib/bridgetown-core/resource/relations.rb, line 109
def has_many_relation_for_type(type) # rubocop:disable Naming/PredicateName
  label, singular_label = collection_labels

  other_collection_for_type(type).resources.select do |other_resource|
    resource.data.slug.in?(
      Array(other_resource.data[label] || other_resource.data[singular_label])
    )
  end
end
has_one_relation_for_type(type) click to toggle source

@param type [Symbol] @return [Bridgetown::Resource::Base]

# File lib/bridgetown-core/resource/relations.rb, line 121
def has_one_relation_for_type(type) # rubocop:disable Naming/PredicateName
  label, singular_label = collection_labels

  other_collection_for_type(type).resources.find do |other_resource|
    resource.data.slug.in?(
      Array(other_resource.data[label] || other_resource.data[singular_label])
    )
  end
end
kind_of_relation_for_type(type) click to toggle source

@param type [Symbol] @return [String]

# File lib/bridgetown-core/resource/relations.rb, line 69
def kind_of_relation_for_type(type)
  relation_schema&.each do |relation_type, collections|
    collections = Array(collections).yield_self do |collections_arr|
      collections_arr +
        collections_arr.map { |item| ActiveSupport::Inflector.pluralize(item) }
    end.flatten.uniq
    return relation_type if collections.include?(type.to_s)
  end
end
other_collection_for_type(type) click to toggle source

@param type [Symbol] @return [Bridgetown::Collection]

# File lib/bridgetown-core/resource/relations.rb, line 81
def other_collection_for_type(type)
  site.collections[type] || site.collections[ActiveSupport::Inflector.pluralize(type)]
end