torque-postgresql 0.2.14 → 0.2.15

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: d0ed00788be18944446039d37828775e220ab9c8
4
- data.tar.gz: 1c7d9ab51f7715931944c9442a84d1798a73068a
3
+ metadata.gz: 15b1a0c063511d941ef67b0aa104ef7163aa9134
4
+ data.tar.gz: 93421afa177dde965f10e0458d258757956d0983
5
5
  SHA512:
6
- metadata.gz: 993b2f4cb92a254f801db65f8ec97730e260a427e62d4cde9ea986cc6a63b813e28c4179da1ec84e6516889a0bf4e7591774468e67afaea267495a257e779533
7
- data.tar.gz: a9f78472309cd8604d692f9b2128027b41e16a6d1768f4b02dbf3fd00bf80a471c0cb550566e3960da4b2be43a8cfb8ac4be725c47dddd2d25c66f9b95f2ba88
6
+ metadata.gz: 2224a3d965215d7ac5a7b7bb43d552728747ead30aa5095288e8b861ca6f370f86868d4ff1da33ef7f82626ad214b075f80efc45d99cbfe3845b9d97f96a6201
7
+ data.tar.gz: c0eb5be0217a014cc8dcfc6a8c02fd4b4c4ed47cb62c0f03b8cf4c35cada960851b5984cfb8860adcdd76c40a716750f697772b133177196a834ced0d1d41717
@@ -90,26 +90,32 @@ module Torque
90
90
 
91
91
  # Gets a list of user defined types.
92
92
  # You can even choose the +category+ filter
93
- def user_defined_types(category = nil)
94
- category_condition = "AND typtype = '#{category}'" unless category.nil?
93
+ def user_defined_types(*categories)
94
+ category_condition = categories.present? \
95
+ ? "AND t.typtype IN ('#{categories.join("', '")}')" \
96
+ : "AND t.typtype NOT IN ('b', 'd')"
97
+
95
98
  select_all(<<-SQL, 'SCHEMA').rows.to_h
96
- SELECT t.typname AS name,
97
- CASE t.typtype
98
- WHEN 'e' THEN 'enum'
99
- END AS type
100
- FROM pg_type t
101
- LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
102
- WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
103
- #{category_condition}
104
- AND NOT EXISTS(
105
- SELECT 1 FROM pg_catalog.pg_type el
106
- WHERE el.oid = t.typelem AND el.typarray = t.oid
107
- )
108
- AND (t.typrelid = 0 OR (
109
- SELECT c.relkind = 'c' FROM pg_catalog.pg_class c
110
- WHERE c.oid = t.typrelid
111
- ))
112
- ORDER BY t.typtype DESC
99
+ SELECT t.typname AS name,
100
+ CASE t.typtype
101
+ WHEN 'e' THEN 'enum'
102
+ END AS type
103
+ FROM pg_type t
104
+ LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
105
+ WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
106
+ #{category_condition}
107
+ AND NOT EXISTS(
108
+ SELECT 1
109
+ FROM pg_catalog.pg_type el
110
+ WHERE el.oid = t.typelem
111
+ AND el.typarray = t.oid
112
+ )
113
+ AND (t.typrelid = 0 OR (
114
+ SELECT c.relkind = 'c'
115
+ FROM pg_catalog.pg_class c
116
+ WHERE c.oid = t.typrelid
117
+ ))
118
+ ORDER BY t.typtype DESC
113
119
  SQL
114
120
  end
115
121
 
@@ -4,7 +4,7 @@ module Torque
4
4
  module Builder
5
5
  class Enum
6
6
 
7
- attr_accessor :klass, :attribute, :subtype, :options, :values
7
+ attr_accessor :klass, :attribute, :subtype, :options, :values, :enum_module
8
8
 
9
9
  # Start a new builder of methods for composite values on
10
10
  # ActiveRecord::Base
@@ -74,9 +74,14 @@ module Torque
74
74
 
75
75
  # Create all methods needed
76
76
  def build
77
+ @enum_module = Module.new
78
+
77
79
  plural
78
80
  text
79
81
  all_values
82
+
83
+ klass.include enum_module
84
+ klass.extend enum_module::ClassMethods
80
85
  end
81
86
 
82
87
  private
@@ -98,7 +103,8 @@ module Torque
98
103
  def plural
99
104
  attr = attribute
100
105
  enum_klass = subtype.klass
101
- klass.singleton_class.module_eval do
106
+ enum_module.const_set('ClassMethods', Module.new)
107
+ enum_module::ClassMethods.module_eval do
102
108
  # def self.statuses() statuses end
103
109
  define_method(attr.pluralize) do
104
110
  enum_klass.values
@@ -113,7 +119,7 @@ module Torque
113
119
 
114
120
  # def self.statuses_options() statuses_texts.zip(statuses) end
115
121
  define_method(attr.pluralize + '_options') do
116
- enum_klass.values
122
+ public_send(attr.pluralize + '_texts').zip(enum_klass.values)
117
123
  end
118
124
  end
119
125
  end
@@ -122,7 +128,7 @@ module Torque
122
128
  # the model scope
123
129
  def text
124
130
  attr = attribute
125
- klass.module_eval do
131
+ enum_module.module_eval do
126
132
  # def status_text() status.text('status', self) end
127
133
  define_method("#{attr}_text") { send(attr).text(attr, self) }
128
134
  end
@@ -133,10 +139,11 @@ module Torque
133
139
  def all_values
134
140
  attr = attribute
135
141
  vals = values_methods
136
- klass.module_eval do
142
+ model_klass = klass
143
+ enum_module.module_eval do
137
144
  vals.each do |val, list|
138
145
  # scope :disabled, -> { where(status: 'disabled') }
139
- scope list[0], -> { where(attr => val) }
146
+ model_klass.scope list[0], -> { where(attr => val) }
140
147
 
141
148
  # def disabled? status.disabled? end
142
149
  define_method(list[1]) { send(attr).public_send("#{val}?") }
@@ -39,6 +39,8 @@ module Torque
39
39
 
40
40
  # Check if the model's table depends on any inheritance
41
41
  def physically_inherited?
42
+ return false unless connected?
43
+
42
44
  @physically_inherited ||= connection.schema_cache.dependencies(
43
45
  defined?(@table_name) ? @table_name : decorated_table_name,
44
46
  ).present?
@@ -59,14 +59,11 @@ module Torque
59
59
 
60
60
  # Dump user defined types like enum
61
61
  def user_defined_types(stream)
62
- types = @connection.user_defined_types
62
+ types = @connection.user_defined_types('e')
63
63
  return unless types.any?
64
64
 
65
65
  stream.puts " # These are user-defined types used on this database"
66
- types.each do |name, type|
67
- raise StandardError, "User-defined type '#{name}' cannot be dumped!" if type.blank?
68
- send(type.to_sym, name, stream)
69
- end
66
+ types.each { |name, type| send(type.to_sym, name, stream) }
70
67
  stream.puts
71
68
  rescue => e
72
69
  stream.puts "# Could not dump user-defined types because of following #{e.class}"
@@ -1,5 +1,5 @@
1
1
  module Torque
2
2
  module PostgreSQL
3
- VERSION = '0.2.14'
3
+ VERSION = '0.2.15'
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.14
4
+ version: 0.2.15
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-12-08 00:00:00.000000000 Z
11
+ date: 2019-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails