database_validations 0.8.2 → 0.8.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: 9e9b338c0155871428c1d59ed10ef0a15f1c87a909b37f634eb63dfd9f442683
4
- data.tar.gz: 71b95e1f2f73ee07aec4948540abf620ca33d162d6de8e5d4c1617442f3e9530
3
+ metadata.gz: 56c892c79d9621f4bd44320198ddc1a982a45f5e8c03099cb8ce50bff1292983
4
+ data.tar.gz: '09c4a8c5cd90672af903a20c6608d22a019cf252fc2ebe7ad324abfe70b3fa32'
5
5
  SHA512:
6
- metadata.gz: c29b334bf891fbe940d6e2f2873db03110708614aec60075453702be5c5aca0ac6ccde30d24c18d7530a49bc6646fbb3f417f94802f5ca5b7ff02ef51d7c8235
7
- data.tar.gz: a635e8d67bf1135bd5325e34667407c87ef0dc360bff2e031fdc36bdd907ae41059dde31ebb6b46fd99a5346a840ae9191cc28507ed387df84ad7dc643cc16ba
6
+ metadata.gz: e5435113bf31727fc907ddb1d4ac38534f36e6a2f8d410c14b90e8b3fa589dc630b5620aedb4043606c653d575808f530e0d265360b9588965df4b0d43b74679
7
+ data.tar.gz: 5edde842f3003a572f8cf89ca69d025261b8ed277c61fa992abc6338c6de6ea1adb9d64f7946550a443c154c94de555921e8c77378f44308a6508fa1bfad4aab
@@ -4,12 +4,22 @@ module DatabaseValidations
4
4
 
5
5
  included do
6
6
  alias_method :validate, :valid?
7
+
8
+ validate do
9
+ Helpers.each_belongs_to_presence_validator(self.class) do |validator|
10
+ next unless validator.column_and_relation_nil_for?(self)
11
+
12
+ errors.add(validator.relation, :blank, message: :required)
13
+ end
14
+ end
7
15
  end
8
16
 
9
17
  def valid?(context = nil)
10
18
  output = super(context)
11
19
 
12
20
  Helpers.each_belongs_to_presence_validator(self.class) do |validator|
21
+ next if validator.column_and_relation_nil_for?(self)
22
+
13
23
  validates_with(ActiveRecord::Validations::PresenceValidator, validator.validates_presence_options)
14
24
  end
15
25
 
@@ -17,8 +27,6 @@ module DatabaseValidations
17
27
  end
18
28
 
19
29
  def save(opts = {})
20
- return false unless Helpers.check_foreign_key_missing(self)
21
-
22
30
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
23
31
  rescue ActiveRecord::InvalidForeignKey => e
24
32
  Helpers.handle_foreign_key_error!(self, e)
@@ -26,8 +34,6 @@ module DatabaseValidations
26
34
  end
27
35
 
28
36
  def save!(opts = {})
29
- raise ActiveRecord::RecordInvalid, self unless Helpers.check_foreign_key_missing(self)
30
-
31
37
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
32
38
  rescue ActiveRecord::InvalidForeignKey => e
33
39
  Helpers.handle_foreign_key_error!(self, e)
@@ -16,6 +16,11 @@ module DatabaseValidations
16
16
  Helpers.generate_key_for_belongs_to(column)
17
17
  end
18
18
 
19
+ # @return [Boolean]
20
+ def column_and_relation_nil_for?(instance)
21
+ instance.public_send(column).nil? && instance.public_send(relation).nil?
22
+ end
23
+
19
24
  def handle_foreign_key_error(instance)
20
25
  # Hack to not query the database because we know the result already
21
26
  instance.send("#{relation}=", nil)
@@ -26,15 +26,6 @@ module DatabaseValidations
26
26
  raise error
27
27
  end
28
28
 
29
- def check_foreign_key_missing(instance)
30
- Helpers.each_belongs_to_presence_validator(instance.class) do |validator|
31
- if instance.public_send(validator.column).nil? && instance.public_send(validator.relation).nil?
32
- instance.errors.add(validator.relation, :blank, message: :required)
33
- end
34
- end
35
- instance.errors.empty?
36
- end
37
-
38
29
  def each_options_storage(klass)
39
30
  while klass.respond_to?(:validates_db_uniqueness_of)
40
31
  storage = klass.instance_variable_get(:'@database_validations_opts')
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.8.2'.freeze
2
+ VERSION = '0.8.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-10 00:00:00.000000000 Z
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord