module LinkedRails::Model::Collections::ClassMethods

Public Instance Methods

collections_add(opts) click to toggle source
# File lib/linked_rails/model/collections.rb, line 48
def collections_add(opts)
  initialize_collections
  collections.delete_if { |c| c[:name] == opts[:name] }
  collections.append(opts)
end
initialize_collections() click to toggle source
# File lib/linked_rails/model/collections.rb, line 54
def initialize_collections
  return if collections && method(:collections).owner == singleton_class

  self.collections = superclass.try(:collections)&.dup || []
end
with_collection(name, options = {}) click to toggle source

Defines a collection to be used in {collection_for} @see Ldable#collection_for @note Adds a instance_method <name>_collection @param [Hash] name as to be used in {collection_for} @param [Hash] options @option options [Sym] association the name of the association @option options [Class] association_class the class of the association @option options [Sym] joins the associations to join @return [Collection]

# File lib/linked_rails/model/collections.rb, line 69
def with_collection(name, options = {})
  options[:association] ||= name.to_sym
  options[:association_class] ||= name.to_s.classify.constantize

  collections_add(name: name, options: options)

  define_method "#{name.to_s.singularize}_collection" do |opts = {}|
    collection_for(name, opts)
  end
end