Module: LinkedRails::Model::Collections::ClassMethods

Defined in:
lib/linked_rails/model/collections.rb

Instance Method Summary collapse

Instance Method Details

#collections_add(opts) ⇒ Object



48
49
50
51
52
# 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_collectionsObject



54
55
56
57
58
# 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 = {}) ⇒ Collection

Note:

Adds a instance_method <name>_collection

Defines a collection to be used in LinkedRails::Model::Collections#collection_for

Parameters:

Options Hash (options):

  • association (Sym)

    the name of the association

  • association_class (Class)

    the class of the association

  • joins (Sym)

    the associations to join

Returns:

See Also:

  • Ldable#collection_for


69
70
71
72
73
74
75
76
77
78
# 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