active_form_objects 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dsl/polymorphs.rb +11 -0
- 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: 0cbf0e9447233fffdff5dd16af954f684ad903ccd35caad58363d53282a67e9f
|
4
|
+
data.tar.gz: e5cb98994f70e13654e514becec81a190f11f05f083287f38b38777c3fe36df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ace46d36714d2925f86fe6fd94c893d6ffd768ed07cec58ff5bce6853456325f8290aea4ed25911eb3944de2548e64c082c646c57865c7d56409d1579917f30
|
7
|
+
data.tar.gz: 90a5e250038de9067cec51c45c1f961ef109e0d1e717f4d6171b589f21abfecc30cf2b54233b2b3cf1fe3f9151ccd1308353e61cdd98d9dad140992f953329b6
|
data/lib/dsl/polymorphs.rb
CHANGED
@@ -7,6 +7,14 @@ module Dsl
|
|
7
7
|
|
8
8
|
@@polymorphic_keys = {}
|
9
9
|
|
10
|
+
class PolymorphValidator < ActiveModel::EachValidator
|
11
|
+
def validate_each(record, attribute, data)
|
12
|
+
return unless record.try(attribute).try(:id).nil? &&
|
13
|
+
!options[:keys].include?(data['type'].try(:to_sym))
|
14
|
+
record.errors.add(attribute, 'type must be included in the list')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
10
18
|
class_methods do
|
11
19
|
# Allows to declare a polymorphic relationship
|
12
20
|
#
|
@@ -23,6 +31,8 @@ module Dsl
|
|
23
31
|
# Scannable => model class
|
24
32
|
#
|
25
33
|
def polymorph(key, possible_types)
|
34
|
+
validates(key, polymorph: { keys: possible_types.keys })
|
35
|
+
|
26
36
|
@@polymorphic_keys[name] ||= []
|
27
37
|
@@polymorphic_keys[name] << {
|
28
38
|
key: key,
|
@@ -33,3 +43,4 @@ module Dsl
|
|
33
43
|
end
|
34
44
|
end
|
35
45
|
end
|
46
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_form_objects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michaël Villeneuve
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|