omnis 0.6.3 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'ostruct'
2
+ require 'monadic'
2
3
 
3
4
  module Omnis
4
5
  module MongoQuery
@@ -13,7 +14,7 @@ module Omnis
13
14
  module ClassMethods
14
15
  include Omnis::Query::ClassMethods
15
16
 
16
- attr_reader :page_param_name, :items_per_page
17
+ attr_reader :page_param_name, :items_per_page, :sort_param_name, :sort_default_field, :sort_default_order
17
18
  def field_list
18
19
  @fields ||= []
19
20
  end
@@ -25,6 +26,12 @@ module Omnis
25
26
  @page_param_name = page_param_name
26
27
  @items_per_page = opts[:items_per_page] || 10
27
28
  end
29
+
30
+ def sort(sort_param_name, opts={})
31
+ @sort_param_name = sort_param_name
32
+ @sort_default_field = opts[:default][0]
33
+ @sort_default_order = (opts[:default][1] || :asc).to_sym
34
+ end
28
35
  end
29
36
 
30
37
  module InstanceMethods
@@ -39,6 +46,10 @@ module Omnis
39
46
  :param_names => extracted_param_names(extracted_operators)})
40
47
  end
41
48
 
49
+ def items_per_page
50
+ self.class.items_per_page || 20
51
+ end
52
+
42
53
  private
43
54
  def page
44
55
  return 0 unless @input_params.has_key? page_param_name
@@ -55,8 +66,17 @@ module Omnis
55
66
  self.class.page_param_name || :page
56
67
  end
57
68
 
58
- def items_per_page
59
- self.class.items_per_page || 20
69
+
70
+ def sort_param_name
71
+ self.class.sort_param_name
72
+ end
73
+
74
+ def sort_default_field
75
+ self.class.sort_default_field
76
+ end
77
+
78
+ def sort_default_order
79
+ self.class.sort_default_order || :asc
60
80
  end
61
81
 
62
82
  def mongo_operator(operator)
@@ -73,10 +93,42 @@ module Omnis
73
93
  end
74
94
 
75
95
  def mongo_opts(extracted_operators)
76
- params_with_extra_fields = extracted_operators.collect(&:opts).select {|e| e.has_key? :field}
77
- extra_fields = params_with_extra_fields.map {|e| e[:field]}
78
- fields = self.class.field_list + extra_fields
79
- { :limit => items_per_page, :skip => skip, :fields => fields }
96
+ {}.tap do |opts|
97
+ params_with_extra_fields = extracted_operators.collect(&:opts).select {|e| e.has_key? :field}
98
+ extra_fields = params_with_extra_fields.map {|e| e[:field]}
99
+ opts[:fields] = self.class.field_list + extra_fields
100
+ opts[:limit] = items_per_page
101
+ opts[:skip] = skip
102
+ opts[:sort] = sort_opts if sort_opts.all?
103
+ end
104
+ end
105
+
106
+ def sort_opts
107
+ sort_args = (@input_params[sort_param_name] || '').split ','
108
+
109
+ sort_order = sort_args[1]
110
+ sort_order ||= sort_default_order
111
+ sort_order = sort_order.to_sym
112
+ sort_order = :asc unless [:asc, :desc].include? sort_order
113
+
114
+ @sort_opts = [sort_field(sort_args), sort_order]
115
+ end
116
+
117
+ def sort_field(sort_args)
118
+ sort_field = sort_args[0]
119
+ sort_field ||= sort_default_field
120
+ return nil unless sort_field
121
+
122
+ custom_sort_field = get_custom_sort_field(sort_field)
123
+
124
+ custom_sort_field ? custom_sort_field : sort_field
125
+ end
126
+
127
+ def get_custom_sort_field(sort_field)
128
+ sort_field_param = self.class.params[sort_field.to_sym]
129
+ if sort_field_param && sort_field_param.opts.has_key?(:field)
130
+ sort_field = sort_field_param.opts[:field]
131
+ end
80
132
  end
81
133
 
82
134
  def extracted_param_names(extracted_operators)
