Module: GenericTableAssociation

Extended by:
Assertions::ClassMethods
Includes:
Assertions, Examples
Included in:
ActiveRecord::Base, Generic_Table
Defined in:
app/models/generic_table_association.rb

Defined Under Namespace

Modules: Assertions, ClassMethods, Examples

Constant Summary

Constants included from Examples

Examples::First_stream_method

Instance Method Summary collapse

Methods included from Assertions::ClassMethods

assert_foreign_keys_not_nil, assert_invariant, assert_post_conditions, assert_pre_conditions

Methods included from Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Instance Method Details

#associated_foreign_key_records(association_with_foreign_key) ⇒ Object

find records pointed to by foreign key.


221
222
223
224
225
226
227
# File 'app/models/generic_table_association.rb', line 221

def associated_foreign_key_records(association_with_foreign_key)
	class_with_foreign_key=self.class.association_class(association_with_foreign_key)
	foreign_key_symbol=class_with_foreign_key.associated_foreign_key_name(self.class.name.tableize)
	associated_records=class_with_foreign_key.where(foreign_key_symbol => self[:id])

	return associated_records
end

#associated_to_s(association_name, method, *args) ⇒ Object

def


345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/models/generic_table_association.rb', line 345

def associated_to_s(association_name,method,*args)
	if self[association_name.to_s+'_id'].nil? then # foreign key uninitialized
		return ''
	else
		ass=send(association_name)
		if ass.nil? then
			return ''
		else
			return ass.send(method.to_sym,*args).to_s
		end
	end
end

#association_class(association_name) ⇒ Object

associated_foreign_key_records


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/models/generic_table_association.rb', line 228

def association_class(association_name)
	 if !self.kind_of?(ActiveRecord::Base) then
		raise "#{self.class.name} is not an ActiveRecord::Base."
	 elsif !self.class.is_association?(self.class.association_method_symbol(association_name)) then
		raise "association_name=#{association_name.inspect}, #{self.class.association_method_symbol(association_name)} is not an association of #{self.class.name}."
	else
		association=name_to_association(association_name)
		if association.instance_of?(Array) then
			classes=association.enumerate(:map){|r| r.class}.uniq
			if classes.size==1 then
				return classes[0] # remove Array
			else
				return classes # polymorphic? impossible?
			end #if
		else
			return association.enumerate(:map){|r| r.class}
		end #if
	end #if
end

#association_has_data(association_name) ⇒ Object

association_state


342
343
344
# File 'app/models/generic_table_association.rb', line 342

def association_has_data(association_name)
	return association_state(association_name)[/ and returns type /,0]
end

#association_state(association_name) ⇒ Object

Display attribute or method value from association even if association is nil


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/models/generic_table_association.rb', line 296

def association_state(association_name)
	case self.class.association_arity(association_name)
	when :to_one
		foreign_key_value=foreign_key_value(association_name)
		if foreign_key_value.nil? then # foreign key uninitialized
			return "Foreign key #{association_name.to_s}_id defined as attribute but has nil value."
		#~ elsif foreign_key_value.empty? then # foreign key uninitialized
			#~ return "Foreign key #{association_name.to_s}_id defined as attribute but has empty value."
		else
			ass=send(association_name)
			if ass.nil? then
				return "Foreign key #{association_name.to_s}_id has value #{foreign_key_value.inspect} but the association returns nil."
			else
				return "Foreign key #{association_name.to_s}_id has value #{foreign_key_value.inspect},#{ass.inspect} and returns type #{ass.class.name}."
			end
		end
	when :to_many
		ass=send(association_name)
		associations_foreign_key_name=(self.class.name.tableize.singularize+'_id').to_sym
		if ass.nil? then
			return "Association #{association_name}'s foreign key #{associations_foreign_key_name} has value #{ass[self.class.name.to_s+'_id']} but the association returns nil."
		elsif ass.empty? then
			ret= "Association #{association_name} with foreign key #{associations_foreign_key_name} is empty; "
			case self.class.association_class(association_name).association_macro_type(self.class.name.tableize.singularize)
			when :has_many
				return ret+"but has many."
			when :belongs_to
				return ret+"but belongs_to."
			when :neither_has_many_nor_belongs_to
				return ret+"because neither_has_many_nor_belongs_to."
			else
				return "New return value from #{self.class.name}.association_macro_type(#{association_name})=#{self.class.association_macro_type(association_name)}."
			end #case
		else
			associations_foreign_key_values=ass.map { |a| a.send(associations_foreign_key_name) }.uniq.join(',')
			return "Association #{association_name}'s foreign key #{associations_foreign_key_name} has value #{associations_foreign_key_values},#{ass.inspect} and returns type #{ass.class.name}."
		end
		
	when :not_generic_table
		return "#{self.class.name} does not recognize #{association_name} as a generic table."
	when:not_an_association
		return "#{self.class.name} does not recognize #{association_name} as association."
	else
		return "New return value from #{self.class.name}.association_arity(#{association_name})=#{self.class.association_arity(association_name)}."
	end #if
end

#foreign_key_points_to_me?(ar_from_fixture, association_name) ⇒ Boolean

association_class

Returns:

  • (Boolean)

247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/generic_table_association.rb', line 247

def foreign_key_points_to_me?(ar_from_fixture,association_name)
	associated_records=assert_call_result(ar_from_fixture,association_name)
	if associated_records.instance_of?(Array) then
		associated_records.each do |ar|
			fkAssName=ar_from_fixture.class.name.tableize.singularize
			fk=ar.class.associated_foreign_key_name(fkAssName.to_s.to_sym)
			@associated_foreign_key_id=ar[fk]
		end #each
	else # single record
			ar.class.associated_foreign_key_name(associated_records,association_name).each do |fk|
				assert_equal(ar_from_fixture.id,associated_foreign_key_id(associated_records,fk.to_sym),"assert_foreign_key_points_to_me: associated_records=#{associated_records.inspect},ar_from_fixture=#{ar_from_fixture.inspect},association_name=#{association_name}")
			end #each
	end #if
end

#foreign_key_to_association(foreign_key) ⇒ Object

translate foreign_key into asociation Example: foreign_Key_to_association(:fk_id)==fk association


214
215
216
# File 'app/models/generic_table_association.rb', line 214

def foreign_key_to_association(foreign_key)
	name_to_association(self.class.foreign_key_to_association_name(foreign_key))
end

#foreign_key_value(association_name) ⇒ Object

foreign_Key_to_association


217
218
219
# File 'app/models/generic_table_association.rb', line 217

def foreign_key_value(association_name)
	return self[association_name.to_s+'_id']
end

#logical_primary_key_recursive_value(delimiter = ',') ⇒ Object

logical key with each foeign key replaced by logical key value pointed to


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/generic_table_association.rb', line 262

def logical_primary_key_recursive_value(delimiter=',')
	if self.class.sequential_id? then
		return logical_primary_key_value
	else
		self.class.logical_primary_key.map do |e| 
			if self.class.is_foreign_key_name?(e) then
				association=foreign_key_to_association(e)
				if association.nil? then
					nil
				else
					association.logical_primary_key_recursive_value 
				end #if
			else
				self[e]
			end #if
		end #map
	end #if
end

#logical_primary_key_value(delimiter = ',') ⇒ Object

logical_primary_key_recursive_value


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/models/generic_table_association.rb', line 280

def logical_primary_key_value(delimiter=',')
	if self.class.sequential_id? then
		if self.respond_to?(:logical_primary_key) then # still sequential, not requred, default
			return self[:created_at] # better sequential key
		else
			return id # logical primary key
		end
	else
		if self.class.logical_primary_key.is_a?(Array) then
			return self.class.logical_primary_key.map {|k| self[k]}.join(delimiter)
		else #not array
			return self[self.class.logical_primary_key]
		end #if
	end #if
end

#name_to_association(association_name) ⇒ Object

transform association name into association records for instance


209
210
211
# File 'app/models/generic_table_association.rb', line 209

def name_to_association(association_name)
	method(association_name).call
end