Sha256: f1ba39db7c449c53a2849cc14225dbd2a481456c0f710574e42f490d04f48632

Contents?: true

Size: 878 Bytes

Versions: 13

Compression:

Stored size: 878 Bytes

Contents

module ActiveForce
  module Association
    class HasOneAssociation < Association
      private

      def target(owner)
        apply_scope(relation_model.query, owner).find_by(foreign_key => owner.id)
      end

      def default_foreign_key
        infer_foreign_key_from_model parent
      end

      def define_assignment_method
        foreign_key = self.foreign_key
        method_name = relation_name
        parent.send :define_method, "#{method_name}=" do |new_target|
          new_target = new_target.first if new_target.is_a?(Array)
          if new_target.present?
            new_target.public_send("#{foreign_key}=", id)
          else
            current_target = public_send(method_name)
            current_target&.public_send("#{foreign_key}=", nil)
          end
          association_cache[method_name] = new_target
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
active_force-0.24.0 lib/active_force/association/has_one_association.rb
active_force-0.23.0 lib/active_force/association/has_one_association.rb
active_force-0.22.1 lib/active_force/association/has_one_association.rb
active_force-0.22.0 lib/active_force/association/has_one_association.rb
active_force-0.21.0 lib/active_force/association/has_one_association.rb
active_force-0.20.1 lib/active_force/association/has_one_association.rb
active_force-0.20.0 lib/active_force/association/has_one_association.rb
active_force-0.19.0 lib/active_force/association/has_one_association.rb
active_force-0.18.0 lib/active_force/association/has_one_association.rb
active_force-0.17.0 lib/active_force/association/has_one_association.rb
active_force-0.16.0 lib/active_force/association/has_one_association.rb
active_force-0.15.1 lib/active_force/association/has_one_association.rb
active_force-0.15.0 lib/active_force/association/has_one_association.rb