mongoid_hash_query 0.2.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38cb5283bf7399e66d57f5040f3575d3982ddc98
4
- data.tar.gz: 3a3a92fd0fe6a84c51362a0da71f25446c10cb27
3
+ metadata.gz: e0f4f38de5ee2165660a982868d31884d464d41b
4
+ data.tar.gz: bd64881083350d33433cba569cc96ff9f3078413
5
5
  SHA512:
6
- metadata.gz: c7c6c5b45715c6352d487a5f4b7baa2bf7a3c194136e7951bf77c650646681001977cf295927246d8fa7c3758615cf6db45944020a1f99332520084e2c2d9cbe
7
- data.tar.gz: 9e8a7e0d5881d1e0f11f4159723b21ccfad4c2eaade7c89fd5807da2432a58849e865d8789f515469e4e96c875ae95be74339b589f629c3e7359681382d3bc03
6
+ metadata.gz: 0bc9122b1edd0d2bffea199c61705ba2dd05fab09fb100f33a39f541e12558c71ebb0e65f79bd19d26726163cdcec750bf15c2a72414c8ae01328c55a61586c8
7
+ data.tar.gz: 4e067d3e87dc3ba701401253fd66ca17319cffb62ff11f9f84712e2b77f362b84c2de752dcf865b455a35872590a3635e0d064030719192a440314447c3995e8
data/README.md CHANGED
@@ -19,6 +19,8 @@ on unknown attributes (attributes not returned from the API) by brute forcing
19
19
  which might or might not be a security issue. If you don't like that check
