Class: SampleModels::AttributeSequence::RequiredBelongsToSource
- Inherits:
-
AbstractSource
- Object
- AbstractSource
- SampleModels::AttributeSequence::RequiredBelongsToSource
- Defined in:
- lib/sample_models/attribute_sequence.rb
Instance Method Summary collapse
- #existing_instance_not_previously_returned ⇒ Object
-
#initialize(assoc) ⇒ RequiredBelongsToSource
constructor
A new instance of RequiredBelongsToSource.
- #set_instance ⇒ Object
- #value ⇒ Object
Methods inherited from AbstractSource
Constructor Details
#initialize(assoc) ⇒ RequiredBelongsToSource
Returns a new instance of RequiredBelongsToSource.
95 96 97 98 99 |
# File 'lib/sample_models/attribute_sequence.rb', line 95 def initialize(assoc) super() @assoc = assoc @previous_instances = {} end |
Instance Method Details
#existing_instance_not_previously_returned ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sample_models/attribute_sequence.rb', line 101 def existing_instance_not_previously_returned previous_ids = @previous_instances.values.map(&:id) instance = nil if previous_ids.empty? @assoc.klass.last else @assoc.klass.last( :conditions => ["id not in (?)", previous_ids] ) end end |
#set_instance ⇒ Object
113 114 115 116 117 |
# File 'lib/sample_models/attribute_sequence.rb', line 113 def set_instance instance = existing_instance_not_previously_returned instance ||= @assoc.klass.sample @previous_instances[@number] = instance end |
#value ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sample_models/attribute_sequence.rb', line 119 def value if @previous_instances[@number] value = @previous_instances[@number] begin value.reload value.id rescue ActiveRecord::RecordNotFound set_instance @previous_instances[@number].id end else set_instance @previous_instances[@number].id end end |