postgres_ext 2.1.2 → 2.1.3

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: f41de74b7eea3b27f6770cb352ba70497d94513d
4
- data.tar.gz: 3e9cf324c7a635ac338fd5af691909ed13f1f02e
3
+ metadata.gz: 81e5d99adec7bf0164b0e2fc17902e80ef62d264
4
+ data.tar.gz: ca7ab1ff4f77d91b31062130e17e87a055db719c
5
5
  SHA512:
6
- metadata.gz: 5843378f753e2755c7c8459d89b580d10089d564b18ee390e349a3590efb0260bb12f6e9ef784bb02ef2514389351b303d867dbe7de4de859ac16ffdd88925ab
7
- data.tar.gz: cf54fb746947851736f94fbf0e543eae772dfa7e85094c343853f12086c0c3c2c906ae2f15b4412de00fed0f1c749279a51a15a004fe8c5f07de9bdbadc05092
6
+ metadata.gz: 029878e575cb456781db615deb004839836a5bb46ccbd3a14c184b8e9b3b153c21c22b921e1938ed82378c9fc7f409799e882f623ff1913fb4dbd46e618685ca
7
+ data.tar.gz: 30a7dd666faf99922130a77cad0f25a148bc030b152ede31722475fe0c67addc29f08bf3f479a5e727ab3ff560667970ed96cf07f9ca2ad57d5039acdf8d78d7
@@ -67,7 +67,7 @@ module ActiveRecord
67
67
  end
68
68
  end
69
69
 
70
- [:with, :rank].each do |name|
70
+ [:with].each do |name|
71
71
  class_eval <<-CODE, __FILE__, __LINE__ + 1
72
72
  def #{name}_values # def select_values
