diff --git a/README.md b/README.md index 25c0858..012b44a 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,11 @@ class Posts < Rasti::DB::Collection many_to_many :categories one_to_many :comments - query(:created_by) { |user_id| where user_id: user_id } - query(:entitled) { |title| where title: title } + query :created_by do |user_id| + where user_id: user_id + end + + query :entitled, -> (title) { where title: title } end class Comments < Rasti::DB::Collection diff --git a/lib/rasti/db/collection.rb b/lib/rasti/db/collection.rb index da01a42..1135db5 100644 --- a/lib/rasti/db/collection.rb +++ b/lib/rasti/db/collection.rb @@ -66,8 +66,8 @@ def many_to_many(name, options={}) relations[name] = Relations::ManyToMany.new name, self, options end - def query(name, &block) - queries[name] = block + def query(name, query=nil, &block) + queries[name] = query || block define_method name do |*args| result = Query.new(self.class, dataset, schema).instance_exec *args, &block diff --git a/spec/minitest_helper.rb b/spec/minitest_helper.rb index 7cb5fc3..2ac9572 100644 --- a/spec/minitest_helper.rb +++ b/spec/minitest_helper.rb @@ -21,8 +21,11 @@ class Posts < Rasti::DB::Collection many_to_many :categories one_to_many :comments - query(:created_by) { |user_id| where user_id: user_id } - query(:entitled) { |title| where title: title } + query :created_by do |user_id| + where user_id: user_id + end + + query :entitled, -> (title) { where title: title } end class Comments < Rasti::DB::Collection