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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dsl/polymorphs.rb +11 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 264e500907e70a06681bb42c4553d532b52f34b311145b993065725672ccdc54
4
- data.tar.gz: ea8d279f54b93ca9f573f7350f63432f11dc26b3e4ad6eb4de7d87ce0b78cfd0
3
+ metadata.gz: 0cbf0e9447233fffdff5dd16af954f684ad903ccd35caad58363d53282a67e9f
4
+ data.tar.gz: e5cb98994f70e13654e514becec81a190f11f05f083287f38b38777c3fe36df1
5
5
  SHA512:
6
- metadata.gz: 92288f3451f81cadb980157da7783060aef7d071ea3d83a45646e1a02e8586ad3d58b03cc8bd49514de16e68fb7a20ca96c7b4756b2687a9ca4b7e24fad5cdf3
7
- data.tar.gz: f98fa84485a428a4c1bf53ff45fd138dbaa77b8114872e77cfff480e28db4064bfbaeedd2e9d4c08086b39073733020d7fe32e3c052943503799fe8de56b0008
6
+ metadata.gz: 0ace46d36714d2925f86fe6fd94c893d6ffd768ed07cec58ff5bce6853456325f8290aea4ed25911eb3944de2548e64c082c646c57865c7d56409d1579917f30
7
+ data.tar.gz: 90a5e250038de9067cec51c45c1f961ef109e0d1e717f4d6171b589f21abfecc30cf2b54233b2b3cf1fe3f9151ccd1308353e61cdd98d9dad140992f953329b6
@@ -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.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: 2018-12-17 00:00:00.000000000 Z
11
+ date: 2019-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport