Module: Neoid::Node::InstanceMethods

Defined in:
lib/neoid/node.rb

Instance Method Summary collapse

Instance Method Details

#_neo_saveObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/neoid/node.rb', line 95

def _neo_save
  return unless Neoid.enabled?

  data = to_neo.merge(ar_type: self.class.name, ar_id: id, Neoid::UNIQUE_ID_KEY => neo_unique_id)
  data.reject! { |k, v| v.nil? }

  gremlin_query = <<-GREMLIN
    idx = g.idx('node_auto_index');
    q = null;
    if (idx) q = idx.get(unique_id_key, unique_id);

    node = null;
    if (q && q.hasNext()) {
      node = q.next();
      node_data.each {
        if (node.getProperty(it.key) != it.value) {
          node.setProperty(it.key, it.value);
        }
      }
    } else {
      node = g.addVertex(node_data);
      if (enable_subrefs) g.addEdge(g.v(subref_id), node, neo_subref_node_rel_type);

      if (enable_model_index) g.idx(neo_model_index_name).put('ar_id', node.ar_id, node);
    }

    node
  GREMLIN

  script_vars = {
    unique_id_key: Neoid::UNIQUE_ID_KEY,
    node_data: data,
    unique_id: neo_unique_id,
    enable_subrefs: Neoid.config.enable_subrefs,
    enable_model_index: Neoid.config.enable_per_model_indexes && self.class.neoid_config.enable_model_index
  }

  if Neoid.config.enable_subrefs
    script_vars.update(
      subref_id: self.class.neo_subref_node.neo_id,
      neo_subref_node_rel_type: self.class.neo_subref_node_rel_type
    )
  end

  if Neoid.config.enable_per_model_indexes && self.class.neoid_config.enable_model_index
    script_vars.update(
      neo_model_index_name: self.class.neo_model_index_name
    )
  end

  Neoid::logger.info "Node#neo_save #{self.class.name} #{id}"

  node = Neoid.execute_script_or_add_to_batch(gremlin_query, script_vars) do |value|
    @_neo_representation = Neoid::Node.from_hash(value)
  end.then do |result|
    neo_search_index
  end

  node
end

#neo_after_relationship_remove(relationship) ⇒ Object



193
194
195
# File 'lib/neoid/node.rb', line 193

def neo_after_relationship_remove(relationship)
  relationship.neo_destroy
end

#neo_after_relationship_through_remove(record) ⇒ Object



204
205
206
207
# File 'lib/neoid/node.rb', line 204

def neo_after_relationship_through_remove(record)
  @__neo_temp_rels.each { |record, relationship| relationship.neo_destroy }
  @__neo_temp_rels.delete(record)
end

#neo_before_relationship_through_remove(record) ⇒ Object



197
198
199
200
201
202
# File 'lib/neoid/node.rb', line 197

def neo_before_relationship_through_remove(record)
  rel_model, foreign_key_of_owner, foreign_key_of_record = Neoid::Relationship.[self.class.name.to_s][record.class.name.to_s]
  rel_model = rel_model.to_s.constantize
  @__neo_temp_rels ||= {}
  @__neo_temp_rels[record] = rel_model.where(foreign_key_of_owner => id, foreign_key_of_record => record.id).first
end

#neo_find_by_idObject



89
90
91
92
93
# File 'lib/neoid/node.rb', line 89

def neo_find_by_id
  # Neoid::logger.info "Node#neo_find_by_id #{self.class.neo_index_name} #{self.id}"
  node = Neoid.db.get_node_auto_index(Neoid::UNIQUE_ID_KEY, neo_unique_id)
  node.present? ? Neoid::Node.from_hash(node[0]) : nil
end

#neo_helper_get_field_value(field, options = {}) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/neoid/node.rb', line 177

def neo_helper_get_field_value(field, options = {})
  if options[:block]
    options[:block].call
  else
    send(field) rescue (raise "No field #{field} for #{self.class.name}")
  end
end

#neo_load(hash) ⇒ Object



185
186
187
# File 'lib/neoid/node.rb', line 185

def neo_load(hash)
  Neoid::Node.from_hash(hash)
end

#neo_nodeObject



189
190
191
# File 'lib/neoid/node.rb', line 189

def neo_node
  _neo_representation
end

#neo_search_indexObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/neoid/node.rb', line 156

def neo_search_index
  return if self.class.neoid_config.search_options.blank? || (
    self.class.neoid_config.search_options.index_fields.blank? &&
    self.class.neoid_config.search_options.fulltext_fields.blank?
  )

  Neoid.ensure_default_fulltext_search_index

  Neoid.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, 'ar_type', self.class.name, neo_node.neo_id)

  self.class.neoid_config.search_options.fulltext_fields.each do |field, options|
    Neoid.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, "#{field}_fulltext", neo_helper_get_field_value(field, options), neo_node.neo_id)
  end

  self.class.neoid_config.search_options.index_fields.each do |field, options|
    Neoid.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, field, neo_helper_get_field_value(field, options), neo_node.neo_id)
  end

  neo_node
end