lib/active_graph/relationship/query.rb in activegraph-11.5.0.beta.3 vs lib/active_graph/relationship/query.rb in activegraph-12.0.0.beta.1

- old
+ new

@@ -4,22 +4,21 @@ class RecordNotFound < ActiveGraph::RecordNotFound; end module ClassMethods # Returns the object with the specified neo4j id. - # @param [String,Integer] id of node to find + # @param [String] id of node to find def find(id) - fail "Unknown argument #{id.class} in find method (expected String or Integer)" if !(id.is_a?(String) || id.is_a?(Integer)) - find_by_id(id) + fail "Unknown argument #{id.class} in find method (expected String)" unless [Integer, String].any?(&id.method(:is_a?)) + find_by_id(id) || fail(RecordNotFound.new("Couldn't find #{name} with 'id'=#{id.inspect}", name, id)) end # Loads the relationship using its neo_id. def find_by_id(key) query = ActiveGraph::Base.new_query - result = query.match('()-[r]-()').where('ID(r)' => key.to_i).limit(1).return(:r).first - fail RecordNotFound.new("Couldn't find #{name} with 'id'=#{key.inspect}", name, key) if result.blank? - result[:r] + result = query.match('()-[r]-()').where("r.#{id_property_name}" => key).limit(1).return(:r).first + result&.send(:[], :r) end # Performs a very basic match on the relationship. # This is not executed lazily, it will immediately return matching objects. # To use a string, prefix the property with "r1" @@ -27,22 +26,26 @@ # MyRelClass.where('r1.grade > r1') def where(args = {}) where_query.where(where_string(args)).pluck(:r1) end + def find_by(args) + where(args).first + end + # Performs a basic match on the relationship, returning all results. # This is not executed lazily, it will immediately return matching objects. def all all_query.pluck(:r1) end def first - all_query.limit(1).order('ID(r1)').pluck(:r1).first + all_query.limit(1).order('r1.created_at').pluck(:r1).first end def last - all_query.limit(1).order('ID(r1) DESC').pluck(:r1).first + all_query.limit(1).order('r1.created_at DESC').pluck(:r1).first end private def deprecation_warning! @@ -87,10 +90,12 @@ end def where_string(args) case args when Hash - args.map { |k, v| v.is_a?(Integer) ? "r1.#{k} = #{v}" : "r1.#{k} = '#{v}'" }.join(', ') + args.transform_keys { |key| key == :neo_id ? 'elementId(r1)' : "r1.#{key}" } + .transform_values { |v| v.is_a?(Integer) ? v : "'#{v}'" } + .map { |k, v| "#{k} = #{v}" }.join(', ') else args end end end