validation_scopes 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/validation_scopes.rb +40 -14
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8771aa3bbf22a50d74be7e5761a076c2f2b917e3
4
- data.tar.gz: f04d4bbf0d9231cd495635c1ed86684513f15491
3
+ metadata.gz: 0e189d48da303b09816e41dd0340f1bdb869d68a
4
+ data.tar.gz: cb046e7bf929b6211e062eed7e0be384e1ba67ad
5
5
  SHA512:
6
- metadata.gz: 59644834b4f7918e62a99ba3548ff591cc6553dfd8f2f1e643effa37b9db87122c628aaf01f167d071b6b175c0db48e72553a0bf587aa99adeb2a3c4f1663391
7
- data.tar.gz: efee7563fdb285f83a2d9aa5a0b4bfe4e944b299bc1131c89d407eeb33cd882c42599e5ec297f11e7de3928e7b1cbc7cee823b147236aafd8983dcb84d8229ce
6
+ metadata.gz: 37d48cd86d626b98b2e7a6b0214e815ef500b11bf1eb46e4c5f6239ba50cd315a4027539f42db3ca80b573fe66b1dd49d235ac7faaa7884cf22a8400742989bb
7
+ data.tar.gz: bef028153a6bdc041f2faa683a4afe1e5852d2ede8d62f64e363db31ef833d1f02e45c8a38d2366b67cf996c5e4a03d1d407c20046eff33e11345801769d7890
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.6.0 - 2015-07-14
2
+
3
+ * Add support for STI classes (Jeremy Mickelson)
4
+ * Add support for validates_associated (Jeremy Mickelson)
5
+
1
6
  # 0.5.2 - 2015-06-18
2
7
 
3
8
  * Fix Rails 4.2 deprecations and breakages (Tamás Michelberger)
@@ -6,23 +6,41 @@ module ValidationScopes
6
6
  base.extend ClassMethods
7
7
  end
8
8
 
9
+ # Based on AssociatedValidator from ActiveRecord, see:
10
+ # activerecord-4.2.0/lib/active_record/validations/associated.rb @ line 3
11
+ class AssociatedValidator < ActiveModel::EachValidator
12
+ def validate_each(record, attribute, value)
13
+ all_valid = Array.wrap(value).all? do |r|
14
+ r.marked_for_destruction? || r.send("no_#{options[:scope]}?")
15
+ end
16
+ unless all_valid
17
+ record.errors.add(attribute, 'is invalid')
18
+ end
19
+ end
20
+ end
21
+
9
22
  module ClassMethods
10
- def validation_scope(scope)
11
- @all_scopes ||= []
12
- @all_scopes << scope
23
+ def validation_scopes
24
+ @validation_scopes ||= []
25
+ end
13
26
 
14
- def self.all_scopes
15
- @all_scopes
16
- end
27
+ def validation_proxies
28
+ @validation_proxies ||= {}
29
+ end
30
+
31
+ def validation_scope(scope)
32
+ validation_scopes << scope
17
33
 
18
34
  base_class = self
19
35
 
20
- proxy_class = Class.new(DelegateClass(base_class)) do
36
+ superclass = self.superclass.validation_proxies[scope] || DelegateClass(base_class)
37
+ proxy_class = Class.new(superclass) do
21
38
  include ActiveModel::Validations
22
39
 
23
- def initialize(record)
24
- @base_record = record
25
- super(record)
40
+ @scope = scope
41
+ def self.validates_associated(*attr_names)
42
+ validates_with AssociatedValidator,
43
+ _merge_attributes(attr_names).reverse_merge(scope: @scope)
26
44
  end
27
45
 
28
46
  # Hacks to support dynamic_model helpers
@@ -30,14 +48,22 @@ module ValidationScopes
30
48
  self
31
49
  end
32
50
 
33
- # Rails 3 default implementation of model_name blows up for anonymous classes
34
51
  class << self; self; end.class_eval do
52
+ # Rails 3 default implementation of model_name blows up for anonymous
53
+ # classes
35
54
  define_method(:model_name) do
36
55
  base_class.model_name
37
56
  end
57
+ # Before Rails 4.1 callback functions were created using the class
58
+ # name, so we must name our anonymous classes.
59
+ define_method(:name) do
60
+ "#{base_class.name}#{scope.to_s.classify}ValidationProxy"
61
+ end
38
62
  end
39
63
  end
40
64
 
65
+ validation_proxies[scope] = proxy_class
66
+
41
67
  yield proxy_class
42
68
 
43
69
  define_method(scope) do
@@ -53,14 +79,14 @@ module ValidationScopes
53
79
  end
54
80
 
55
81
  define_method("init_validation_scope_for_#{scope}") do
56
- unless instance_variable_defined?("@#{scope}")
57
- instance_variable_set("@#{scope}", proxy_class.new(self))
82
+ unless instance_variable_defined?("@validation_#{scope}")
83
+ instance_variable_set("@validation_#{scope}", proxy_class.new(self))
58
84
  end
59
85
  end
60
86
 
61
87
  define_method("validation_scope_proxy_for_#{scope}") do
62
88
  send "init_validation_scope_for_#{scope}"
63
- instance_variable_get("@#{scope}")
89
+ instance_variable_get("@validation_#{scope}")
64
90
  end
65
91
  end
66
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabe da Silveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord