Skip to content

Commit

Permalink
chain scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
simkim committed Nov 3, 2022
1 parent 099df50 commit e1519eb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
32 changes: 26 additions & 6 deletions lib/couchbase-orm/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,48 @@ def build_update(**cond)
end.join(", ")
end

def scoping
scopes = (Thread.current[@model.name] ||= [])
scopes.push(self)
result = yield
scopes.pop
result
end

def method_missing(method, *args, &block)
if @model.respond_to?(method)
scoping {
@model.public_send(method, *args, &block)
}
else
super
end
end
end

module ClassMethods
def relation
Thread.current[self.name]&.last || CouchbaseOrm_Relation.new(model: self)
end

def where(**conds)
CouchbaseOrm_Relation.new(model: self, where: conds)
relation.where(**conds)
end

def not(**conds)
CouchbaseOrm_Relation.new(model: self, where: conds, _not: true)
relation.not(**conds)
end

def order(*ordersl, **ordersh)
order = ordersh.reverse_merge(ordersl.map{ |o| [o, :asc] }.to_h)
CouchbaseOrm_Relation.new(model: self, order: order)
relation.order(*ordersl, **ordersh)
end

def limit(limit)
CouchbaseOrm_Relation.new(model: self, limit: limit)
relation.limit(limit)
end

def all
CouchbaseOrm_Relation.new(model: self)
relation.all
end

delegate :ids, :delete_all, :count, :empty?, :filter, :reduce, :find_by, to: :all
Expand Down
36 changes: 35 additions & 1 deletion spec/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class RelationModel < CouchbaseOrm::Base
attribute :last_name, :string
attribute :active, :boolean
attribute :age, :integer

def self.adult
where(age: {_gte: 18})
end

def self.active
where(active: true)
end

def self.test(&block)
yield
end
end

describe CouchbaseOrm::Relation do
Expand Down Expand Up @@ -215,7 +227,6 @@ class RelationModel < CouchbaseOrm::Base
expect(RelationModel.order(:age).pluck(:age, :active)).to match_array([[10, true], [20, true], [30, false]])
end


it "should query true boolean" do
m1 = RelationModel.create!(active: true)
_m2 = RelationModel.create!(active: false)
Expand Down Expand Up @@ -300,5 +311,28 @@ class RelationModel < CouchbaseOrm::Base
expect(m3.reload.age).to eq(50)
expect(m4.reload.age).to eq(40)
end
describe "scopes" do
it "should chain scopes" do
_m1 = RelationModel.create!(age: 10, active: true)
_m2 = RelationModel.create!(age: 20, active: false)
m3 = RelationModel.create!(age: 30, active: true)
m4 = RelationModel.create!(age: 40, active: true)


expect(RelationModel.all.adult.all.active.all).to match_array([m3, m4])
expect(RelationModel.where(active: true).adult).to match_array([m3, m4])
end

it "should be thread safe" do
m1 = RelationModel.create!(age: 10, active: true)
m2 = RelationModel.create!(age: 20, active: false)
RelationModel.active.test do
expect(RelationModel.all).to match_array([m1])
Thread.start do
expect(RelationModel.all).to match_array([m1, m2])
end
end
end
end
end

0 comments on commit e1519eb

Please sign in to comment.