directiverecord 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmQwN2FkODM3MTk4NjFkOTFhYjViYjIxYjdlYzlhZmM0YmMzZTIwMg==
4
+ M2I3ZDI1Yzk0OWViMDIxYjFlMGU2ZjM5NjVhMDY4MzI1NDQ0ZGViZA==
5
5
  data.tar.gz: !binary |-
6
- Y2RjZjMwNTUzODkxYjgwNWRmODAxNTZiZTlhZTIzZWMyZjNjNDc4Mg==
6
+ YjRkYzVkODQwNDNjYzc4ZDZmYWNlNDJhNDMyMjM5ZjhiYTVmZGVkOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NmZiMmEwMTgxNThjZGVlZjA0NjFlYTE2OGZhZWU3ZGQyMjU4NjVjNGRkYTg1
10
- MjlhNDNlYTFhMTNhMGI3MzI4YTRhZjZjYmJhNDk3M2U2NTExYjkzZGI2ZTU5
11
- MzA3MjM5ZjQ3Yzk5YTcwZWFmZWI4MDIyZmI1MTY5YmQ0NWMyMzA=
9
+ YzM2ODg4NTBjZDc4YmQ5MjExYWZiYzA2MDZhY2U5ZTY0Njg2YzAzM2YyZWFl
10
+ ZjFmNjEyM2FiZTczOTBkMjEwZmRkMTU3NTIwNDUxYzhkMDY5YWQ1NDgyNWY3
11
+ N2EzZWY0YWI0ODM1YWEwNzJlNzgwNzU5MTNhZWEzY2RlMmY2YWQ=
12
12
  data.tar.gz: !binary |-
13
- ZWYyYmY0OGI2YmQwZGZmNWIyMTEyNDA2ZDNiY2M1ZjAwN2M1ZjUyNGI5NWFh
14
- MjgwMzA0Yjg4YTk1ZmE1ZDgxMjhmZWY5OGFiNjI1NmZmYzRiMTc4OWZiMjg0
15
- ZDBkNzUxNWMwNmI4YzE0ODA3MmQ0NTAxZjBiMzE4M2E3MzQyMGE=
13
+ NGNmNzZlMDRiNGIxZDcyNDZkYTMxNmFlZTQxYzZhNzMyZWRhYjhmMGY4OGIx
14
+ Y2ZiYmQ5N2NmYmU3N2UzMmRhMWZhN2VkNWQ5NjYyZTMwZjY2ZGY5MDI5YTRk
15
+ NzNjMTBkMWRmNzhmNmQwNDhkOTQ0NTYzZmJlYTU5ODBmNzkwZDg=
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  = DirectiveRecord CHANGELOG
2
2
 
3
+ == Version 0.1.18 (March 18, 2015)
4
+
5
+ * Being able to “flatten” a subselect (in order to aggregate a subselect within a subselect)
6
+
3
7
  == Version 0.1.17 (March 13, 2015)
4
8
 
5
9
  * Corrected normalize_subselect!
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.17
1
+ 0.1.18
@@ -10,7 +10,9 @@ module DirectiveRecord
10
10
  options = to_options(args)
11
11
  validate_options! options
12
12
 
13
- original_options = options.deep_dup.reject!{|k, v| v.nil?}
13
+ original_options = options.deep_dup
14
+ original_options.reject!{|k, v| v.nil?}
15
+
14
16
  check_path_delimiter! options
15
17
  optimize_query! options
16
18
 
@@ -168,13 +170,14 @@ SQL
168
170
 
169
171
  def normalize_subselect!(options, original_options)
170
172
  options[:subselect] = options[:subselect].sort_by{|name, (klass, opts)| opts[:join] ? 0 : 1}.collect do |name, (klass, opts)|
171
- qry_options = original_options.deep_dup.reject!{|k, v| [:subselect, :numerize_aliases, :limit, :offset, :order_by].include?(k)}
173
+ qry_options = original_options.deep_dup
174
+ qry_options.reject!{|k, v| [:subselect, :numerize_aliases, :limit, :offset, :order_by, :join, :flatten].include?(k)}
172
175
 
173
176
  opts.each do |key, value|
174
177
  value = [value].flatten
175
178
  if key == :select
176
179
  qry_options[key] = value
177
- elsif key == :join
180
+ elsif [:join, :flatten].include?(key)
178
181
  # do nothing
179
182
  elsif key.to_s.match(/include_(\w+)/)
180
183
  (qry_options[$1.to_sym] || []).select!{|x| value.any?{|y| x.include?(y)}}
@@ -203,6 +206,22 @@ SQL
203
206
  end
204
207
 
205
208
  query = klass.to_qry(qry_options).gsub(/\n\s*/, " ").gsub(/#{base_alias}[\s\.]/, "")
209
+
210
+ if opts[:flatten]
211
+ qry_alias = quote_alias("_#{name}")
212
+
213
+ dup_options = qry_options.deep_dup
214
+ normalize_select!(dup_options)
215
+ prepend_base_alias!(dup_options)
216
+
217
+ select = dup_options[:select].collect do |sql|
218
+ sql_alias = sql.match(/ AS (.*?)$/).captures[0]
219
+ "SUM(#{qry_alias}.#{sql_alias}) AS #{sql_alias}"
220
+ end
221
+
222
+ query = "SELECT #{select.join(", ")} FROM (#{query}) #{qry_alias}"
223
+ end
224
+
206
225
  "#{prefix}(#{query}) #{query_alias}#{postfix}"
207
226
 
208
227
  end if options[:subselect]
@@ -1,7 +1,7 @@
1
1
  module DirectiveRecord
2
2
  MAJOR = 0
3
3
  MINOR = 1
4
- TINY = 17
4
+ TINY = 18
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join(".")
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: directiverecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Engel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-13 00:00:00.000000000 Z
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord