module LinkedRails::Model::Collections

Public Instance Methods

collection_for(name, instance_opts = {}) click to toggle source

Initialises a {Collection} for one of the collections defined by {has_collection} @see Ldable#has_collection @param [Hash] name as defined with {has_collection} @param [Class] user_context @param [Hash] filter @param [Integer, String] page @param [ApplicationRecord] part_of @param [Hash] opts Additional options to be passed to the collection. @return [Collection]

# File lib/linked_rails/model/collections.rb, line 21
def collection_for(name, instance_opts = {})
  collection_opts = collections.detect { |c| c[:name] == name }.try(:[], :options).dup
  return if collection_opts.blank?

  collection_opts[:name] = name
  collection_opts[:parent] = self
  collection_opts[:part_of] = collection_opts.key?(:part_of) ? send(collection_opts[:part_of]) : self
  collection_class = collection_opts.delete(:collection_class) || LinkedRails.collection_class
  collection_class.collection_or_view(collection_opts, instance_opts)
end
parent_collections(user_context) click to toggle source
# File lib/linked_rails/model/collections.rb, line 32
def parent_collections(user_context)
  return [] if try(:parent).try(:collections).blank?

  parent_collections_for(parent, user_context)
end

Private Instance Methods

parent_collections_for(parent, user_context) click to toggle source
# File lib/linked_rails/model/collections.rb, line 40
def parent_collections_for(parent, user_context)
  parent
    .collections
    .select { |collection| is_a?(collection[:options][:association_class]) }
    .map { |collection| parent.collection_for(collection[:name], user_context: user_context) }
end