calculate_in_group 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/lib/calculate_in_group/railtie.rb +4 -0
- data/lib/calculate_in_group/version.rb +3 -0
- data/lib/calculate_in_group.rb +91 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c5a7b900a5d475fc22804562ef0bac072706dcb251219b5be5c06c302f5dd75a
|
4
|
+
data.tar.gz: 73fa7ceb228a9704acc409ba00b99af5d307b9cfe8739a09496097481ee5b7d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80974131cdfb53dfc1279c516a61c72565ae29a3cc2bd181823298158a7fae24360e7fae377ab4e174984c2d4657650b93ac4d3b5b647f4bca084998d81667ab
|
7
|
+
data.tar.gz: 0d6c1989fa40e2dd60e77f1897467a05dd36caf2ee581d60884e176688c0bf501c7ecb0cc940c3eb04ef25bef5f04c2689e51a70db07ead31925ebd2ae55e027
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022 Igor Kasyanchuk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# CalculateInGroup
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "calculate_in_group"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install calculate_in_group
|
22
|
+
```
|
23
|
+
|
24
|
+
## Testing
|
25
|
+
|
26
|
+
ruby test/calculate_in_group_test.rb
|
27
|
+
|
28
|
+
Not sure, why rake test doesn't works for me :)
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
Contribution directions go here.
|
32
|
+
|
33
|
+
## License
|
34
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require "calculate_in_group/version"
|
2
|
+
require "calculate_in_group/railtie"
|
3
|
+
|
4
|
+
module CalculateInGroup
|
5
|
+
module QueryMethods
|
6
|
+
def calculate_in_group(operation_type, field, groups = {}, options = {})
|
7
|
+
# check if arguments are good
|
8
|
+
raise ArgumentError.new("Operation #{operation_type} not supported. Try to use: :count, :average, :sum, :maximum, :minimum") if ![:count, :average, :sum, :maximum, :minimum].include?(operation_type.to_s.to_sym)
|
9
|
+
raise ArgumentError.new("Column #{field} not found in `#{table_name}`") if !column_names.include?(field.to_s)
|
10
|
+
|
11
|
+
# init variables
|
12
|
+
table = self.arel_table
|
13
|
+
group_field = "__#{field}"
|
14
|
+
operation_attribute = "__#{operation_type}_all"
|
15
|
+
query = Arel::Nodes::Case.new
|
16
|
+
conditions = []
|
17
|
+
|
18
|
+
# build conditions
|
19
|
+
groups.each do |(k, v)|
|
20
|
+
c = case v
|
21
|
+
when Range
|
22
|
+
# range could be endless, so we need to compact and build correct SQL for "between"
|
23
|
+
a = table[field].gteq(v.begin) if v.begin
|
24
|
+
b = table[field].lteq(v.end) if v.end
|
25
|
+
[a, b].compact.inject(&:and)
|
26
|
+
when Array
|
27
|
+
# SQL "IN"
|
28
|
+
table[field].in(v)
|
29
|
+
else
|
30
|
+
# SQL "="
|
31
|
+
table[field].eq(v)
|
32
|
+
end
|
33
|
+
query = query.when(Arel.sql("(" + c.to_sql + ")#{__calculate_in_group_db_addon}")).then(k)
|
34
|
+
conditions.push(where(field => v))
|
35
|
+
end
|
36
|
+
|
37
|
+
# what we actually want to do? count, sum, average...., determine how we start SELECT ...
|
38
|
+
operation = if operation_type == :count
|
39
|
+
Arel.star.count.as(operation_attribute)
|
40
|
+
else
|
41
|
+
table[field].send(operation_type).as(operation_attribute)
|
42
|
+
end
|
43
|
+
|
44
|
+
# time to build query to DB
|
45
|
+
# SELECT + WHERE + GROUP + OPERATION (sum, count, etc..)
|
46
|
+
res = select([operation, query.as(group_field)])
|
47
|
+
res = res.merge(conditions.inject(&:or)) if !options[:include_nil] && conditions.any?
|
48
|
+
res = res.group(group_field)
|
49
|
+
|
50
|
+
# process result from DB#
|
51
|
+
# check if need to include "nil" value
|
52
|
+
result = res.inject({}) do |res, e|
|
53
|
+
key = e.send(group_field)
|
54
|
+
if key.nil? && options[:include_nil].present? && !!options[:include_nil] != options[:include_nil] # not boolean, when option is a label
|
55
|
+
key = options[:include_nil]
|
56
|
+
end
|
57
|
+
res[key] = e.send(operation_attribute)
|
58
|
+
res
|
59
|
+
end
|
60
|
+
|
61
|
+
# check if we need to build full hash with all grouped fields
|
62
|
+
if options.has_key?(:default_for_missing)
|
63
|
+
(groups.keys.map(&:to_s) - result.keys).each do |k|
|
64
|
+
result[k] = options[:default_for_missing]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# return :)
|
69
|
+
result
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
# this is needed for Postgres DB, it want's a bit different SQL in CASE statement
|
74
|
+
def __calculate_in_group_db_addon
|
75
|
+
adapter_type = connection.adapter_name.downcase.to_sym
|
76
|
+
case adapter_type
|
77
|
+
when :mysql, :mysql2, :sqlite3, :sqlite
|
78
|
+
nil # should work normally
|
79
|
+
when :postgresql
|
80
|
+
"::boolean"
|
81
|
+
else
|
82
|
+
raise NotImplementedError, "Unsupported adapter type '#{adapter_type}'"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
ActiveSupport.on_load(:active_record) do
|
90
|
+
ActiveRecord::Base.extend(CalculateInGroup::QueryMethods)
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calculate_in_group
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Rails active record grouping with range
|
84
|
+
email:
|
85
|
+
- igorkasyanchuk@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- lib/calculate_in_group.rb
|
94
|
+
- lib/calculate_in_group/railtie.rb
|
95
|
+
- lib/calculate_in_group/version.rb
|
96
|
+
homepage: https://github.com/igorkasyanchuk/calculate_in_group
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata:
|
100
|
+
homepage_uri: https://github.com/igorkasyanchuk/calculate_in_group
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.3.3
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Rails active record grouping with range
|
120
|
+
test_files: []
|