Module: LinkedRails::Model::Collections

Extended by:
ActiveSupport::Concern
Included in:
LinkedRails::Model
Defined in:
lib/linked_rails/model/collections.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

COLLECTION_CUSTOMIZABLE_OPTIONS =
{
  # display [Sym] The default display type.
  #   Choose between :grid, :settingsTable, :table, :card, :default
  display: :default,
  # grid_max_columns [Integer] The default amount of columns to use in a grid.
  grid_max_columns: 3,
  # page_size [Integer] The default page size.
  page_size: 20,
  # table_type [Sym] The columns to use in the table.
  table_type: lambda {
    case display&.to_sym
    when :table
      :default
    when :settingsTable
      :settings
    end
  },
  # title String The default title.
  title: -> { title_from_translation },
  # type [Sym] The default pagination type.
  #   Choose between :paginated, :infinite.
  type: :paginated
}.freeze
COLLECTION_STATIC_OPTIONS =
{
  # association [Sym] The association of the collection items.
  association: nil,
  # association_base [Scope, Array] The items of the collection.
  association_base: -> { apply_scope(sorted_association(filtered_association)) },
  # association_class [Class] The class of the collection items.
  association_class: nil,
  # association_scope [Sym] The scope applied to the collection.
  association_scope: nil,
  # collection_class [Class] The base class of the collection.
  #   If you want to use a class other than LinkedRails.collection_class.
  collection_class: nil,
  # collection_class [Hash] The default filters applied to the collection.
  default_filters: {},
  # collection_class [Array<Hash>] The default sortings applied to the collection.
  default_sortings: [{key: Vocab.schema.dateCreated, direction: :desc}],
  # iri_template_keys [Array<Sym>] Custom query keys for the iri template
  iri_template_keys: [],
  # joins [Array<Sym>, Sym] The associations to join
  joins: nil,
  # parent [Instance] The default parent of a collection.
  parent: nil,
  # parent_iri [Array<String>] The iri elements of the parent
  parent_iri: -> { parent&.iri_elements },
  # part_of [Instance] The record to serialize as isPartOf
  part_of: -> { parent },
  # policy_scope [Scope] The policy scope class to be used for scoping
  #   Set to false to skip scoping
  policy_scope: -> { policy ? policy::Scope : Pundit::PolicyFinder.new(filtered_association).scope! },
  # route_key [Symbol, String] The route key for the association
  route_key: nil
}.freeze
COLLECTION_OPTIONS =
COLLECTION_CUSTOMIZABLE_OPTIONS.merge(COLLECTION_STATIC_OPTIONS)

Instance Method Summary collapse

Instance Method Details

#collection_for(name, **instance_opts) ⇒ Collection

Initialises a Collection for one of the collections defined by has_collection

Parameters:

  • name (Hash)

    as defined with has_collection

  • user_context (Class)
  • filter (Hash)
  • page (Integer, String)
  • part_of (ApplicationRecord)
  • opts (Hash)

    Additional options to be passed to the collection.

Returns:

See Also:

  • Ldable#has_collection


188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/linked_rails/model/collections.rb', line 188

def collection_for(name, **instance_opts)
  collection_opts = collection_options_for(name).dup
  return if collection_opts.blank?

  collection_opts[:name] = name
  collection_opts[:parent] = self
  collection_class =
    collection_opts.delete(:collection_class) ||
    collection_opts[:association_class].default_collection_option(:collection_class) ||
    LinkedRails.collection_class
  collection_class.collection_or_view(collection_opts, instance_opts)
end

#collection_iri(collection, **opts) ⇒ Object



201
202
203
# File 'lib/linked_rails/model/collections.rb', line 201

def collection_iri(collection, **opts)
  LinkedRails.iri(path: collection_root_relative_iri(collection, **opts))
end

#collection_options_for(name) ⇒ Object



205
206
207
208
209
210
# File 'lib/linked_rails/model/collections.rb', line 205

def collection_options_for(name)
  opts = collections.detect { |c| c[:name] == name.to_sym }
  raise("Collection #{name} not found for #{self}") unless opts

  opts[:options] || {}
end

#collection_root_relative_iri(collection, **opts) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/linked_rails/model/collections.rb', line 212

def collection_root_relative_iri(collection, **opts)
  collection_opts = collection_options_for(collection).dup
  template = collection_opts[:iri_template]
  klass = collection_opts[:association_class]
  opts[:route_key] = collection_opts[:route_key] || klass.collection_route_key
  opts[:parent_iri] = iri_elements

  template.expand(**opts).to_s
end

#parent_collections(user_context) ⇒ Object



222
223
224
225
226
# File 'lib/linked_rails/model/collections.rb', line 222

def parent_collections(user_context)
  return [self.class.root_collection(user_context: user_context)] if try(:parent).try(:collections).blank?

  parent_collections_for(parent, user_context)
end