foreign_key_validation 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjBhMDg4MzJhYjgzMjlkMDJkZDBiODI5ZGM1ZjE5MWQ2NmI1YzIxMw==
4
+ ZmIyZjgxODI4YjY3ZjBiMWE5YzAxMWY1OTU2MjBjMmM2NDY4ZTVlNA==
5
5
  data.tar.gz: !binary |-
6
- ZTBiZTExNmE4NjM4MGRmM2QwYjI3MmExZDBlMmU3NjY4ZDlkYTIzMw==
6
+ NGE0M2NkZDBmZDM0ODU0NWVjNzA4NWQ3NWFiNWNiOTYxMDZkOGYxZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDM4OGQ0ZDRhYjNhOTFhNzIzMTM4YzUxOWI1ZWJhZGNjYjNhNDgzNDEyMWVl
10
- YzA1ODQwYzRjYjY5MGY2MTBlMTMxOGU5MDE2YjRlMjgyNmMyZWFkNGJlODJk
11
- NjFiYjAyZjIyYTg5MWUzMDhmMWU5MjNlNGIzNjMxZGE5MDIxOWQ=
9
+ OTRiYzRkNzFhMTI3MzllZTdiOWMwZjIyOWM5NDcyYjMzNjg5Y2U5YTZjMTZi
10
+ MDk0NzZhYjM0ZjE0NDM3ZTA5OWMyNTIwNGU0YjRmM2EwZjZlNzEzZGJiNmY1
11
+ OTgxMTU1YzQ0NzU2YjlmZDQxOGQ2OGQ3MmE2ZGEyYTM0YzkwNTg=
12
12
  data.tar.gz: !binary |-
13
- YTYzOTRlODQ1ZjgwYWFlNTIzYmRmNDRmNTVjMDJhMTViNzVmNTc3ZmNlYThl
14
- NWE0Yjc3NDA4OTgyZWUxN2M3ZTJlZDQ1MTYwMmFjZDY2MzM5ZDllMjM3ZTNk
15
- YzI1NWNmZTI4ZjI1NmNiMDMzOWU0Y2U1MjRjZWVlY2Q1MDMyYTA=
13
+ NmY1ZjI1MDVmMmVhM2U4Yjk3MTg0OTE5NTZjYzg5ZjhkNzg5NTIzYjIxNzFh
14
+ ZWI4M2JkY2E4YjM3MTVjMTI1Y2IzZjFjNDY3ODQxODlkMWNlZTEyYzIzYTcy
15
+ ZmYwYzhkNGQ4M2M2Y2YyODg3ZDNlZThjYzRjNDhiMTgxMzY2ODM=
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "rails", "~> 4.0.0"
21
+ spec.add_development_dependency "rails", "~> 4.0"
22
22
  spec.add_development_dependency "bundler", "~> 1.6"
23
23
  spec.add_development_dependency "rake", "~> 10.1"
24
24
  spec.add_development_dependency "rspec-rails", "~> 3.0"
@@ -8,7 +8,7 @@ module ForeignKeyValidation
8
8
  return if send(reflection_name).try(validate_against_key).nil? or try(validate_against_key).nil?
9
9
 
10
10
  if send(reflection_name).send(validate_against_key) != send(validate_against_key)
11
- errors.add(validate_against_key, "#{validate_against_key} of #{reflection_name} does not match #{self.class.name.tableize} #{validate_against_key}")
11
+ errors.add(validate_against_key, "#{validate_against_key} of #{reflection_name} does not match #{self.class.name.tableize} #{validate_against_key}.")
12
12
  end
13
13
  end
14
14
  end
@@ -18,12 +18,13 @@ module ForeignKeyValidation
18
18
  subclasses.map {|klass| klass.send(:validate_foreign_keys, opt) } if subclasses.any?
19
19
 
20
20
  validate_against = (opt[:on] || :user).to_s
21
- validate_against_key = "#{validate_against}_id"
22
- reflection_names = reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_s)
21
+ reflections = reflect_on_all_associations(:belongs_to)
22
+ reflection_names = reflections.map(&:name).map(&:to_s)
23
+ validate_against_key = reflections.select {|r| r.name.to_s == validate_against}.first.try(:foreign_key)
23
24
  validate_with = ((Array(opt[:with]).map(&:to_s) if opt[:with]) || reflection_names).reject {|n| n == validate_against}
24
25
 
