Class: SampleModels::SecondPassBaseAttributeSequence

Inherits:
AttributeSequence show all
Defined in:
lib/sample_models/attribute_sequence.rb

Instance Method Summary collapse

Methods inherited from AttributeSequence

#belongs_to_association, build, #next, #value

Constructor Details

#initialize(model, column) ⇒ SecondPassBaseAttributeSequence

Returns a new instance of SecondPassBaseAttributeSequence.



92
93
94
95
# File 'lib/sample_models/attribute_sequence.rb', line 92

def initialize(model, column)
  super(model, column, nil, nil)
  @previous_values = {}
end

Instance Method Details

#belongs_assoc_value_already_used?(record) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/sample_models/attribute_sequence.rb', line 97

def belongs_assoc_value_already_used?(record)
  @previous_values.any? { |prev_num, prev_record|
    prev_record == record && prev_num != @number
  }
end

#belongs_to_assoc_foreign_key_valueObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sample_models/attribute_sequence.rb', line 103

def belongs_to_assoc_foreign_key_value
  assoc_klass = belongs_to_association.klass
  unless assoc_klass == @model.ar_class
    record = (assoc_klass.last || assoc_klass.sample)
    while belongs_assoc_value_already_used?(record)
      record = assoc_klass.sample
    end
    @previous_values[@number] = record
    record.id
  end
end