activerecord-postgres_enum 0.2.2 → 0.3.0

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: cdf8acb0b3c01c8e883965fcde17f1c1fb280d3c229f98eb9c49e7dd74fa71fa
4
- data.tar.gz: 133f33acd62d7546a183809f512fbfbedd3859535798d469a12101e20d26934b
3
+ metadata.gz: c68566d10f50ac8201be6fef301800d43510eaaec25464fa8eb84166c9c999c8
4
+ data.tar.gz: df6c0c72b1cb347fa0273bc545bc6512b354d7db768b570a64cd77f92b3aee20
5
5
  SHA512:
6
- metadata.gz: 859272852f26c70f5d0824fc350e10f65cdf9ff503fa64a0985e578a00e9fe0a1366f3bb95d79c869d2872894ecff380cd2c077f5e0236c9af485ad9b3f01a04
7
- data.tar.gz: 0e92dd024d589b0c4e52cc784840dfbee7574d2d2cccf2710afa21defd65b16b306807dabb94cd17a0218521c644b2a5b6fe2c323e6e5fcfda1d3b0db81255b6
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "0.2.2"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  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.2.2
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-26 00:00:00.000000000 Z
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