73
73
  @values[:#{name}] || [] # @values[:select] || []
@@ -80,6 +80,19 @@ module ActiveRecord
80
80
  CODE
81
81
  end
82
82
 
83
+ [:rank].each do |name|
84
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
85
+ def #{name}_value=(value) # def readonly_value=(value)
86
+ raise ImmutableRelation if @loaded # raise ImmutableRelation if @loaded
87
+ @values[:#{name}] = value # @values[:readonly] = value
88
+ end # end
89
+
90
+ def #{name}_value # def readonly_value
91
+ @values[:#{name}] # @values[:readonly]
92
+ end # end
93
+ CODE
94
+ end
95
+
83
96
  def with(*args)
84
97
  check_if_method_has_arguments!('with', args)
85
98
  spawn.with!(*args.compact.flatten)
@@ -94,8 +107,8 @@ module ActiveRecord
94
107
  spawn.ranked! options
95
108
  end
96
109
 
97
- def ranked!(*args)
98
- self.rank_values += args
110
+ def ranked!(value)
111
+ self.rank_value = value
99
112
  self
100
113
  end
101
114
 
@@ -104,7 +117,7 @@ module ActiveRecord
104
117
 
105
118
  build_with(arel, with_values)
106
119
 
107
- build_rank(arel, rank_values)
120
+ build_rank(arel, rank_value) if rank_value
108
121
 
109
122
  arel
110
123
  end
@@ -129,24 +142,27 @@ module ActiveRecord
129
142
  arel.with with_statements unless with_statements.empty?
130
143
  end
131
144
 
132
- def build_rank(arel, ranks)
145
+ def build_rank(arel, rank_window_options)
133
146
  unless arel.projections.count == 1 && Arel::Nodes::Count === arel.projections.first
134
- rank_orders = ranks.uniq.reject(&:blank?).flat_map do |value|
135
- case value
147
+ rank_window = case rank_window_options
136
148
  when :order
137
149
  arel.orders
138
150
  when Symbol
139
- table[value].asc
151
+ table[rank_window_options].asc
140
152
  when Hash
141
- value.map { |field, dir| table[field].send(dir) }
153
+ rank_window_options.map { |field, dir| table[field].send(dir) }
142
154
  else
143
- Arel::Nodes::SqlLiteral.new value
155
+ Arel::Nodes::SqlLiteral.new "(#{rank_window_options})"
144
156
  end
145
- end
146
157
 
147
- unless rank_orders.blank?
158
+ unless rank_window.blank?
148
159
  rank_node = Arel::Nodes::SqlLiteral.new 'rank()'
149
- window = Arel::Nodes::Window.new.order(rank_orders)
160
+ window = Arel::Nodes::Window.new
161
+ if String === rank_window
162
+ window = window.frame rank_window
163
+ else
164
+ window = window.order(rank_window)
165
+ end
150
166
  over_node = Arel::Nodes::Over.new rank_node, window
151
167
 
152
168
  arel.project(over_node)
@@ -3,9 +3,10 @@ require 'arel/visitors/to_sql'
3
3
  module Arel
4
4
  module Visitors
5
5
  class ToSql
6
- def visit_Array o
7
- if last_column.respond_to?(:array) && last_column.array
8
- quoted o
6
+ def visit_Array o, a
7
+ column = a.relation.engine.columns.find { |col| col.name == a.name.to_s } if a
8
+ if column && column.respond_to?(:array) && column.array
9
+ quoted o, a
9
10
  else
10
11
  o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
11
12
  end
@@ -4,33 +4,33 @@ module Arel
4
4
  class Visitor
5
5
  # We are adding our visitors to the main visitor for the time being until the right spot is found to monkey patch
6
6
  private
7
- def visit_Arel_Nodes_ContainedWithin o
8
- "#{visit o.left} << #{visit o.right}"
7
+ def visit_Arel_Nodes_ContainedWithin o, a = nil
8
+ "#{visit o.left, a} << #{visit o.right, o.left}"
9
9
  end
10
10
 
11
- def visit_Arel_Nodes_ContainedWithinEquals o
12
- "#{visit o.left} <<= #{visit o.right}"
11
+ def visit_Arel_Nodes_ContainedWithinEquals o, a = nil
12
+ "#{visit o.left, a} <<= #{visit o.right, o.left}"
13
13
  end
14
14
 
15
- def visit_Arel_Nodes_Contains o
15
+ def visit_Arel_Nodes_Contains o, a = nil
16
16
  left_column = o.left.relation.engine.columns.find { |col| col.name == o.left.name.to_s }
17
17
 
18
18
  if left_column && left_column.respond_to?(:array) && left_column.array
19
- "#{visit o.left} @> #{visit o.right}"
19
+ "#{visit o.left, a} @> #{visit o.right, o.left}"
20
20
  else
21
- "#{visit o.left} >> #{visit o.right}"
21
+ "#{visit o.left, a} >> #{visit o.right, o.left}"
22
22
  end
23
23
  end
24
24
 
25
- def visit_Arel_Nodes_ContainsEquals o
26
- "#{visit o.left} >>= #{visit o.right}"
25
+ def visit_Arel_Nodes_ContainsEquals o, a = nil
26
+ "#{visit o.left, a} >>= #{visit o.right, o.left}"
27
27
  end
28
28
 
29
- def visit_Arel_Nodes_Overlap o
30
- "#{visit o.left} && #{visit o.right}"
29
+ def visit_Arel_Nodes_Overlap o, a = nil
30
+ "#{visit o.left, a} && #{visit o.right, o.left}"
31
31
  end
32
32
 
33
- def visit_IPAddr value
33
+ def visit_IPAddr value, a = nil
34
34
  "'#{value.to_s}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}'"
35
35
  end
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module PostgresExt
2
- VERSION = '2.1.2'
2
+ VERSION = '2.1.3'
3
3
  end
data/postgres_ext.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.version = PostgresExt::VERSION
18
18
 
19
19
  gem.add_dependency 'activerecord', '~> 4.0.0'
20
- gem.add_dependency 'arel', '~> 4.0.0'
20
+ gem.add_dependency 'arel', '~> 4.0.1'
21
21
  gem.add_dependency 'pg_array_parser', '~> 0.0.9'
22
22
 
23
23
  gem.add_development_dependency 'rails', '~> 4.0.0'
@@ -23,8 +23,8 @@ describe 'Window functions' do
23
23
  end
24
24
 
25
25
  it 'uses the rank value when a string passed to it' do
26
- query = Person.ranked('lucky_number desc')
27
- query.to_sql.should eq 'SELECT "people".*, rank() OVER (ORDER BY lucky_number desc) FROM "people"'
26
+ query = Person.ranked('ORDER BY lucky_number DESC')
27
+ query.to_sql.should eq 'SELECT "people".*, rank() OVER (ORDER BY lucky_number DESC) FROM "people"'
28
28
  end
29
29
 
30
30
  it 'combines the order and rank' do
@@ -44,6 +44,11 @@ describe 'Window functions' do
44
44
  it 'does not apply the rank when performing a count' do
45
45
  query = Person.ranked(lucky_number: :desc).count
46
46
  query.should eq 0
47
+
48
+ Person.create!
49
+
50
+ query = Person.ranked(lucky_number: :desc).count
51
+ query.should eq 1
47
52
  end
48
53
  end
49
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan McClain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-09 00:00:00.000000000 Z
11
+ date: 2013-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 4.0.0
33
+ version: 4.0.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 4.0.0
40
+ version: 4.0.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pg_array_parser
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
236
  version: '0'
237
237
  requirements: []
238
238
  rubyforge_project:
239
- rubygems_version: 2.0.3
239
+ rubygems_version: 2.1.0
240
240
  signing_key:
241
241
  specification_version: 4
242
242
  summary: Extends ActiveRecord to handle native PostgreSQL data types