foreign_key_validation 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmUxYTU1NWRhZjEwYWQ1NDI2Mzk0MDFkMmZiZWViOTZmODc1NzA2MA==
4
+ Zjg4OGRlMzQzMTJhZmQyOWFhOGQzNTI2NDhhYTE0YThlZWM0Y2NjMg==
5
5
  data.tar.gz: !binary |-
6
- MjQzNDJhZTc4MzcwZjBhNmJmZmZkODQ5NDU4YmE5OWY1MmU5ZGU0Yg==
6
+ NTRlZDQxNTUyMTdiZGRmYjQwNTlkMzkwOTUwMTMyMWNjZmUyYzBjZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWQ5NWQ5NzkyY2QxZjg3OTE0ZDFmYWZlNzlmYTc1MDJjZDc2ZjZmNTg3NmE1
10
- NWE0Y2EzYWJiYWMyNjBiNjE1NWQ5MTkwOGFmYTQ1YWM5YjJmNjMyZTg3ZDFj
11
- ZjM5OTQ2MTU5NGJiYzIxZDA4OWNhNmM3MWQ5NDkzODNjYzRmYWU=
9
+ NDAyZjI0YzcxYzQzOGVmMThkYzk5MmUyNDcyZmY4YjA3OWU2NTc1OTA2ODc2
10
+ ODVjOWEwZDhhYWVjYjk3MzAyNzZlNzI5NTBmZDNjYjQ2Zjk4MTBlZTRhZjgx
11
+ M2U2NzBjY2QzYzEyNmEyNzFhYTE1ZDA2ODAwNjYyMTQ0ZDFhNzY=
12
12
  data.tar.gz: !binary |-
13
- YmQ2ODhmNTEyMjAwYTY4Zjg2MWJiOTMwNDYyNDQ0M2Y4NGZlODE1MDRiODZh
14
- NmI2MTM3MmUxYWRlMzg4Y2JkYzQ3ZmI5ODQxNDc4ZWM1N2EyNzllMzBkYjUz
15
- MDhiOTQwNmZkNDI4OWJlZmUxMDIzMTc0MGRlZWNiZDMxZjE3ZGU=
13
+ NWM2OWNhNDVkMDdmNDgxNTRmNzEzMTU1NzNjYTI3M2Y2ZjA3ZWYzYWViYjll
14
+ NjdmOGE2MjI2NTE3MjE3ZWZmZWZjYTI0ZDk5YTg2N2FlMmQzNDM4YWE0ZmU2
15
+ NGNlMGM1YTFlNzZjMDEyNjY2OWU1ZjNiY2I5ZTk4Y2NlM2YwNTM=
data/README.md CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
- Call `validate_foreign_keys` in your model. By default it assumes that it should check all foreign keys against the `user_id` column. So any relation (except `user`) will be checked for a matching `user_id` if the column exists.
26
+ Call `validate_foreign_keys` below the association definitions (`belongs_to`, ...) in your model. By default it assumes that it should check all foreign keys against the `user_id` column. So any relation (except `user`) will be checked for a matching `user_id` if the column exists.
27
27
 
28
28
  Change behaviour by calling `validate_foreign_keys` with arguments hash.
29
29
 
@@ -5,6 +5,8 @@ module ForeignKeyValidation
5
5
  included do
6
6
  private
7
7
  def validate_foreign_key(validate_against, relation)
8
+ return if send(relation).try("#{validate_against}_id").nil? or try("#{validate_against}_id").nil?
9
+
8
10
  if send(relation).send("#{validate_against}_id") != send("#{validate_against}_id")
9
11
  errors.add(validate_against, "#{validate_against} of #{relation} does not match #{self.class.table_name} #{validate_against}")
10
12
  end
@@ -14,10 +16,10 @@ module ForeignKeyValidation
14
16
  module ClassMethods
15
17
  def validate_foreign_keys(opt={})
16
18
  validate_against = (opt[:on] || :user).to_s
17
- reflections = self.reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_s)
19
+ reflections = reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_s)
18
20
  validate_with = ((Array(opt[:with]).map(&:to_s) if opt[:with]) || reflections).reject {|n| n == validate_against}
