foreign_key_validation 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Appraisals +8 -4
- data/README.md +4 -8
- data/foreign_key_validation.gemspec +3 -2
- data/gemfiles/3.2.gemfile +2 -1
- data/gemfiles/4.0.gemfile +2 -1
- data/gemfiles/4.1.gemfile +2 -1
- data/gemfiles/4.2.gemfile +2 -1
- data/lib/foreign_key_validation.rb +7 -3
- data/lib/foreign_key_validation/filter.rb +1 -1
- data/lib/foreign_key_validation/model_extension.rb +1 -1
- data/lib/foreign_key_validation/validator.rb +8 -7
- data/lib/foreign_key_validation/version.rb +1 -1
- data/spec/collector/base_spec.rb +4 -2
- data/spec/config_spec.rb +34 -3
- data/spec/spec_helper.rb +4 -5
- data/spec/support/load_models.rb +0 -2
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YmM1OWE1NWI4NzQzZjM0MmFkNDg1NTIwOGYwMmRjM2YzYTAzZmQ4MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YWI5MTJmYWRmOWNiMjllNzY1NjA4NjFiZGU0ZTM3MmYzNDIwMGVjNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MTVlOTJkNzlmZjE5OGUyZjY3ZTQ4ZWUxNTUxNjlhOWE4MjBmYWNhMjRlOWE0
|
10
|
+
NjI3NTYyZDMzNDcyOTdiZDk0NDc2YzBmZDBkMDdmNjQwOTBmZmYwMGI0NTJi
|
11
|
+
OTRhMzY0ZDRlMWQ2MTY4OWRjZjg2OTkwMjUzYjYxZDA0ZDZhNjE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTZkYWYwZDU2OTg1ZjZiZjUyMTY3NWJiMGFlZWQyZWY1ODQwZGVmNTU2YmM0
|
14
|
+
ZGUwMzdhMjc0MDEyZDUwNDc5NWI1ZTVlNjBjOTJhMTcxOTZkMGQxMzg1N2Q4
|
15
|
+
YjNiMTE1YWM1YzIzZTk5ODBjNTA2ZDA3YWM3OThkMzYwZGRlNTk=
|
data/Appraisals
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
appraise "3.2" do
|
2
|
-
gem "
|
2
|
+
gem "activerecord", "~> 3.2.0"
|
3
|
+
gem "activesupport", "~> 3.2.0"
|
3
4
|
gemspec
|
4
5
|
end
|
5
6
|
|
6
7
|
appraise "4.0" do
|
7
|
-
gem "
|
8
|
+
gem "activerecord", "~> 4.0.0"
|
9
|
+
gem "activesupport", "~> 4.0.0"
|
8
10
|
gemspec
|
9
11
|
end
|
10
12
|
|
11
13
|
appraise "4.1" do
|
12
|
-
gem "
|
14
|
+
gem "activerecord", "~> 4.1.0"
|
15
|
+
gem "activesupport", "~> 4.1.0"
|
13
16
|
gemspec
|
14
17
|
end
|
15
18
|
|
16
19
|
appraise "4.2" do
|
17
|
-
gem "
|
20
|
+
gem "activerecord", "~> 4.2.0.beta4"
|
21
|
+
gem "activesupport", "~> 4.2.0.beta4"
|
18
22
|
gemspec
|
19
23
|
end
|
data/README.md
CHANGED
@@ -8,8 +8,9 @@
|
|
8
8
|
Protect your models by specifying a collection of relations that should be tested for consistency with a predefined column (e.g. `user_id`).This is useful when the column `user_id` is used in multiple models. We can check if the `user_id` of *model A* matches `user_id` of *model B* before saving the records - if the IDs are different, an error will be attached to the errors hash of checked model.
|
9
9
|
|
10
10
|
## Requirements
|
11
|
+
|
11
12
|
ruby >= 1.9.3
|
12
|
-
|
13
|
+
active_record & active_support >= 3.2.0
|
13
14
|
|
14
15
|
## Installation
|
15
16
|
|
@@ -40,24 +41,19 @@ This would only check `model.project.admin_user_id` to match `model.admin_user_i
|
|
40
41
|
You can customize the default behaviour of the gem by calling the `configure` method on the module with a block (e.g. initializer).
|
41
42
|
|
42
43
|
ForeignKeyValidation.configure do |config|
|
43
|
-
config.error_message =
|
44
|
+
config.error_message = lambda { |key, name, object| "My custom msg!" }
|
44
45
|
config.inject_subclasses = false # default: true
|
45
46
|
config.validate_against = :admin # default: :user
|
46
47
|
end
|
47
48
|
|
48
|
-
## Note
|
49
|
-
|
50
|
-
Only tested with ActiveRecord
|
51
|
-
|
52
49
|
## Tests
|
53
50
|
|
54
|
-
Use these commands to run the testsuite against different versions of
|
51
|
+
Use these commands to run the testsuite against different versions of ActiveRecord
|
55
52
|
|
56
53
|
bundle
|
57
54
|
appraisal install
|
58
55
|
appraisal rspec
|
59
56
|
|
60
|
-
|
61
57
|
## Contributing
|
62
58
|
|
63
59
|
1. Fork it ( https://github.com/marcusg/foreign_key_validation/fork )
|
@@ -18,13 +18,14 @@ 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_runtime_dependency "
|
21
|
+
spec.add_runtime_dependency "activerecord", ">= 3.2"
|
22
|
+
spec.add_runtime_dependency "activesupport", ">= 3.2"
|
22
23
|
|
23
24
|
spec.add_development_dependency 'appraisal', '~> 1.0'
|
24
25
|
spec.add_development_dependency 'coveralls', '~> 0.7'
|
25
26
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
27
|
spec.add_development_dependency 'rake', '~> 10.1'
|
27
|
-
spec.add_development_dependency 'rspec
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
29
|
spec.add_development_dependency 'database_cleaner', '~> 1.3'
|
29
30
|
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
30
31
|
spec.add_development_dependency 'pry', '~> 0.10'
|
data/gemfiles/3.2.gemfile
CHANGED
data/gemfiles/4.0.gemfile
CHANGED
data/gemfiles/4.1.gemfile
CHANGED
data/gemfiles/4.2.gemfile
CHANGED
@@ -11,21 +11,25 @@ module ForeignKeyValidation
|
|
11
11
|
DEFAULT_CONFIG = {
|
12
12
|
inject_subclasses: true,
|
13
13
|
validate_against: :user,
|
14
|
-
error_message:
|
14
|
+
error_message: lambda { |key, reflection_name, object|
|
15
15
|
"#{key} of #{reflection_name} does not match #{object.class.name.tableize} #{key}."
|
16
16
|
}
|
17
17
|
}
|
18
18
|
|
19
19
|
class << self
|
20
|
-
attr_writer :configuration
|
21
20
|
|
22
|
-
def configure
|
21
|
+
def configure
|
23
22
|
yield configuration
|
24
23
|
end
|
25
24
|
|
26
25
|
def configuration
|
27
26
|
@configuration ||= OpenStruct.new(DEFAULT_CONFIG)
|
28
27
|
end
|
28
|
+
|
29
|
+
def reset_configuration
|
30
|
+
@configuration = nil
|
31
|
+
end
|
32
|
+
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -5,7 +5,7 @@ module ForeignKeyValidation
|
|
5
5
|
module ClassMethods
|
6
6
|
|
7
7
|
def validate_foreign_keys(opt={})
|
8
|
-
|
8
|
+
descendants.map {|klass| klass.public_send(:validate_foreign_keys, opt)} if ForeignKeyValidation.configuration.inject_subclasses
|
9
9
|
|
10
10
|
collector = Collector.new(opt.merge(klass: self))
|
11
11
|
collector.check!
|
@@ -11,21 +11,22 @@ module ForeignKeyValidation
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def validate
|
14
|
-
|
14
|
+
to_enum(:invalid_reflection_names).map {|n| attach_error(n) }.any?
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def invalid_reflection_names(&block)
|
15
20
|
reflection_names.each do |reflection_name|
|
16
21
|
next unless keys_present?(reflection_name)
|
17
22
|
if keys_different?(reflection_name)
|
18
|
-
|
19
|
-
has_errors = true
|
23
|
+
yield reflection_name
|
20
24
|
end
|
21
25
|
end
|
22
|
-
has_errors
|
23
26
|
end
|
24
27
|
|
25
|
-
private
|
26
|
-
|
27
28
|
def key_on_relation(relation)
|
28
|
-
object.
|
29
|
+
object.public_send(relation).try(validate_against_key)
|
29
30
|
end
|
30
31
|
|
31
32
|
def key_on_object
|
data/spec/collector/base_spec.rb
CHANGED
@@ -17,12 +17,14 @@ describe ForeignKeyValidation::Collector do
|
|
17
17
|
|
18
18
|
describe "#check!" do
|
19
19
|
|
20
|
+
subject { ForeignKeyValidation::Collector }
|
21
|
+
|
20
22
|
it "returns true for known class" do
|
21
|
-
expect(
|
23
|
+
expect(subject.new(klass: Issue).check!).to be true
|
22
24
|
end
|
23
25
|
|
24
26
|
it "raises error for class without relations" do
|
25
|
-
expect{
|
27
|
+
expect{subject.new(klass: Dummy).check!}.to raise_error(ForeignKeyValidation::Errors::NoReleationFoundError)
|
26
28
|
end
|
27
29
|
|
28
30
|
end
|
data/spec/config_spec.rb
CHANGED
@@ -4,8 +4,6 @@ describe ForeignKeyValidation do
|
|
4
4
|
|
5
5
|
describe ".configuration" do
|
6
6
|
|
7
|
-
subject { ForeignKeyValidation }
|
8
|
-
|
9
7
|
it "initializes configuration as open struct" do
|
10
8
|
expect(subject.configuration).to be_instance_of OpenStruct
|
11
9
|
end
|
@@ -16,13 +14,46 @@ describe ForeignKeyValidation do
|
|
16
14
|
|
17
15
|
it "defaults to proc for error messages" do
|
18
16
|
expect(subject.configuration.error_message).to be_a Proc
|
19
|
-
expect(subject.configuration.error_message.call).to match /does not match/
|
17
|
+
expect(subject.configuration.error_message.call(nil,nil,nil)).to match /does not match/
|
20
18
|
end
|
21
19
|
|
22
20
|
it "defaults to :user for validate against key" do
|
23
21
|
expect(subject.configuration.validate_against).to eq :user
|
24
22
|
end
|
25
23
|
|
24
|
+
it "defaults to nil for unknown config options" do
|
25
|
+
expect(subject.configuration.foo_bar).to eq nil
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ".configure" do
|
31
|
+
|
32
|
+
it "allow change of inject_subclasses config" do
|
33
|
+
subject.configure {|c| c.inject_subclasses = false}
|
34
|
+
expect(subject.configuration.inject_subclasses).to eq false
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allow change of error_message config" do
|
38
|
+
subject.configure {|c| c.error_message = proc { "MY MSG" }}
|
39
|
+
expect(subject.configuration.error_message.call).to eq "MY MSG"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "allow change of validate_against config" do
|
43
|
+
subject.configure {|c| c.validate_against = :my_user}
|
44
|
+
expect(subject.configuration.validate_against).to eq :my_user
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".reset_configuration" do
|
50
|
+
|
51
|
+
it "allow reset of config" do
|
52
|
+
subject.configure {|c| c.validate_against = :my_user}
|
53
|
+
subject.reset_configuration
|
54
|
+
expect(subject.configuration.validate_against).to eq :user
|
55
|
+
end
|
56
|
+
|
26
57
|
end
|
27
58
|
|
28
59
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,26 +1,25 @@
|
|
1
1
|
require 'coveralls'
|
2
2
|
Coveralls.wear!
|
3
3
|
|
4
|
-
require
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_record'
|
5
6
|
require 'foreign_key_validation'
|
6
|
-
require 'rspec
|
7
|
+
require 'rspec'
|
7
8
|
require 'database_cleaner'
|
8
9
|
require 'pry'
|
9
10
|
|
10
11
|
RSpec.configure do |config|
|
11
12
|
config.run_all_when_everything_filtered = true
|
12
13
|
config.filter_run :focus
|
13
|
-
config.infer_base_class_for_anonymous_controllers = true
|
14
14
|
|
15
15
|
# reset and reload model classes for each run
|
16
16
|
config.before(:each) do
|
17
|
-
ForeignKeyValidation.
|
17
|
+
ForeignKeyValidation.reset_configuration
|
18
18
|
load "support/reset_models.rb"
|
19
19
|
load "support/load_models.rb"
|
20
20
|
end
|
21
21
|
|
22
22
|
config.before(:suite) do
|
23
|
-
puts "Running specs against Rails #{Rails.version}" if defined?(Rails)
|
24
23
|
DatabaseCleaner.strategy = :transaction
|
25
24
|
DatabaseCleaner.clean_with(:truncation)
|
26
25
|
end
|
data/spec/support/load_models.rb
CHANGED
metadata
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreign_key_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcus Geißler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - ! '>='
|
@@ -81,7 +95,7 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '10.1'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name: rspec
|
98
|
+
name: rspec
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ~>
|