Skip to content
Browse files

Adding support for restricting the fields returned using only or exce…

…pt parameters
  • Loading branch information...
1 parent daf0ba6 commit 45e3bcabb838e1cc52d5215894a1c772bd596ea5 @vicentemundim committed Jun 20, 2012
View
32 Gemfile.lock
@@ -1,38 +1,40 @@
PATH
remote: .
specs:
- mongoid_query_string_interface (0.6.2)
+ mongoid_query_string_interface (0.7.0)
mongoid (>= 2.0.2)
- query_string_interface (>= 0.1.0)
+ query_string_interface (>= 0.5.0)
will_paginate (~> 3.0.pre2)
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.9)
- activesupport (= 3.0.9)
- builder (~> 2.1.2)
- i18n (~> 0.5.0)
- activesupport (3.0.9)
+ activemodel (3.2.6)
+ activesupport (= 3.2.6)
+ builder (~> 3.0.0)
+ activesupport (3.2.6)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
archive-tar-minitar (0.5.2)
bson (1.3.1)
bson_ext (1.3.1)
- builder (2.1.2)
+ builder (3.0.0)
columnize (0.3.4)
database_cleaner (0.6.7)
diff-lcs (1.1.2)
- i18n (0.5.0)
+ i18n (0.6.0)
linecache (0.46)
rbx-require-relative (> 0.0.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mongo (1.3.1)
bson (>= 1.3.1)
- mongoid (2.0.2)
- activemodel (~> 3.0)
- mongo (~> 1.3)
+ mongoid (2.4.11)
+ activemodel (~> 3.1)
+ mongo (<= 1.6.2)
tzinfo (~> 0.3.22)
- query_string_interface (0.1.0)
+ multi_json (1.3.6)
+ query_string_interface (0.5.0)
activesupport (>= 3.0.0)
rake (0.8.7)
rbx-require-relative (0.0.5)
@@ -62,8 +64,8 @@ GEM
simplecov (0.4.2)
simplecov-html (~> 0.4.4)
simplecov-html (0.4.5)
- tzinfo (0.3.29)
- will_paginate (3.0.pre2)
+ tzinfo (0.3.33)
+ will_paginate (3.0.3)
PLATFORMS
ruby
View
14 lib/mongoid/query_string_interface.rb
@@ -36,7 +36,8 @@ def filter_only_and_order_by(params={})
end
def filter_only_by(params={})
- where(filtering_options(hash_with_indifferent_access(params)))
+ params = hash_with_indifferent_access(params)
+ where(filtering_options(params)).filter_fields_by(params)
end
def paginated_collection_with_filter_by(params={})
@@ -48,6 +49,17 @@ def paginated_collection_with_filter_by(params={})
build_pager_from(collection)
end
+ def filter_fields_by(params)
+ params = field_filtering_options(hash_with_indifferent_access(params))
+ params.present? ? send(*params.first) : criteria
+ end
+
+ def field_filtering_options(params)
+ super.tap do |hash|
+ hash[:without] = hash.delete(:except) if hash.present?
+ end
+ end
+
protected
def sorting_options(options)
super(options).map do |sort_option|
View
2 lib/version.rb
@@ -1,6 +1,6 @@
# encoding: utf-8
module Mongoid #:nodoc
module QueryStringInterface #:nodoc
- VERSION = "0.6.2"
+ VERSION = "0.7.0"
end
end
View
2 mongoid_query_string_interface.gemspec
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "mongoid_query_string_interface"
- s.add_runtime_dependency("query_string_interface", [">= 0.1.0"])
+ s.add_runtime_dependency("query_string_interface", [">= 0.5.0"])
s.add_runtime_dependency("will_paginate", ["~> 3.0.pre2"])
s.add_runtime_dependency("mongoid", [">= 2.0.2"])
View
18 spec/mongoid/query_string_interface_spec.rb
@@ -154,6 +154,7 @@ class EmbeddedDocument
@context = mock('context')
Document.stub!(:where).and_return(@context)
@context.stub!(:order_by).and_return(@context)
+ @context.stub!(:filter_fields_by).and_return(@context)
end
it "should add a paginate method to the document" do
@@ -267,6 +268,7 @@ def self.paginate(options)
criteria.stub!(:where).and_return(criteria)
criteria.stub!(:order_by).and_return(criteria)
criteria.stub!(:paginate).and_return(criteria)
+ criteria.stub!(:filter_fields_by).and_return(criteria)
criteria
end
@@ -462,6 +464,22 @@ def self.paginate(options)
end
end
end
+
+ describe "when filtering fields" do
+ describe "with only" do
+ it "should only return the specified fields" do
+ document = Document.filter_by('only' => 'title|_id').first
+ document.attributes.should == {"_id" => document.id, "title" => document.title}
+ end
+ end
+
+ describe "with except" do
+ it "should return the all fields except the specified fields" do
+ document = Document.filter_by('except' => 'title').first
+ document.attributes.should == document.reload.attributes.except('title')
+ end
+ end
+ end
end
describe 'when returning paginated collection' do

0 comments on commit 45e3bca

Please sign in to comment.
Something went wrong with that request. Please try again.