database_validations 0.8.4 → 0.8.5

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: ef7ec227437ca99440f72837a6dfe1d3fa21416ade5bcf60b2e3780037fae6af
4
- data.tar.gz: 6e8a592f0884396b80b33d001c9194d9d97c1e78c14b099d66c140dee266edd7
3
+ metadata.gz: 51bc2df5e836cbe85e6ec06cc3446652458093b3dba7d7dbd9123e579d67d6ce
4
+ data.tar.gz: 82b5fdfec69d72f1fecf811f6add8f4e5d606ed2adbd2a6fab1a90125b0e57e2
5
5
  SHA512:
6
- metadata.gz: 642ee70b3bdb810e06adeb4ebcf0e285ba5487a76ee5a8990cec6656e0f90ee3f27f8daf02402664addeaafabf592b0b8d68a339ab21dcda052b6d6b82fb2d81
7
- data.tar.gz: f34ade4123b89869f946da93c334e713e575eeb6b2b6908faefe38007f6c241854a512222e3f64caba2ae945c8001b5e22dd65d7ebe385fbfe738e2e2d85ff14
6
+ metadata.gz: 7a97f163683bc28f3884dbdd5b8d26a4059443199211bd4e5c980e5abdd1ef2b5d37efcc6c8557e0a853fe0fdb1f247f5677b6c0534b7f632bfb25af20e86606
7
+ data.tar.gz: 556d083c72c34d0b90e381e052999289eb797ba74cd1b938fe5a1a53cdd47511d41472671ea5c10902b6bc02049e1772a2d7b83f0ec0fd1ff43c8298235e4a50
@@ -7,6 +7,7 @@ require 'database_validations/rails/railtie' if defined?(Rails)
7
7
  require 'database_validations/validations/uniqueness_handlers'
8
8
  require 'database_validations/validations/uniqueness_options'
9
9
 
10
+ require 'database_validations/validations/belongs_to_presence_validator'
10
11
  require 'database_validations/validations/belongs_to_handlers'
11
12
  require 'database_validations/validations/belongs_to_options'
12
13
 
@@ -4,14 +4,6 @@ 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_blank_for?(self)
11
-
12
- errors.add(validator.relation, :blank, message: :required)
13
- end
14
- end
15
7
  end
16
8
 
17
9
  def valid?(context = nil)
@@ -56,6 +48,8 @@ module DatabaseValidations
56
48
 
57
49
  foreign_key = reflections[name.to_s].foreign_key
58
50
 
51
+ validates_with DatabaseValidations::Validations::BelongsToPresenceValidator, column: foreign_key, relation: name
52
+
59
53
  @database_validations_opts.push_belongs_to(foreign_key, name)
60
54
 
61
55
  include(DatabaseValidations::BelongsToHandlers)
@@ -0,0 +1,25 @@
1
+ module DatabaseValidations
2
+ module Validations
3
+ class BelongsToPresenceValidator < ActiveModel::Validator
4
+ attr_reader :attributes
5
+
6
+ # This is a hack to simulate presence validator
7
+ # It's used for cases when some 3rd parties are relies on the validators
8
+ # For example, required option from simple_form checks the validator
9
+ def self.kind
10
+ :presence
11
+ end
12
+
13
+ def initialize(options = {})
14
+ @attributes = [options[:relation]]
15
+ super
16
+ end
17
+
18
+ def validate(record)
19
+ return unless record.public_send(options[:column]).blank? && record.public_send(options[:relation]).blank?
20
+
21
+ record.errors.add(options[:relation], :blank, message: :required)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.8.4'.freeze
2
+ VERSION = '0.8.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
@@ -184,6 +184,7 @@ files:
184
184
  - lib/database_validations/validations/adapters/sqlite_adapter.rb
185
185
  - lib/database_validations/validations/belongs_to_handlers.rb
186
186
  - lib/database_validations/validations/belongs_to_options.rb
187
+ - lib/database_validations/validations/belongs_to_presence_validator.rb
187
188
  - lib/database_validations/validations/errors.rb
188
189
  - lib/database_validations/validations/helpers.rb
189
190
  - lib/database_validations/validations/options_storage.rb