19
21
 
20
- raise ArgumentError, "No foreign key #{validate_against} on #{self.table_name} table!" unless reflections.include?(validate_against)
22
+ raise ArgumentError, "No foreign key #{validate_against} on #{table_name} table!" unless reflections.include?(validate_against)
21
23
  raise ArgumentError, "Unknown relation in #{validate_with}!" unless validate_with.all? {|k| reflections.include?(k) }
22
24
 
23
25
  define_method "validate_foreign_keys_on_#{validate_against}" do
@@ -1,3 +1,3 @@
1
1
  module ForeignKeyValidation
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe ForeignKeyValidation::ModelExtension do
4
4
 
5
+ # NOTE: it's important to not create the objects through relation (user.projects.create...)
6
+ # it looks like active_record is caching the classes - but we need to test different class configs
7
+
5
8
  context "Without calling validation" do
6
9
 
7
- # NOTE: it's important here to not create the object through relation (user.projects.create...)
8
- # it looks like active_record is caching the classes - but we need to test different class configs
9
10
  let(:user) { User.create }
10
11
  let(:project) { Project.create user: user }
11
12
  let(:idea) { Idea.create user: user, project: project }
@@ -55,8 +56,6 @@ describe ForeignKeyValidation::ModelExtension do
55
56
  Comment.send :validate_foreign_keys
56
57
  end
57
58
 
58
- # NOTE: it's important here to not create the object through relation (user.projects.create...)
59
- # it looks like active_record is caching the classes - but we need to test different class configs
60
59
  let(:user) { User.create }
61
60
  let(:project) { Project.create user: user }
62
61
  let(:idea) { Idea.create user: user, project: project }
@@ -113,8 +112,6 @@ describe ForeignKeyValidation::ModelExtension do
113
112
  end
114
113
  end
115
114
 
116
- # NOTE: it's important here to not create the object through relation (user.projects.create...)
117
- # it looks like active_record is caching the classes - but we need to test different class configs
118
115
  let(:user) { User.create }
119
116
  let(:project) { Project.create user: user }
120
117
  let(:idea) { Idea.create user: user, project: project }
@@ -158,8 +155,6 @@ describe ForeignKeyValidation::ModelExtension do
158
155
 
159
156
  context "With calling validation with wrong attributes hash" do
160
157
 
161
- # NOTE: it's important here to not create the object through relation (user.projects.create...)
162
- # it looks like active_record is caching the classes - but we need to test different class configs
163
158
  let(:user) { User.create }
164
159
  let(:project) { Project.create user: user }
165
160
  let(:issue) { Issue.create user: user, project: project }
@@ -175,5 +170,46 @@ describe ForeignKeyValidation::ModelExtension do
175
170
 
176
171
  end
177
172
 
173
+ context "With calling validation and missing foreign key on self" do
174
+
175
+ before do
176
+ Issue.class_eval do
177
+ validate_foreign_keys
178
+ end
179
+ end
180
+
181
+ let(:user) { User.create }
182
+ let(:project) { Project.create user: user }
183
+ let(:issue) { Issue.create project: project }
184
+
185
+ it "does not allow to rewrite user id of issue" do
186
+ issue.user_id = 42
187
+ issue.save
188
+ expect(issue.errors.messages.values.flatten).to include("user of project does not match issues user")
189
+ expect(issue.reload.user_id).to_not eq(42)
190
+ end
191
+
192
+ end
193
+
194
+ context "With calling validation and missing foreign key on relation" do
195
+
196
+ before do
197
+ Issue.class_eval do
198
+ validate_foreign_keys
199
+ end
200
+ end
201
+
202
+ let(:user) { User.create }
203
+ let(:project) { Project.create }
204
+ let(:issue) { Issue.create project: project, user: user }
205
+
206
+ it "allow to rewrite user id of issue" do
207
+ issue.user_id = 42
208
+ issue.save
209
+ issue.reload
210
+ expect(issue.user_id).to eq(42)
211
+ end
212
+
213
+ end
178
214
 
179
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreign_key_validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Geißler