torque-postgresql 2.3.0 → 2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/torque/postgresql/adapter/database_statements.rb +13 -14
- data/lib/torque/postgresql/adapter/schema_statements.rb +5 -0
- data/lib/torque/postgresql/auxiliary_statement/recursive.rb +149 -0
- data/lib/torque/postgresql/auxiliary_statement/settings.rb +75 -22
- data/lib/torque/postgresql/auxiliary_statement.rb +39 -40
- data/lib/torque/postgresql/base.rb +10 -0
- data/lib/torque/postgresql/config.rb +5 -1
- data/lib/torque/postgresql/inheritance.rb +3 -1
- data/lib/torque/postgresql/railtie.rb +5 -1
- data/lib/torque/postgresql/relation/auxiliary_statement.rb +28 -15
- data/lib/torque/postgresql/version.rb +1 -1
- data/spec/models/category.rb +2 -0
- data/spec/schema.rb +7 -1
- data/spec/tests/auxiliary_statement_spec.rb +374 -35
- data/spec/tests/schema_spec.rb +9 -0
- metadata +5 -2
@@ -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
|
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
|
-
|
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
|
-
|
51
|
-
|
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)
|
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 =
|
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
|