enum_kit 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enum_kit/active_record_patches/enum.rb +21 -13
- data/lib/enum_kit/constants.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b664ac9606a62da83707c8dbb8d568692c270492d850d3137d25ccb9690d6cd5
|
4
|
+
data.tar.gz: 97829f364eb1da0366dd85fb5f3b898a128b62f5af8c460fe53976a96467af68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d28e48493d25bf2eb094dce0adf8c37c457959d70b30071e18dca1d9466749852925db512cc96584a8dbe535daaca07c59cbc1685651d2fa8590ca7af747629
|
7
|
+
data.tar.gz: c64d4bf2f48a506f7ce4056b7de079c3dd423e4a3e8f687b1fc89391e03eb441b477ef0511cc48b697d8ffeb1a4e488ae42e5ad75f557586ac7a4583d8fdb662
|
@@ -8,28 +8,36 @@ module ActiveRecord
|
|
8
8
|
module Enum
|
9
9
|
# Retrieve the acceptable values for the enum type associated with the given column.
|
10
10
|
#
|
11
|
-
# @param [String, Symbol] The name of an enum
|
12
|
-
# @return
|
11
|
+
# @param column_name [String, Symbol] The name of a column representing an enum.
|
12
|
+
# @return [Array] The acceptable values for the enum type associated with the column.
|
13
13
|
#
|
14
|
-
def pg_enum_values(
|
15
|
-
|
16
|
-
type = columns_hash[name.to_s].sql_type
|
14
|
+
def pg_enum_values(column_name)
|
15
|
+
type = columns_hash[column_name.to_s]&.sql_type
|
17
16
|
|
18
|
-
#
|
19
|
-
|
17
|
+
raise "Unable to determine '#{table_name}.#{column_name}' type. Did you forget to db:migrate?" if type.blank?
|
18
|
+
|
19
|
+
enums = connection.enums[type.to_sym]
|
20
|
+
|
21
|
+
raise "Unable to retrieve enums for type '#{type}'. Did you forget to db:migrate?" if enums.nil?
|
22
|
+
|
23
|
+
enums
|
20
24
|
end
|
21
25
|
|
22
26
|
# Define a PostgreSQL enum type.
|
23
27
|
#
|
24
|
-
#
|
25
|
-
#
|
28
|
+
# By default, setting an enum attribute to an unregistered value results in an exception being raised.
|
29
|
+
# You can disable this feature by setting the option `:exceptions` to `false` when registering the enum:
|
30
|
+
# => pg_enum :size, exceptions: false
|
31
|
+
#
|
32
|
+
# @param column_name [String, Symbol] The name of a column representing an enum.
|
33
|
+
# @param options [Hash] Any additional options.
|
26
34
|
#
|
27
|
-
def pg_enum(
|
28
|
-
values = pg_enum_values(
|
35
|
+
def pg_enum(column_name, options = {})
|
36
|
+
values = pg_enum_values(column_name).map { |v| [v.to_sym, v.to_s] }
|
29
37
|
|
30
|
-
enum(
|
38
|
+
enum(column_name => Hash[values])
|
31
39
|
|
32
|
-
enum = type_for_attribute(
|
40
|
+
enum = type_for_attribute(column_name)
|
33
41
|
|
34
42
|
raise 'Expected an ActiveRecord::Enum::EnumType' unless enum.is_a?(ActiveRecord::Enum::EnumType)
|
35
43
|
|
data/lib/enum_kit/constants.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nialto Services
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|