20
20
  [whitelisting](https://github.com/kollegorna/mongoid_hash_query#whitelisting)._
21
21
 
22
+ *New*! You can now do [__aggregation queries__](#aggregation-queries).
23
+
22
24
  ## Installation
23
25
 
24
26
  Add this line to your application's Gemfile:
@@ -91,6 +93,9 @@ Soon you will be able to send in regex..
91
93
  You can apply an equality filter on hashes like that:
92
94
  * `{example_field: {foo: 'bar', bar: 'foo'}}`
93
95
 
96
+ Or you can apply a regex in example_field.foo like that:
97
+ * `{example_field: {foo: {regex: true, value: 'bar', ignore_case: true}, bar: 'foo'}}`
98
+
94
99
 
95
100
  ### Limit
96
101
  A limit param defines the number of returned resources. For instance:
@@ -140,6 +145,31 @@ end
140
145
  `Api::V1::ResourceFilter.new(resource, params[resource]).apply_filters` will be
141
146
  called to apply the filters in resource association.~~
142
147
 
148
+ ## Aggregation Queries
149
+ Sometimes we need to ask the database queries that act on the collection but don't want back an array of elements but a value instead! Now you can do that by simply calling the aggregations method inside the controller:
150
+
151
+ ```ruby
152
+ aggregations(resource, {
153
+ aggregate: {
154
+ integer_column: { avg: true, max: true, min: true, sum: true },
155
+ float_column: {avg: true, max: true, min: true },
156
+ datetime_column: { max: true, min: true }
157
+ }
158
+ })
159
+ ```
160
+
161
+ and you will get a hash (HashWithIndifferentAccess) back that holds all your aggregations like:
162
+ ```ruby
163
+ {
164
+ aggregations: {
165
+ "float_column"=>{"avg"=>25.5, "max"=>50, "min"=>1},
166
+ "integer_column"=>{"avg"=>4.38, "sum"=>219, "max"=>9, "min"=>0},
167
+ "datetime_at"=>{"max"=>2015-06-11 20:59:14 UTC, "min"=>2015-06-11 20:59:12 UTC}
168
+ }
169
+ }
170
+ ```
171
+
172
+ These attributes usually go to the "meta" section of your serializer. In that way it's easy to parse them in the front-end (for ember check [here](http://guides.emberjs.com/v1.10.0/models/handling-metadata/)). Please note that you should apply the aggregations __after__ you apply the filters (if there any) but __before__ you apply pagination!
143
173
 
144
174
 
145
175
  ## Contributing
@@ -1,10 +1,13 @@
1
1
  require 'mongoid_hash_query/version'
2
+ require 'mongoid_hash_query/helpers'
2
3
  require 'mongoid_hash_query/field_filters'
3
4
  require 'mongoid_hash_query/limit_filters'
4
5
  require 'mongoid_hash_query/sort_filters'
5
6
  require 'mongoid_hash_query/scope_filters'
6
7
  require 'mongoid_hash_query/filter_applier'
7
8
 
9
+ require 'mongoid_hash_query/aggregation'
10
+
8
11
  module MongoidHashQuery
9
12
  class << self
10
13
  attr_accessor :configuration
@@ -30,6 +33,10 @@ module MongoidHashQuery
30
33
  ).apply_filters
31
34
  end
32
35
 
36
+ def aggregations(resource, params)
37
+ Aggregation.new(resource, params).apply
38
+ end
39
+
33
40
  class Configuration
34
41
  attr_accessor :has_filter_classes, :filter_class_prefix, :filter_class_suffix
35
42
  end
@@ -0,0 +1,68 @@
1
+ module MongoidHashQuery
2
+ class Aggregation
3
+ include Helpers
4
+
5
+ attr_reader :configuration, :params, :resource, :model
6
+
7
+ def initialize(resource, params, model: nil)
8
+ @configuration = Module.nesting.last.configuration
9
+ @resource = resource
10
+ @params = HashWithIndifferentAccess.new(params)
11
+ @model = model
12
+
13
+ unless @model
14
+ @model = model_class_name(@resource)
15
+ end
16
+ end
17
+
18
+ def apply
19
+ if params[:aggregate].is_a? Hash
20
+ meta_attributes = HashWithIndifferentAccess.new
21
+
22
+ params[:aggregate].each do |field, asked_aggrs|
23
+ #next if @model.fields[field].nil? #allow on embedded documents too
24
+ next unless params[:aggregate][field].is_a? Hash
25
+
26
+ if @model.fields[field]
27
+ case @model.fields[field].options[:type].to_s.downcase.to_sym
28
+ when :integer, :float, :bigdecimal
29
+ meta_attributes[field] = apply_aggregations(
30
+ {avg: :avg, sum: :sum, max: :max, min: :min},
31
+ params[:aggregate][field],
32
+ field
33
+ )
34
+
35
+ when :date, :datetime, :time, :timewithzone,
36
+ meta_attributes[field] = apply_aggregations(
37
+ {max: :max, min: :min},
38
+ params[:aggregate][field],
39
+ field
40
+ )
41
+ end
42
+ else
43
+ meta_attributes[field] = apply_aggregations(
44
+ {avg: :avg, sum: :sum, max: :max, min: :min},
45
+ params[:aggregate][field],
46
+ field
47
+ )
48
+ end
49
+ end
50
+ end
51
+
52
+ return {aggregations: meta_attributes}
53
+ end
54
+
55
+ def apply_aggregations(available_aggr, asked_aggr, field)
56
+ meta_attributes = HashWithIndifferentAccess.new
57
+
58
+ available_aggr.each do |k, v|
59
+ if asked_aggr[k]
60
+ meta_attributes[k] = resource.send(v,field)
61
+ meta_attributes[k] = meta_attributes[k].to_f if meta_attributes[k].is_a? BigDecimal
62
+ end
63
+ end
64
+
65
+ return meta_attributes
66
+ end
67
+ end
68
+ end
@@ -29,7 +29,11 @@ module MongoidHashQuery::FieldFilters
29
29
 
30
30
  def filter_hash(resource, field, param)
31
31
  param.each do |k, v|
32
- resource = resource.where("#{field}.#{k}" => v)
32
+ if v.is_a?(Hash) && v[:regex]
33
+ resource = resource.where("#{field}.#{k}" => Regexp.new(v[:value], v[:ignore_case]))
34
+ else
35
+ resource = resource.where("#{field}.#{k}" => v)
36
+ end
33
37
  end
34
38
 
35
39
  return resource
@@ -1,5 +1,6 @@
1
1
  module MongoidHashQuery
2
2
  class FilterApplier
3
+ include Helpers
3
4
  include FieldFilters
4
5
  #include AssociationFilters
5
6
  include ScopeFilters
@@ -52,6 +53,8 @@ module MongoidHashQuery
52
53
  @resource = filter_symbol(@resource, v.name, @params[v.name])
53
54
  when Time.to_s
54
55
  @resource = filter_time(@resource, v.name, @params[v.name])
56
+ else
57
+ @resource = @resource.where(v.name => @params[v.name])
55
58
  end
56
59
  end
57
60
 
@@ -66,11 +69,5 @@ module MongoidHashQuery
66
69
  return @resource
67
70
  end
68
71
 
69
- def filter_class(resource_name)
70
- end
71
-
72
- def model_class_name(resource)
73
- resource.klass
74
- end
75
72
  end
76
73
  end
@@ -0,0 +1,7 @@
1
+ module MongoidHashQuery
2
+ module Helpers
3
+ def model_class_name(resource)
4
+ resource.klass
5
+ end
6
+ end
7
+ end
@@ -9,4 +9,3 @@ module MongoidHashQuery::ScopeFilters
9
9
  return resource
10
10
  end
11
11
  end
12
-
@@ -1,3 +1,3 @@
1
1
  module MongoidHashQuery
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_hash_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filippos Vasilakis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-03 00:00:00.000000000 Z
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -57,8 +57,10 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/mongoid_hash_query.rb
60
+ - lib/mongoid_hash_query/aggregation.rb
60
61
  - lib/mongoid_hash_query/field_filters.rb
61
62
  - lib/mongoid_hash_query/filter_applier.rb
63
+ - lib/mongoid_hash_query/helpers.rb
62
64
  - lib/mongoid_hash_query/limit_filters.rb
63
65
  - lib/mongoid_hash_query/scope_filters.rb
64
66
  - lib/mongoid_hash_query/sort_filters.rb