Class: SampleModels::ValidatesPresenceOfAttributeSequence
Instance Method Summary
collapse
#belongs_to_association, build, #initialize, #next
Instance Method Details
#belongs_to_value ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/sample_models/attribute_sequence.rb', line 129
def belongs_to_value
@previous_belongs_to_instances ||= {}
if @previous_belongs_to_instances[@number]
value = @previous_belongs_to_instances[@number]
begin
value.reload
value.id
rescue ActiveRecord::RecordNotFound
set_belongs_to_instance
@previous_belongs_to_instances[@number].id
end
else
set_belongs_to_instance
@previous_belongs_to_instances[@number].id
end
end
|
#existing_instance_not_previously_returned ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/sample_models/attribute_sequence.rb', line 146
def existing_instance_not_previously_returned
previous_ids = @previous_belongs_to_instances.values.map(&:id)
instance = nil
if previous_ids.empty?
belongs_to_association.klass.last
else
belongs_to_association.klass.last(
:conditions => ["id not in (?)", previous_ids]
)
end
end
|
#set_belongs_to_instance ⇒ Object
158
159
160
161
162
|
# File 'lib/sample_models/attribute_sequence.rb', line 158
def set_belongs_to_instance
instance = existing_instance_not_previously_returned
instance ||= belongs_to_association.klass.sample
@previous_belongs_to_instances[@number] = instance
end
|
#value ⇒ Object
164
165
166
|
# File 'lib/sample_models/attribute_sequence.rb', line 164
def value
belongs_to_association ? belongs_to_value : super
end
|