foreign_key_validation 1.1.1 → 1.1.2

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
- YjZkZDY5NzBlNGQ2ZTQ0MThkMjc5OWY4MGU3ZDVjZDVkMTg5NjdiNQ==
4
+ YmM1OWE1NWI4NzQzZjM0MmFkNDg1NTIwOGYwMmRjM2YzYTAzZmQ4MQ==
5
5
  data.tar.gz: !binary |-
6
- NmU5Y2NmNGQ3M2YyNjFlOWI4YmI3M2JlZTZhYTM3NDA0MWU0NzFmYg==
6
+ YWI5MTJmYWRmOWNiMjllNzY1NjA4NjFiZGU0ZTM3MmYzNDIwMGVjNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmY4NTllNGJhZTc0ODhlNmE0YjM3ZmYyNWU0OTZhMDQzOGYwMzVjZDkwYzIy
10
- Y2VhOWNiZGE4MDczNTU4OGRlYWZmNmY0ODQ0YTU1ZmU5Mjg4MDZjNDY4NGNk
11
- ZDM4NjgxYzM4NDQwODk1NzlmYWZkNzRkYmQ5NDczMzYzNGU4Y2Y=
9
+ MTVlOTJkNzlmZjE5OGUyZjY3ZTQ4ZWUxNTUxNjlhOWE4MjBmYWNhMjRlOWE0
10
+ NjI3NTYyZDMzNDcyOTdiZDk0NDc2YzBmZDBkMDdmNjQwOTBmZmYwMGI0NTJi
11
+ OTRhMzY0ZDRlMWQ2MTY4OWRjZjg2OTkwMjUzYjYxZDA0ZDZhNjE=
12
12
  data.tar.gz: !binary |-
13
- Y2QwMTRkNzg4ZWE5MjVmOTc3N2I4NTM1YTk5OTg2Zjk4N2VkODE0OTJlYWYx
14
- ODUxOTdjMDBhZWNmMzcwNmJiZGExOTI0MTExY2I0NGZmYTU1MzBiNDkwZmE4
15
- MWE0MTM1YmJhYmFlODMzM2FlNzZiZGEwYTM4MmRmOGVhNDNhN2E=
13
+ MTZkYWYwZDU2OTg1ZjZiZjUyMTY3NWJiMGFlZWQyZWY1ODQwZGVmNTU2YmM0
14
+ ZGUwMzdhMjc0MDEyZDUwNDc5NWI1ZTVlNjBjOTJhMTcxOTZkMGQxMzg1N2Q4
15
+ YjNiMTE1YWM1YzIzZTk5ODBjNTA2ZDA3YWM3OThkMzYwZGRlNTk=
data/Appraisals CHANGED
@@ -1,19 +1,23 @@
1
1
  appraise "3.2" do
2
- gem "rails", "~> 3.2.0"
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 "rails", "~> 4.0.0"
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 "rails", "~> 4.1.0"
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 "rails", "~> 4.2.0.beta2"
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
- rails >= 3.2.0
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 = proc { |key, name, object| "My custom msg!" }
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 Rails
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 "rails", ">= 3.2"
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-rails', '~> 3.0'
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
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 3.2.0"
5
+ gem "activerecord", "~> 3.2.0"
6
+ gem "activesupport", "~> 3.2.0"
6
7
 
7
8
  gemspec :path => "../"
data/gemfiles/4.0.gemfile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 4.0.0"
5
+ gem "activerecord", "~> 4.0.0"
6
+ gem "activesupport", "~> 4.0.0"
6
7
 
7
8
  gemspec :path => "../"
data/gemfiles/4.1.gemfile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 4.1.0"
5
+ gem "activerecord", "~> 4.1.0"
6
+ gem "activesupport", "~> 4.1.0"
6
7
 
7
8
  gemspec :path => "../"
data/gemfiles/4.2.gemfile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 4.2.0.beta2"
5
+ gem "activerecord", "~> 4.2.0.beta4"
6
+ gem "activesupport", "~> 4.2.0.beta4"
6
7
 
7
8
  gemspec :path => "../"
@@ -11,21 +11,25 @@ module ForeignKeyValidation
11
11
  DEFAULT_CONFIG = {
12
12
  inject_subclasses: true,
13
13
  validate_against: :user,
14
- error_message: proc { |key, reflection_name, object|
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(&blk)
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
 
@@ -13,7 +13,7 @@ module ForeignKeyValidation
13
13
  return true
14
14
  end
15
15
  collector.klass.send :private, filter_name.to_sym
16
- collector.klass.send :before_validation, filter_name
16
+ collector.klass.public_send :before_validation, filter_name
17
17
  end
18
18
 
19
19
  private
@@ -5,7 +5,7 @@ module ForeignKeyValidation
5
5
  module ClassMethods
6
6
 
7
7
  def validate_foreign_keys(opt={})
8
- subclasses.map {|klass| klass.send(:validate_foreign_keys, opt)} if ForeignKeyValidation.configuration.inject_subclasses
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
- has_errors = false
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
- attach_error(reflection_name)
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.send(relation).try(validate_against_key)
29
+ object.public_send(relation).try(validate_against_key)
29
30
  end
30
31
 
31
32
  def key_on_object
@@ -1,3 +1,3 @@
1
1
  module ForeignKeyValidation
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -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(ForeignKeyValidation::Collector.new(klass: Issue).check!).to be true
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{ForeignKeyValidation::Collector.new(klass: Dummy).check!}.to raise_error(ForeignKeyValidation::Errors::NoReleationFoundError)
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 "rails/all"
4
+ require 'active_support'
5
+ require 'active_record'
5
6
  require 'foreign_key_validation'
6
- require 'rspec/rails'
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.configuration = nil
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
@@ -1,5 +1,3 @@
1
- require "active_record"
2
-
3
1
  class User < ActiveRecord::Base
4
2
  has_many :projects
5
3
  has_many :ideas
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.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-10 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
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-rails
98
+ name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ~>