25
- raise ArgumentError, "Can't find any belongs_to relations for #{name} class. Put validation call below association definitions" if reflection_names.empty?
26
- raise ArgumentError, "No foreign key #{validate_against_key} on #{table_name} table!" unless reflection_names.include?(validate_against)
26
+ raise ArgumentError, "Can't find any belongs_to relations for #{name} class. Put validation call below association definitions!" if reflection_names.empty?
27
+ raise ArgumentError, "No foreign key for relation #{validate_against} on #{table_name} table!" unless reflection_names.include?(validate_against)
27
28
  raise ArgumentError, "Unknown relation in #{validate_with}!" unless validate_with.all? {|k| reflection_names.include?(k) }
28
29
 
29
30
  define_method "validate_foreign_keys_on_#{validate_against}" do
@@ -1,3 +1,3 @@
1
1
  module ForeignKeyValidation
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -84,21 +84,21 @@ describe ForeignKeyValidation::ModelExtension do
84
84
  it "does not allow to rewrite user id of idea" do
85
85
  idea.user_id = 42
86
86
  idea.save
87
- expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
87
+ expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id.")
88
88
  expect(idea.reload.user_id).to_not eq(42)
89
89
  end
90
90
 
91
91
  it "does not allow to rewrite user id of issue" do
92
92
  issue.user_id = 42
93
93
  issue.save
94
- expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id")
94
+ expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id.")
95
95
  expect(issue.reload.user_id).to_not eq(42)
96
96
  end
97
97
 
98
98
  it "does not allow to rewrite user id of comment" do
99
99
  comment.user_id = 42
100
100
  comment.save
101
- expect(comment.errors.messages.values.flatten).to include("user_id of issue does not match comments user_id")
101
+ expect(comment.errors.messages.values.flatten).to include("user_id of issue does not match comments user_id.")
102
102
  expect(comment.reload.user_id).to_not eq(42)
103
103
  end
104
104
 
@@ -112,7 +112,7 @@ describe ForeignKeyValidation::ModelExtension do
112
112
  it "does not allow to rewrite user id of developer" do
113
113
  developer.user_id = 42
114
114
  developer.save
115
- expect(developer.errors.messages.values.flatten).to include("user_id of boss does not match developers user_id")
115
+ expect(developer.errors.messages.values.flatten).to include("user_id of boss does not match developers user_id.")
116
116
  expect(developer.reload.user_id).to_not eq(42)
117
117
  end
118
118
 
@@ -147,7 +147,7 @@ describe ForeignKeyValidation::ModelExtension do
147
147
  it "does not allow to rewrite user id of idea" do
148
148
  idea.user_id = 42
149
149
  idea.save
150
- expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
150
+ expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id.")
151
151
  expect(idea.reload.user_id).to_not eq(42)
152
152
  end
153
153
 
@@ -177,7 +177,7 @@ describe ForeignKeyValidation::ModelExtension do
177
177
  context "with calling validation and wrong attributes hash" do
178
178
 
179
179
  it "raises error due to wrong :on key" do
180
- expect{Idea.class_eval { validate_foreign_keys on: :not_existing }}.to raise_error("No foreign key not_existing_id on ideas table!")
180
+ expect{Idea.class_eval { validate_foreign_keys on: :not_existing }}.to raise_error("No foreign key for relation not_existing on ideas table!")
181
181
  end
182
182
 
183
183
  it "raises error due to wrong :with key" do
@@ -189,7 +189,7 @@ describe ForeignKeyValidation::ModelExtension do
189
189
  context "with calling validation and missing relations" do
190
190
 
191
191
  it "raises error due to wrong :on key" do
192
- expect{Dummy.class_eval { validate_foreign_keys }}.to raise_error("Can't find any belongs_to relations for Dummy class. Put validation call below association definitions")
192
+ expect{Dummy.class_eval { validate_foreign_keys }}.to raise_error("Can't find any belongs_to relations for Dummy class. Put validation call below association definitions!")
193
193
  end
194
194
 
195
195
  end
@@ -210,7 +210,7 @@ describe ForeignKeyValidation::ModelExtension do
210
210
  it "does not allow to rewrite user id of issue" do
211
211
  issue.user_id = 42
212
212
  issue.save
213
- expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id")
213
+ expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id.")
214
214
  expect(issue.reload.user_id).to_not eq(42)
215
215
  end
216
216
 
@@ -46,5 +46,5 @@ class Manager < Member
46
46
  end
47
47
 
48
48
  class Developer < Member
49
- belongs_to :boss, class_name: "Manager", foreign_key: :boss_id
49
+ belongs_to :boss, class_name: "Manager", foreign_key: :custom_boss_id
50
50
  end
@@ -29,7 +29,7 @@ ActiveRecord::Schema.define do
29
29
 
30
30
  create_table "members", force: true do |t|
31
31
  t.integer "user_id"
32
- t.integer "boss_id"
32
+ t.integer "custom_boss_id"
33
33
  t.string "type"
34
34
  end
35
35
 
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Geißler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: '4.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement