Sha256: 25585f84c35bece0f1b9193d3d357624769a2cad4a85dae9cfa7cef7734526b0

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module MassiveRecord
  module ORM
    module Relations
      class Proxy
        #
        # Proxy used to reference one other object in another table.
        #
        class ReferencesOne < Proxy

          def proxy_target=(proxy_target)
            set_foreign_key_in_proxy_owner(proxy_target.id) if proxy_target
            super(proxy_target)
          end

          def replace(proxy_target)
            super
            set_foreign_key_in_proxy_owner(nil) if proxy_target.nil?
          end


          private

          def find_proxy_target(options = {})
            proxy_target_class.find(proxy_owner.send(metadata.foreign_key))
          end

          def can_find_proxy_target?
            super || proxy_owner.send(metadata.foreign_key).present?
          end

          def set_foreign_key_in_proxy_owner(id)
            if update_foreign_key_fields_in_proxy_owner? && proxy_owner.respond_to?(metadata.foreign_key_setter)
              proxy_owner.send(metadata.foreign_key_setter, id)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
massive_record-0.2.2 lib/massive_record/orm/relations/proxy/references_one.rb
massive_record-0.2.2.rc2 lib/massive_record/orm/relations/proxy/references_one.rb
massive_record-0.2.2.rc1 lib/massive_record/orm/relations/proxy/references_one.rb