lib/neo4j/active_node/scope.rb in neo4j-4.1.5 vs lib/neo4j/active_node/scope.rb in neo4j-5.0.0.rc.1

- old
+ new

@@ -10,11 +10,11 @@ # @example without argument # class Person # include Neo4j::ActiveNode # property :name # property :score - # has_many :out, :friends, model_class: self + # has_many :out, :friends, type: :has_friend, model_class: self # scope :top_students, -> { where(score: 42)}") } # end # Person.top_students.to_a # a_person.friends.top_students.to_a # a_person.friends.friends.top_students.to_a @@ -26,34 +26,31 @@ # @example Argument as a cypher identifier # class Person # include Neo4j::ActiveNode # property :name # property :score - # has_many :out, :friends, model_class: self + # has_many :out, :friends, type: :has_friend, model_class: self # scope :great_students, ->(identifier) { where("#{identifier}.score > 41") } # end # Person.as(:all_people).great_students(:all_people).to_a # # @see http://guides.rubyonrails.org/active_record_querying.html#scopes def scope(name, proc) _scope[name.to_sym] = proc - module_eval(%{ - def #{name}(query_params=nil, _=nil, query_proxy=nil) - eval_context = ScopeEvalContext.new(self, query_proxy || self.class.query_proxy) - proc = self.class._scope[:"#{name}"] - self.class._call_scope_context(eval_context, query_params, proc) - end - }, __FILE__, __LINE__) + define_method(name) do |query_params = nil, some_var = nil| + self.class.send(name, query_params, some_var, current_scope) + end - instance_eval(%{ - def #{name}(query_params=nil, _=nil, query_proxy=nil) - eval_context = ScopeEvalContext.new(self, query_proxy || self.query_proxy) - proc = _scope[:"#{name}"] + klass = class << self; self; end + klass.instance_eval do + define_method(name) do |query_params = nil, _ = nil| + eval_context = ScopeEvalContext.new(self, current_scope || self.query_proxy) + proc = _scope[name.to_sym] _call_scope_context(eval_context, query_params, proc) end - }, __FILE__, __LINE__) + end end # rubocop:disable Style/PredicateName def has_scope?(name) ActiveSupport::Deprecation.warn 'has_scope? is deprecated and may be removed from future releases, use scope? instead.', caller @@ -85,16 +82,15 @@ def current_scope=(scope) #:nodoc: ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope) end - - def all + def all(var = :n) if current_scope - current_scope.clone + current_scope.new_link(var) else - self.as(:n) + self.as(var) end end end class ScopeEvalContext @@ -138,12 +134,12 @@ end private def raise_invalid_scope_type!(scope_type) - if !VALID_SCOPE_TYPES.include?(scope_type) - fail ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES" - end + return if VALID_SCOPE_TYPES.include?(scope_type) + + fail ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES" end end end end