foreign_key_validation 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2da928f68d5283f2afdbdf6f91f8a838bea9a56e
4
+ data.tar.gz: 5f979905a156bc8d56bb2510b25808a9ce5ef968
5
+ SHA512:
6
+ metadata.gz: e285968bf2b4e9b799679a4f7eecfe7f1d62aaa47b086f738a989acef715e22f55864b958d0ba5779b6cf398762ca455764c58c2de674a0bd3526baf50a6ecef
7
+ data.tar.gz: 771db50df035df9566356e647e162b2462fecd2c17c152e31cb1f22aed2783b4200719694acba0b2625f0d76a45498cf913d16a03769bfac766e5299ae5927d1
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in foreign_key_validation.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Marcus Geißler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Marcus Geißler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # foreign_key_validation
2
+
3
+ Protect your models by specifying a collection of foreign keys that should be tested for consistency with the `belongs_to` relations. For example, When the `user_id` is used in all models we can check if the `user_id` of `model a` matches `user_id` of `model b` before saving the records.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'foreign_key_validation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install foreign_key_validation
18
+
19
+ ## Usage
20
+
21
+ 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 accessed by `*_id` columns (except `user_id`) would be checked for a matching `user_id` (if the column exists).
22
+
23
+ Change behaviour by calling `validate_foreign_keys` with arguments hash.
24
+
25
+ validate_foreign_keys on: :admin_user_id, with: [:project_id]
26
+
27
+ This would only check `model.project.admin_user_id` to match `model.admin_user_id`.
28
+
29
+ ## Note
30
+
31
+ Only testet with ruby 2.x
32
+
33
+ ## TODO
34
+
35
+ - Tests!
36
+ - Support Ruby 1.9 (remove keyword arguments)
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/marcusg/foreign_key_validation/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'foreign_key_validation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "foreign_key_validation"
8
+ spec.version = ForeignKeyValidation::VERSION
9
+ spec.authors = ["Marcus Geißler"]
10
+ spec.email = ["marcus3006@gmail.com"]
11
+ spec.summary = %q{Protect the foreign keys in your Rails models.}
12
+ spec.description = %q{Protect the foreign keys in your Rails models. Really.}
13
+ spec.homepage = "https://github.com/marcusg/foreign_key_validation"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,50 @@
1
+ module ForeignKeyValidation
2
+ module ModelExtension
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def validate_foreign_key(key_to_validate_against, validation_key)
7
+ relation = validation_key.gsub('_id', '')
8
+
9
+ # do not try to validate if self does not respond to relation or one of the keys is nil
10
+ return if !respond_to?(relation) or send(relation).try(key_to_validate_against).nil? or send(key_to_validate_against).nil?
11
+
12
+ # add error if keys does not match
13
+ if send(relation).send(key_to_validate_against) != send(key_to_validate_against)
14
+ errors.add(key_to_validate_against, "#{key_to_validate_against} of #{relation} does not match #{self.class.name.tableize} #{key_to_validate_against}")
15
+ end
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def validate_foreign_keys(on: :user_id, with: nil)
22
+ key_to_validate_against = on
23
+
24
+ # check if key_to_validate_against is present as column
25
+ raise ArgumentError, "No foreign key #{key_to_validate_against} on #{self.table_name} table!" unless self.column_names.include?(key_to_validate_against.to_s)
26
+
27
+ # use provided 'with' array or column_names from self to get all foreign keys
28
+ keys_to_validate_with = (Array(with).map(&:to_s) if with) || self.column_names.select {|n| n.match(/\w_id/)}
29
+
30
+ # reject keys that match either the key_to_validate_against or the current class name key (needed for sti models)
31
+ keys_to_validate_with.reject! {|n| n == key_to_validate_against.to_s || n == "#{self.class.name.underscore}_id" }
32
+
33
+ define_method "validate_foreign_keys_on_#{key_to_validate_against}" do
34
+
35
+ keys_to_validate_with.each do |validation_key|
36
+ validate_foreign_key(key_to_validate_against, validation_key)
37
+ end
38
+
39
+ end
40
+ before_validation "validate_foreign_keys_on_#{key_to_validate_against}"
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ ActiveRecord::Base.send :include, ForeignKeyValidation::ModelExtension
@@ -0,0 +1,3 @@
1
+ module ForeignKeyValidation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "foreign_key_validation/version"
2
+ require "foreign_key_validation/model_extension"
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreign_key_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcus Geißler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Protect the foreign keys in your Rails models. Really.
42
+ email:
43
+ - marcus3006@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - foreign_key_validation.gemspec
55
+ - lib/foreign_key_validation.rb
56
+ - lib/foreign_key_validation/model_extension.rb
57
+ - lib/foreign_key_validation/version.rb
58
+ homepage: https://github.com/marcusg/foreign_key_validation
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Protect the foreign keys in your Rails models.
82
+ test_files: []