Sha256: a4012fe9f216b401841ac0df3f65b4d7db01805d28b3ee3c8f0eab3253f42d35
Contents?: true
Size: 1.49 KB
Versions: 3
Compression:
Stored size: 1.49 KB
Contents
module AssignableValues module ActiveRecord module Restriction class BelongsToAssociation < Base private def association_class model.reflect_on_association(property).klass end def association_id_method association = model.reflect_on_association(property) if association.respond_to?(:foreign_key) association.foreign_key # Rails >= 3.1 else association.primary_key_name # Rails 2 + 3.0 end end def error_property association_id_method end def association_id(record) record.send(association_id_method) end def has_previously_saved_value?(record) !record.new_record? && record.respond_to?(association_id_was_method) end def previously_saved_value(record) if old_id = record.send(association_id_was_method).presence if old_id == association_id(record) current_value(record) # no need to query the database if nothing changed else association_class.find_by_id(old_id) end end end def current_value(record) value = record.send(property) value = record.send(property, true) if (value && value.id) != association_id(record) value end private def association_id_was_method :"#{association_id_method}_was" end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems