activerecord-postgres_enum 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c68566d10f50ac8201be6fef301800d43510eaaec25464fa8eb84166c9c999c8
|
4
|
+
data.tar.gz: df6c0c72b1cb347fa0273bc545bc6512b354d7db768b570a64cd77f92b3aee20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8aa9af3d60353212b85d1f2373464dce9a370e0d16629e9310c240085bd28e3e8a2eab38922365784ce5be45ead41d577c4cbbe6c80523406346f8dd8b7dbe2c
|
7
|
+
data.tar.gz: 1565021cfb38112174caff888368d96b97f31717cc59d1da370cb10cecab11c4fd513467f1ae30e84bbbc014c21183d7e18efdd61586dad78cdecb449a4c7a72
|
@@ -9,6 +9,7 @@ require "active_support/lazy_load_hooks"
|
|
9
9
|
require "active_record/postgres_enum/version"
|
10
10
|
require "active_record/postgres_enum/postgresql_adapter"
|
11
11
|
require "active_record/postgres_enum/schema_dumper"
|
12
|
+
require "active_record/postgres_enum/enum_validator"
|
12
13
|
|
13
14
|
ActiveSupport.on_load(:active_record) do
|
14
15
|
require "active_record/postgres_enum/extensions"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module PostgresEnum
|
5
|
+
class EnumValidator < ActiveModel::EachValidator
|
6
|
+
def self.enum_values(attr_id, type, connection)
|
7
|
+
@enums ||= {}
|
8
|
+
return @enums[attr_id] if @enums.key?(attr_id)
|
9
|
+
|
10
|
+
@enums[attr_id] ||= connection.enums.fetch(type.to_sym) do
|
11
|
+
raise "Enum `#{type}` not found in a database #{connection}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_each(object, attribute, value)
|
16
|
+
enum_type = enum_type(object, attribute)
|
17
|
+
attr_id = "#{object.class.name}##{attribute}"
|
18
|
+
|
19
|
+
return if self.class.enum_values(attr_id, enum_type, object.class.connection).include?(value)
|
20
|
+
|
21
|
+
object.errors.add(
|
22
|
+
attribute,
|
23
|
+
options[:message] || :invalid_enum_value,
|
24
|
+
value: value,
|
25
|
+
type: enum_type
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def enum_type(object, attribute)
|
32
|
+
object.class.columns_hash[attribute.to_s].sql_type
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ActiveModel
|
39
|
+
module Validations
|
40
|
+
module HelperMethods
|
41
|
+
def validates_enum(*attr_names)
|
42
|
+
validates_with ActiveRecord::PostgresEnum::EnumValidator, _merge_attributes(attr_names)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-postgres_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Merkushin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- LICENSE.txt
|
167
167
|
- README.md
|
168
168
|
- lib/active_record/postgres_enum.rb
|
169
|
+
- lib/active_record/postgres_enum/enum_validator.rb
|
169
170
|
- lib/active_record/postgres_enum/extensions.rb
|
170
171
|
- lib/active_record/postgres_enum/postgresql_adapter.rb
|
171
172
|
- lib/active_record/postgres_enum/schema_dumper.rb
|