<commit_before>module Neo4j
  module Rails
    class Model

      # Channel.rels2(to: location)
      # Channel.rels2(:located, to: location)

      def rels2(type, options = {})
        # type is optinal parameter
        if type.is_a? Hash
          options = type
          type = nil
        end

        rel_type =  type ? ":#{type}" : ''
        # todo: allow both directions?

        cypher = self.cypher
        if end_node = options[:to]
          # todo: by default, to_other returns nodes in both directions, where we would expect it only to do outgoing.
          # in fact, there appears to be no easy way to specify direction here with neo4jrb, so we use cypher.
          # should we decide not to wrap nodes for speed?
          #rels = type ? self.rels(type) : self.rels
          #rels.to_other(end_node)
          #cypher.match("(self)-[rel#{rel_type}]->(node(#{end_node.neo_id}))").mapped(:rel)
          cypher.match("(self)-[rel#{rel_type}]->(end_node)").
              where("ID(end_node) = #{end_node.neo_id}").
              mapped(:rel)
        elsif start_node = options[:from]
          cypher.match("(self)<-[rel#{rel_type}]-(start_node)").
                        where("ID(start_node) = #{start_node.neo_id}").
                        mapped(:rel)
          #cypher.match("(self)<-[rel#{rel_type}]-(node(#{start_node.neo_id}))").mapped(:rel)
          #rels = type ? start_node.rels(type) : start_node.rels
          #rels.to_other(self)
        else
          self.rels(type)
        end
      end

      # returns the relation if related, else false
      def related?(type, options = {})
        rels2(type, options).first.presence
      end

      def ensure_relation(type, options = {}, props = {})
        # options: to and from
        if rel = related?(type, options)
          rel.update_attributes props<commit_msg>error unless id given on rels2<commit_after>module Neo4j
  module Rails
    class Model

      # Channel.rels2(to: location)
      # Channel.rels2(:located, to: location)

      def rels2(type, options = {})
        # type is optinal parameter
        if type.is_a? Hash
          options = type
          type = nil
        end

        rel_type =  type ? ":#{type}" : ''
        # todo: allow both directions?

        cypher = self.cypher
        if end_node = options[:to]
          # todo: by default, to_other returns nodes in both directions, where we would expect it only to do outgoing.
          # in fact, there appears to be no easy way to specify direction here with neo4jrb, so we use cypher.
          # should we decide not to wrap nodes for speed?
          #rels = type ? self.rels(type) : self.rels
          #rels.to_other(end_node)
          #cypher.match("(self)-[rel#{rel_type}]->(node(#{end_node.neo_id}))").mapped(:rel)
          unless end_node.neo_id
            raise "No id on :to node. Unpersisted? #{end_node.inspect}"
          end

          cypher.match("(self)-[rel#{rel_type}]->(end_node)").
              where("ID(end_node) = #{end_node.neo_id}").
              mapped(:rel)
        elsif start_node = options[:from]
          cypher.match("(self)<-[rel#{rel_type}]-(start_node)").
                        where("ID(start_node) = #{start_node.neo_id}").
                        mapped(:rel)
          #cypher.match("(self)<-[rel#{rel_type}]-(node(#{start_node.neo_id}))").mapped(:rel)
          #rels = type ? start_node.rels(type) : start_node.rels
          #rels.to_other(self)
        else
          self.rels(type)
        end
      end

      # returns the relation if related, else false
      def related?(type, options = {})
        rels2(type, options).first.presence
      end

      def ensure_relation(type, options = {}, props = {})
        # options: to and from
        if rel = related?(type, options)
          rel.update_attributes props
{'fraction_non_alphanumeric': 0.12827836925473904, 'fraction_numerical': 0.0033757465593352376, 'mean_word_length': 2.069322709163347, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '891006', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1159, 'n_words': 291}