Class: JoinCollection

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

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ JoinCollection

Returns a new instance of JoinCollection.



8
9
10
# File 'lib/join_collection.rb', line 8

def initialize(collection)
  @source_objects = collection
end

Instance Attribute Details

#join_typeObject

Returns the value of attribute join_type.



6
7
8
# File 'lib/join_collection.rb', line 6

def join_type
  @join_type
end

#plural_targetObject

Returns the value of attribute plural_target.



6
7
8
# File 'lib/join_collection.rb', line 6

def plural_target
  @plural_target
end

#singular_targetObject

Returns the value of attribute singular_target.



6
7
8
# File 'lib/join_collection.rb', line 6

def singular_target
  @singular_target
end

#source_objectsObject (readonly)

Returns the value of attribute source_objects.



5
6
7
# File 'lib/join_collection.rb', line 5

def source_objects
  @source_objects
end

Instance Method Details

#join_many(target, target_class, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/join_collection.rb', line 29

def join_many(target, target_class, options)
  self.join_type = :join_many unless self.join_type == :join_one
  fk, pk, delegate_if, delegate_fields = extract_options(target, options)

  source_pks     = source_objects.map(&pk).compact
  target_objects = target_class.where(fk.in => source_pks).to_a

  mapper = target_objects.group_by(&fk)
  mapper.default = []
  join_data(mapper, pk, delegate_if, delegate_fields)
end

#join_one(target, target_class, options) ⇒ Object



24
25
26
27
# File 'lib/join_collection.rb', line 24

def join_one(target, target_class, options)
  self.join_type = :join_one
  join_many(target, target_class, options)
end

#join_to(target, target_class, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/join_collection.rb', line 12

def join_to(target, target_class, options)
  self.join_type = :join_to
  fk, pk, delegate_if, delegate_fields = extract_options(target, options)

  source_fks     = source_objects.map(&fk).compact
  target_objects = target_class.where(pk.in => source_fks).to_a

  mapper = target_objects.group_by(&pk)
  mapper.default = []
  join_data(mapper, fk, delegate_if, delegate_fields)
end