Class: FixtureRecord::AssociationTraversal::SymbolBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(source_record, symbol, *next_associations) ⇒ SymbolBuilder

Returns a new instance of SymbolBuilder.



33
34
35
36
37
# File 'lib/fixture_record/association_traversal.rb', line 33

def initialize(source_record, symbol, *next_associations)
  @source_record = source_record
  @symbol = symbol
  @next_associations = next_associations
end

Instance Method Details

#buildObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fixture_record/association_traversal.rb', line 39

def build
  raise UnrecognizedAssociationError.new(
    "#{@symbol} is not a recognized association or method on #{@source_record.class}. Is it misspelled?"
  ) unless klass_association.present?

  if through_assoc_option && @source_record._traversed_fixture_record_associations.exclude?(through_assoc_option)
    infill_through
  end

  @source_record._traversed_fixture_record_associations << @symbol
  built_records = Array.wrap(@source_record.send(@symbol)).compact_blank
  return unless built_records.present?

  built_records.each do |record|
    record.fixture_record_prefix = @source_record.fixture_record_prefix
    record.fixture_record_suffix = @source_record.fixture_record_suffix
    record.to_fixture_record(*@next_associations)
  end
end

#infill_throughObject



59
60
61
# File 'lib/fixture_record/association_traversal.rb', line 59

def infill_through
  SymbolBuilder.new(@source_record, through_assoc_option).build
end

#klass_associationObject



67
68
69
# File 'lib/fixture_record/association_traversal.rb', line 67

def klass_association
  @source_record.class.reflect_on_association(@symbol)
end

#through_assoc_optionObject



63
64
65
# File 'lib/fixture_record/association_traversal.rb', line 63

def through_assoc_option
  klass_association.options[:through]
end