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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a34de5e74d0221b789a2b6f032fbb9925ac5514d9543107346afdcaedb040c69
4
- data.tar.gz: 0cce23fa1f546f4f23cdd7e7b124e7eb081a66ab599cbf2a16fe14295fe9cb49
3
+ metadata.gz: b664ac9606a62da83707c8dbb8d568692c270492d850d3137d25ccb9690d6cd5
4
+ data.tar.gz: 97829f364eb1da0366dd85fb5f3b898a128b62f5af8c460fe53976a96467af68
5
5
  SHA512:
6
- metadata.gz: de6d95058b1386969164de088d9f12d5ec69798d87696bc1ef20e8c88289856e1a25a9266b9489dd6618d3c95296870ffc971feea44f0c0ed27f403f199dded3
7
- data.tar.gz: 5d47f9663a4249c66e7624753c14d7ae46c77f7da38cb068809a74ac7f6c0bb6b36be6b6b604e3367a56b2cd732cbdf2f7fec4ed4462d0e61351ceefdcc433b1
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 column.
12
- # @return [Array] The acceptable values for the enum type associated with the column.
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(name)
15
- # Determine the PostgreSQL type for the enum.
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
- # Query the PostgreSQL database for the enum's acceptable values.
19
- connection.enums[type.to_sym]
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
- # @param name [String] The name of an enum column.
25
- # @param options [Hash] The options.
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(name, options = {})
28
- values = pg_enum_values(name).map { |value| [value.to_sym, value.to_s] }
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(name => Hash[values])
38
+ enum(column_name => Hash[values])
31
39
 
32
- enum = type_for_attribute(name)
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
 
@@ -3,5 +3,5 @@
3
3
  module EnumKit
4
4
  # @return [String] The gem's semantic version number.
5
5
  #
6
- VERSION = '0.2.2'
6
+ VERSION = '0.2.3'
7
7
  end
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.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-10-31 00:00:00.000000000 Z
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord