torque-postgresql 0.2.5 → 0.2.6

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: 82df626662b3d4e0cbc9a9eb9df27405088ae7df
4
- data.tar.gz: af5edbbbbfada68697ef0747e955534a553291b1
3
+ metadata.gz: f01743d0b17376aff113086782016f688330d7fe
4
+ data.tar.gz: bf8ab5b19862bef0a158d5cf17575960f6385b7e
5
5
  SHA512:
6
- metadata.gz: a7efb1f2e7ef4285356576f5d4459a8f2c370315ee7d8d182c0164ad830171ecd1fe257ff61f9f526fc665bf6f291b988079c962117903171d6b85015b60f910
7
- data.tar.gz: 70dc80abe0b1de951c8337ac87e38f473affe061060eff9b73be7009ad3ed208bef18f12637abc15c5150d2b7589ded0a03a1fc6772cc2f230dfaeb72b62a75f
6
+ metadata.gz: 9aab3f50ac0d003ffe4876aef773afe975739eceb2e4552e0789d864b2c6e52669f25ef3bcf30f9c2aed48e9628d01137763fea0fa005d97d538f2974f464ad2
7
+ data.tar.gz: 60a4e728532be8c9ae45448192762ac4755a8157dc299495d5087a11d80a6f8a3315400eccd11b0f2c471a928406de642dd6af2cd75164b7169c9ae1763c7362
@@ -1,4 +1,3 @@
1
1
  require_relative 'arel/join_source'
2
2
  require_relative 'arel/select_manager'
3
3
  require_relative 'arel/visitors'
4
- require_relative 'arel/using'
@@ -3,12 +3,6 @@ module Torque
3
3
  module Arel
4
4
  module SelectManager
5
5
 
6
- def using column
7
- column = ::Arel::Nodes::SqlLiteral.new(column.to_s)
8
- @ctx.source.right.last.right = Using.new(column)
9
- self
10
- end
11
-
12
6
  def only
13
7
  @ctx.source.only = true
14
8
  end
@@ -16,13 +16,6 @@ module Torque
16
16
  super
17
17
  end
18
18
 
19
- # Add USING modifier to query
20
- def visit_Torque_PostgreSQL_Arel_Using(o, collector)
21
- collector << 'USING ( '
22
- collector << o.expr
23
- collector << ' )'
24
- end
25
-
26
19
  end
27
20
 
28
21
  ::Arel::Visitors::PostgreSQL.include Visitors
@@ -9,7 +9,9 @@ module Torque
9
9
  LAZY_VALUE = 0.chr
10
10
 
11
11
  class << self
12
- delegate :each, :sample, to: :values
12
+ include Enumerable
13
+
14
+ delegate :each, :sample, to: :members
13
15
 
14
16
  # Find or create the class that will handle the value
15
17
  def lookup(name)
@@ -40,6 +42,11 @@ module Torque
40
42
  end
41
43
  end
42
44
 
45
+ # Different from values, it returns the list of items already casted
46
+ def members
47
+ values.dup.map(&method(:new))
48
+ end
49
+
43
50
  # Fetch a value from the list
44
51
  # see https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/fixtures.rb#L656
45
52
  # see https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/validations/uniqueness.rb#L101
@@ -14,9 +14,22 @@ module Torque
14
14
  include Inheritance
15
15
 
16
16
  SINGLE_VALUE_METHODS = [:itself_only]
17
- MULTI_VALUE_METHODS = [:distinct_on, :auxiliary_statements, :cast_records]
17
+ MULTI_VALUE_METHODS = [:distinct_on, :auxiliary_statements, :cast_records,
18
+ :dynamic_selection]
18
19
  VALUE_METHODS = SINGLE_VALUE_METHODS + MULTI_VALUE_METHODS
19
20
 
21
+ # :nodoc:
22
+ def dynamic_selection_values; get_value(:dynamic_selection); end
23
+ # :nodoc:
24
+ def dynamic_selection_values=(value); set_value(:dynamic_selection, value); end
25
+
26
+ # Resolve column name when calculating models, allowing the column name to
27
+ # be more complex while keeping the query selection quality
28
+ def calculate(operation, column_name)
29
+ column_name = resolve_column(column_name).first if column_name.is_a?(Hash)
30
+ super(operation, column_name)
31
+ end
32
+
20
33
  # Resolve column definition up to second value.
21
34
  # For example, based on Post model:
22
35
  #
@@ -65,6 +78,16 @@ module Torque
65
78
 
