mongoid_query_string_interface 0.2.0 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/lib/mongoid/query_string_interface.rb +36 -6
- data/lib/version.rb +1 -1
- metadata +4 -4
data/Gemfile.lock
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
84
|
+
options.reverse_merge default_pagination_options
|
55
85
|
end
|
56
86
|
|
57
87
|
def filtering_options(options)
|
data/lib/version.rb
CHANGED
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:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
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-
|
18
|
+
date: 2011-03-21 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|