Skip to content

Commit b8bb59b

Browse files
author
Carlos
authored
Merge pull request #30 from crashtech/general-fixes
General fixes
2 parents 64f69bf + 146a5ed commit b8bb59b

File tree

6 files changed

+61
-31
lines changed

6 files changed

+61
-31
lines changed

lib/torque/postgresql/adapter/database_statements.rb

+25-19
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,32 @@ def torque_load_additional_types(oids = nil)
9090

9191
# Gets a list of user defined types.
9292
# 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+
9598
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
113119
SQL
114120
end
115121

lib/torque/postgresql/attributes/builder/enum.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Attributes
44
module Builder
55
class Enum
66

7-
attr_accessor :klass, :attribute, :subtype, :options, :values
7+
attr_accessor :klass, :attribute, :subtype, :options, :values, :enum_module
88

99
# Start a new builder of methods for composite values on
1010
# ActiveRecord::Base
@@ -74,9 +74,14 @@ def conflicting?
7474

7575
# Create all methods needed
7676
def build
77+
@enum_module = Module.new
78+
7779
plural
7880
text
7981
all_values
82+
83+
klass.include enum_module
84+
klass.extend enum_module::ClassMethods
8085
end
8186

8287
private
@@ -98,7 +103,8 @@ def dangerous?(method_name, class_method = false)
98103
def plural
99104
attr = attribute
100105
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
102108
# def self.statuses() statuses end
103109
define_method(attr.pluralize) do
104110
enum_klass.values
@@ -113,7 +119,7 @@ def plural
113119

114120
# def self.statuses_options() statuses_texts.zip(statuses) end
115121
define_method(attr.pluralize + '_options') do
116-
enum_klass.values
122+
public_send(attr.pluralize + '_texts').zip(enum_klass.values)
117123
end
118124
end
119125
end
@@ -122,7 +128,7 @@ def plural
122128
# the model scope
123129
def text
124130
attr = attribute
125-
klass.module_eval do
131+
enum_module.module_eval do
126132
# def status_text() status.text('status', self) end
127133
define_method("#{attr}_text") { send(attr).text(attr, self) }
128134
end
@@ -133,10 +139,11 @@ def text
133139
def all_values
134140
attr = attribute
135141
vals = values_methods
136-
klass.module_eval do
142+
model_klass = klass
143+
enum_module.module_eval do
137144
vals.each do |val, list|
138145
# scope :disabled, -> { where(status: 'disabled') }
139-
scope list[0], -> { where(attr => val) }
146+
model_klass.scope list[0], -> { where(attr => val) }
140147

141148
# def disabled? status.disabled? end
142149
define_method(list[1]) { send(attr).public_send("#{val}?") }

lib/torque/postgresql/inheritance.rb

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def inheritance_merged_attributes
3939

4040
# Check if the model's table depends on any inheritance
4141
def physically_inherited?
42+
return false unless connected?
43+
4244
@physically_inherited ||= connection.schema_cache.dependencies(
4345
defined?(@table_name) ? @table_name : decorated_table_name,
4446
).present?

lib/torque/postgresql/schema_dumper.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ def tables(stream) # :nodoc:
5959

6060
# Dump user defined types like enum
6161
def user_defined_types(stream)
62-
types = @connection.user_defined_types
62+
types = @connection.user_defined_types('e')
6363
return unless types.any?
6464

6565
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) }
7067
stream.puts
7168
rescue => e
7269
stream.puts "# Could not dump user-defined types because of following #{e.class}"

lib/torque/postgresql/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Torque
22
module PostgreSQL
3-
VERSION = '0.2.14'
3+
VERSION = '0.2.15'
44
end
55
end

spec/tests/enum_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,24 @@
513513
expect { type_map.decorate!(AText, :conflict) }.to raise_error(ArgumentError, /already exists in/)
514514
end
515515

516+
context 'on inherited classes' do
517+
it 'has all enum methods' do
518+
klass = ActivityBook
519+
instance = klass.new
520+
521+
expect(klass).to respond_to(:kinds)
522+
expect(klass).to respond_to(:kinds_texts)
523+
expect(klass).to respond_to(:kinds_options)
524+
expect(instance).to respond_to(:kind_text)
525+
526+
klass.kinds.each do |value|
527+
expect(klass).to respond_to(value)
528+
expect(instance).to respond_to(value + '?')
529+
expect(instance).to respond_to(value + '!')
530+
end
531+
end
532+
end
533+
516534
context 'without autoload' do
517535
subject { Author }
518536
let(:instance) { FactoryGirl.build(:author) }

0 commit comments

Comments
 (0)