mongoid_query_string_interface 0.2.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid_query_string_interface (0.2.0)
4
+ mongoid_query_string_interface (0.2.4)
5
5
  json (>= 1.4.6)
6
6
  mongoid (~> 2.0.0.rc)
7
7
 
@@ -11,7 +11,8 @@ module Mongoid
11
11
  OR_OPERATOR = :or
12
12
 
13
13
  ATTRIBUTE_REGEX = /(.*)\.(#{(CONDITIONAL_OPERATORS + SORTING_OPERATORS + [OR_OPERATOR]).join('|')})/
14
-
14
+ PAGER_ATTRIBUTES = [:total_entries, :total_pages, :per_page, :offset, :previous_page, :current_page, :next_page]
15
+
15
16
  PARSERS = [
16
17
  Mongoid::QueryStringInterface::Parsers::DateTimeParser.new,
17
18
  Mongoid::QueryStringInterface::Parsers::NumberParser.new,
@@ -22,9 +23,34 @@ module Mongoid
22
23
 
23
24
  def filter_by(params={})
24
25
  params = hash_with_indifferent_access(params)
25
- filter_only_by(params).order_by(*sorting_options(params)).paginate(pagination_options(params))
26
+ filter_only_and_order_by(params).paginate(pagination_options(params))
26
27
  end
27
-
28
+
29
+ def filter_with_pagination_by(params)
30
+ result = filter_by(params)
31
+
32
+ pager = PAGER_ATTRIBUTES.inject({}) do |pager, attr|
33
+ pager[attr] = result.send(attr)
34
+ pager
35
+ end
36
+
37
+ return { :pager => pager, model_name.human.underscore.pluralize.to_sym => result }
38
+ end
39
+
40
+ def filter_with_optimized_pagination_by(params={})
41
+ params = hash_with_indifferent_access(params)
42
+ per_page = (params[:per_page] || default_pagination_options[:per_page]).to_i
43
+ page = (params[:page] || default_pagination_options[:page]).to_i
44
+ skip = per_page * (page - 1)
45
+
46
+ filter_only_and_order_by(params).skip(skip).limit(per_page)
47
+ end
48
+
49
+ def filter_only_and_order_by(params={})
50
+ params = hash_with_indifferent_access(params)
51
+ filter_only_by(params).order_by(*sorting_options(params))
52
+ end
53
+
28
54
  def filter_only_by(params={})
29
55
  where(filtering_options(hash_with_indifferent_access(params)))
30
56
  end
@@ -35,7 +61,7 @@ module Mongoid
35
61
  pagination = pagination_options(params)
36
62
  pager = WillPaginate::Collection.new pagination[:page], pagination[:per_page], where(filtering_options(params)).count
37
63
 
38
- [:total_entries, :total_pages, :per_page, :offset, :previous_page, :current_page, :next_page].inject({}) do |result, attr|
64
+ PAGER_ATTRIBUTES.inject({}) do |result, attr|
39
65
  result[attr] = pager.send(attr)
40
66
  result
41
67
  end
@@ -49,9 +75,13 @@ module Mongoid
49
75
  []
50
76
  end
51
77
 
52
- private
78
+ def default_pagination_options
79
+ { :per_page => 12, :page => 1 }
80
+ end
81
+
82
+ protected
53
83
  def pagination_options(options)
54
- options.reverse_merge :per_page => 12, :page => 1
84
+ options.reverse_merge default_pagination_options
55
85
  end
56
86
 
57
87
  def filtering_options(options)
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
3
  module QueryStringInterface #:nodoc
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.4"
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_query_string_interface
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vicente Mundim
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-01 00:00:00 -02:00
18
+ date: 2011-03-21 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency