Class: Prim::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/prim/collection.rb

Overview

Collection largely wraps an association collection (like a Relation) but adds the concept of a primary member. Collections can only exist in the context of a Relationship (see Prim::Relationship for more info) and an “owning” ‘instance`, and contain mapping records.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relationship, instance) ⇒ Collection

Returns a new instance of Collection.



15
16
17
18
# File 'lib/prim/collection.rb', line 15

def initialize relationship, instance
  @instance     = instance
  @relationship = relationship
end

Instance Attribute Details

#instanceObject (readonly)

The members of a Collection are not necessarily the “source” records, or the Tags if a Post ‘has_many :tags`. If a mapping table (say, “taggings”) lies between Post and Tag, a Collection’s members will be Taggings instead.



13
14
15
# File 'lib/prim/collection.rb', line 13

def instance
  @instance
end

#membersObject (readonly)

The members of a Collection are not necessarily the “source” records, or the Tags if a Post ‘has_many :tags`. If a mapping table (say, “taggings”) lies between Post and Tag, a Collection’s members will be Taggings instead.



13
14
15
# File 'lib/prim/collection.rb', line 13

def members
  @members
end

#relationshipObject (readonly)

The members of a Collection are not necessarily the “source” records, or the Tags if a Post ‘has_many :tags`. If a mapping table (say, “taggings”) lies between Post and Tag, a Collection’s members will be Taggings instead.



13
14
15
# File 'lib/prim/collection.rb', line 13

def relationship
  @relationship
end

Instance Method Details

#all(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/prim/collection.rb', line 51

def all options = {}
  options[:with_primaries] ||= true

  if options[:with_primaries]
    primary_member = primary
    sources.tap do |records|
      records.find { |r| r == primary_member }.try(:primary=, true)
      records
    end
  else
    sources
  end
end

#primaryObject

Loads the primary member of this collection.



21
22
23
24
25
26
# File 'lib/prim/collection.rb', line 21

def primary
  sources.where( relationship.collection_label => { primary: true } ).first.try do |record|
    record.primary = true
    record
  end
end

#primary=(source_record) ⇒ Object

Sets the primary member of this collection. Requires a ‘source_record` to be passed (i.e. a Tag if Post `has_many :tags, through: :taggings`).



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/prim/collection.rb', line 30

def primary= source_record
  # Ensure what's passed can be set as the primary.
  return true unless source_record.is_a? relationship.source_class

  mapping = mapping_for(source_record)

  if source_record.persisted?
    if mapping.nil?
      create_mapping!(source_record)

    elsif !mapping.primary?
      mapping.update_attributes(primary: true)
    end

  else
    create_source_record!(source_record)
  end

  true
end