66
79
  private
67
80
 
81
+ def dynamic_selection
82
+ @dynamic_selection ||= []
83
+ end
84
+
85
+ def build_arel
86
+ arel = super
87
+ arel.project(*dynamic_selection) if select_values.blank? && dynamic_selection.any?
88
+ arel
89
+ end
90
+
68
91
  # Compatibility method with 5.0
69
92
  unless ActiveRecord::Relation.method_defined?(:get_value)
70
93
  def get_value(name)
@@ -108,7 +131,7 @@ module Torque
108
131
  ActiveRecord::Relation::MULTI_VALUE_METHODS += Relation::MULTI_VALUE_METHODS
109
132
  ActiveRecord::Relation::VALUE_METHODS += Relation::VALUE_METHODS
110
133
  ActiveRecord::QueryMethods::VALID_UNSCOPING_VALUES += [:cast_records, :itself_only,
111
- :distinct_on, :auxiliary_statements]
134
+ :distinct_on, :auxiliary_statements, :dynamic_selection]
112
135
 
113
136
  if ActiveRecord::QueryMethods.const_defined?('DEFAULT_VALUES')
114
137
  Relation::SINGLE_VALUE_METHODS.each do |value|
@@ -59,7 +59,7 @@ module Torque
59
59
 
60
60
  columns.flatten!
61
61
  arel.with(subqueries.flatten)
62
- arel.project(*columns) if columns.any?
62
+ dynamic_selection.concat(columns) if columns.any?
63
63
  end
64
64
 
65
65
  # Throw an error showing that an auxiliary statement of the given
@@ -73,19 +73,19 @@ module Torque
73
73
  end
74
74
 
75
75
  columns.push(build_auto_caster_marker(arel, self.cast_records_value))
76
- arel.project(*columns) if columns.any?
76
+ dynamic_selection.concat(columns) if columns.any?
77
77
  end
78
78
 
79
79
  # Build as many left outer join as necessary for each dependent table
80
80
  def build_inheritances_joins(arel, types)
81
81
  columns = Hash.new{ |h, k| h[k] = [] }
82
- primary_key = quoted_primary_key
82
+ base_on_key = model.arel_table[primary_key]
83
83
  base_attributes = model.attribute_names
84
84
 
85
85
  # Iterate over each casted dependent calculating the columns
86
86
  types.each.with_index do |model, idx|
87
87
  join_table = model.arel_table.alias("\"i_#{idx}\"")
88
- arel.outer_join(join_table).using(primary_key)
88
+ arel.outer_join(join_table).on(base_on_key.eq(join_table[primary_key]))
89
89
  (model.attribute_names - base_attributes).each do |column|
90
90
  columns[column] << join_table
91
91
  end
@@ -6,6 +6,7 @@ module Torque
6
6
  def merge # :nodoc:
7
7
  super
8
8
 
9
+ merge_dynamic_selection
9
10
  merge_distinct_on
10
11
  merge_auxiliary_statements
11
12
  merge_inheritance
@@ -15,6 +16,11 @@ module Torque
15
16
 
16
17
  private
17
18
 
19
+ # Merge dynamic selection columns
20
+ def merge_dynamic_selection
21
+ relation.dynamic_selection_values.concat(other.dynamic_selection_values)
22
+ end
23
+
18
24
  # Merge distinct on columns
19
25
  def merge_distinct_on
20
26
  return if other.distinct_on_values.blank?
@@ -1,5 +1,5 @@
1
1
  module Torque
2
2
  module PostgreSQL
3
- VERSION = '0.2.5'
3
+ VERSION = '0.2.6'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torque-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-31 00:00:00.000000000 Z
11
+ date: 2018-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -190,7 +190,6 @@ files:
190
190
  - lib/torque/postgresql/arel.rb
191
191
  - lib/torque/postgresql/arel/join_source.rb
192
192
  - lib/torque/postgresql/arel/select_manager.rb
193
- - lib/torque/postgresql/arel/using.rb
194
193
  - lib/torque/postgresql/arel/visitors.rb
195
194
  - lib/torque/postgresql/attributes.rb
196
195
  - lib/torque/postgresql/attributes/builder.rb
@@ -1,10 +0,0 @@
1
- module Torque
2
- module PostgreSQL
3
- module Arel
4
- class Using < ::Arel::Nodes::Unary
5
- end
6
-
7
- ::Arel::Nodes::Using = Using
8
- end
9
- end
10
- end