@@ -21,7 +21,7 @@ module Omnis
21
21
  def extract(params)
22
22
  value = @extractor.(params) if params.keys.include? name # only run extractor if the param was in the request (params)
23
23
  value ||= default_value # if there is a default, apply it
24
- return value if extracted_result_is_an_operator? value
24
+ return value if is_operator? value
25
25
  return @operator.new(@name, value, @opts) unless value.nil?
26
26
  end
27
27
 
@@ -32,7 +32,7 @@ module Omnis
32
32
  end
33
33
 
34
34
  private
35
- def extracted_result_is_an_operator?(value)
35
+ def is_operator?(value)
36
36
  value.is_a? Omnis::Operators::NullOperator
37
37
  end
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module Omnis
2
- VERSION = "0.6.3"
2
+ VERSION = "0.6.5"
3
3
  end
@@ -87,10 +87,76 @@ describe Omnis::MongoQuery do
87
87
  t = TestPageDefaultQuery.new({})
88
88
  t.to_mongo.opts.should == { :limit => 20, :skip => 0, :fields => []}
89
89
  end
90
+
90
91
  it "page defaults and page given" do
91
92
  t = TestPageDefaultQuery.new({"page" => 2})
92
93
  t.to_mongo.opts.should == { :limit => 20, :skip => 40, :fields => []}
93
94
  end
95
+
96
+ it "works with a custom paging param" do
97
+ class TestPageCustomPagingParamQuery
98
+ include Omnis::MongoQuery
99
+ page :fancy_pants, :items_per_page => 20
100
+ end
101
+
102
+ m = TestPageCustomPagingParamQuery.new("fancy_pants" => 2).to_mongo
103
+ m.opts.should == { :limit => 20, :skip => 40, :fields => []}
104
+ end
105
+
106
+ it "allows to get items_per_page" do
107
+ t = TestPageDefaultQuery.new({})
108
+ t.items_per_page.should == 20
109
+ end
110
+ end
111
+
112
+ context 'sorting' do
113
+ class TestSortQuery
114
+ include Omnis::MongoQuery
115
+ param :ref_provider, Equals, :field => 'services.ref_provider'
116
+ param :ref_anixe, Equals
117
+ param :name, Equals
118
+ sort :sort, :default => [:ref_anixe, :asc]
119
+ end
120
+
121
+ it 'default sort order' do
122
+ m = TestSortQuery.new({}).to_mongo
123
+ m.opts[:sort].should == [:ref_anixe, :asc]
124
+ end
125
+
126
+ it 'ascending by default' do
127
+ m = TestSortQuery.new("sort" => "name").to_mongo
128
+ m.opts[:sort].should == ['name', :asc]
129
+ end
130
+
131
+ it 'ascending for invalid sort order' do
132
+ m = TestSortQuery.new("sort" => "name,kupa").to_mongo
133
+ m.opts[:sort].should == ['name', :asc]
134
+ end
135
+
136
+ it 'ascending' do
137
+ m = TestSortQuery.new("sort" => "name,asc").to_mongo
138
+ m.opts[:sort].should == ['name', :asc]
139
+ end
140
+
141
+ it 'descending' do
142
+ m = TestSortQuery.new("sort" => "name,desc").to_mongo
143
+ m.opts[:sort].should == ['name', :desc]
144
+ end
145
+
146
+ it 'uses the supplied optional field for sorting' do
147
+ m = TestSortQuery.new("sort" => "ref_provider").to_mongo
148
+ m.opts[:sort].should == ['services.ref_provider', :asc]
149
+ end
150
+
151
+ it 'works with a custom sorting param' do
152
+ class TestSortCustomQuery
153
+ include Omnis::MongoQuery
154
+ param :name, Equals
155
+ sort :fancy_pants, :default => [:ref_anixe, :asc]
156
+ end
157
+ m = TestSortCustomQuery.new("fancy_pants" => "name,desc").to_mongo
158
+ m.opts[:sort].should == ['name', :desc]
159
+ end
94
160
  end
95
161
 
96
162
  context 'practical use case with predefined params' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omnis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport