Sha256: f2b7256e414cccc4eed1ce097286068ea758d3a29744f478d25309de140e4d72

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module Neo4j::ActiveNode
  module Property
    extend ActiveSupport::Concern
    include Neo4j::Shared::Property

    def initialize(attributes = nil)
      super(attributes)
      @attributes ||= Hash[self.class.attributes_nil_hash]
      send_props(@relationship_props) if _persisted_obj && !@relationship_props.nil?
    end

    module ClassMethods
      # Extracts keys from attributes hash which are associations of the model
      # TODO: Validate separately that relationships are getting the right values?  Perhaps also store the values and persist relationships on save?
      def extract_association_attributes!(attributes)
        return unless contains_association?(attributes)
        attributes.each_with_object({}) do |(key, _), result|
          result[key] = attributes.delete(key) if self.association_key?(key)
        end
      end

      def association_key?(key)
        association_method_keys.include?(key.to_sym)
      end

      private

      def contains_association?(attributes)
        return false unless attributes

        attributes.each_key.any?(&method(:association_key?))
      end

      # All keys which could be association setter methods (including _id/_ids)
      def association_method_keys
        @association_method_keys ||=
          associations_keys.map(&:to_sym) +
          associations.values.map do |association|
            if association.type == :has_one
              "#{association.name}_id"
            elsif association.type == :has_many
              "#{association.name.to_s.singularize}_ids"
            end.to_sym
          end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
neo4j-5.2.3 lib/neo4j/active_node/property.rb
neo4j-5.2.2 lib/neo4j/active_node/property.rb