torque-postgresql 3.1.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,22 +10,14 @@ module Torque
10
10
  # :nodoc:
11
11
  def auxiliary_statements_values=(value); set_value(:auxiliary_statements, value); end
12
12
 
13
- # Set use of an auxiliary statement already configurated on the model
14
- def with(*args)
15
- spawn.with!(*args)
13
+ # Set use of an auxiliary statement
14
+ def with(*args, **settings)
15
+ spawn.with!(*args, **settings)
16
16
  end
17
17
 
18
18
  # Like #with, but modifies relation in place.
19
- def with!(*args)
20
- options = args.extract_options!
21
- args.each do |table|
22
- instance = table.is_a?(Class) && table < PostgreSQL::AuxiliaryStatement \
23
- ? table.new(options) \
24
- : PostgreSQL::AuxiliaryStatement.instantiate(table, self, options)
25
-
26
- self.auxiliary_statements_values += [instance]
27
- end
28
-
19
+ def with!(*args, **settings)
20
+ instantiate_auxiliary_statements(*args, **settings)
29
21
  self
30
22
  end
31
23
 
@@ -47,8 +39,23 @@ module Torque
47
39
  # Hook arel build to add the distinct on clause
48
40
  def build_arel(*)
49
41
  arel = super
50
- subqueries = build_auxiliary_statements(arel)
51
- subqueries.nil? ? arel : arel.with(subqueries)
42
+ type = auxiliary_statement_type
43
+ sub_queries = build_auxiliary_statements(arel)
44
+ sub_queries.nil? ? arel : arel.with(*type, *sub_queries)
45
+ end
46
+
47
+ # Instantiate one or more auxiliary statements for the given +klass+
48
+ def instantiate_auxiliary_statements(*args, **options)
49
+ klass = PostgreSQL::AuxiliaryStatement
50
+ klass = klass::Recursive if options.delete(:recursive).present?
51
+
52
+ self.auxiliary_statements_values += args.map do |table|
53
+ if table.is_a?(Class) && table < klass
54
+ table.new(**options)
55
+ else
56
+ klass.instantiate(table, self, **options)
57
+ end
58
+ end
52
59
  end
53
60
 
54
61
  # Build all necessary data for auxiliary statements
@@ -59,6 +66,12 @@ module Torque
59
66
  end
60
67
  end
61
68
 
69
+ # Return recursive if any auxiliary statement is recursive
70
+ def auxiliary_statement_type
71
+ klass = PostgreSQL::AuxiliaryStatement::Recursive
72
+ :recursive if auxiliary_statements_values.any?(klass)
73
+ end
74
+
62
75
  # Throw an error showing that an auxiliary statement of the given
63
76
  # table name isn't defined
64
77
  def auxiliary_statement_error(name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Torque
4
4
  module PostgreSQL
5
- VERSION = '3.1.0'
5
+ VERSION = '3.2.1'
6
6
  end
7
7
  end
@@ -0,0 +1,2 @@
1
+ class Category< ActiveRecord::Base
2
+ end
data/spec/schema.rb CHANGED
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- version = 1
13
+ version = 2
14
14
 
15
15
  return if ActiveRecord::Migrator.current_version == version
16
16
  ActiveRecord::Schema.define(version: version) do
@@ -69,6 +69,11 @@ ActiveRecord::Schema.define(version: version) do
69
69
  t.enum "specialty", enum_type: :specialties
70
70
  end
71
71
 
72
+ create_table "categories", force: :cascade do |t|
73
+ t.integer "parent_id"
74
+ t.string "title"
75
+ end
76
+
72
77
  create_table "texts", force: :cascade do |t|
73
78
  t.integer "user_id"
74
79
  t.string "content"
@@ -86,6 +91,7 @@ ActiveRecord::Schema.define(version: version) do
86
91
  end
87
92
 
88
93
  create_table "courses", force: :cascade do |t|
94
+ t.integer "category_id"
89
95
  t.string "title", null: false
90
96
  t.interval "duration"
91
97
  t.enum "types", enum_type: :types, array: true