foreign_key_validation 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2da928f68d5283f2afdbdf6f91f8a838bea9a56e
4
- data.tar.gz: 5f979905a156bc8d56bb2510b25808a9ce5ef968
3
+ metadata.gz: 6efa881cebd85a67f4706bf7251ee36caa3cc710
4
+ data.tar.gz: 52bcaf493a5508f7f519e6af787082ad82ead28e
5
5
  SHA512:
6
- metadata.gz: e285968bf2b4e9b799679a4f7eecfe7f1d62aaa47b086f738a989acef715e22f55864b958d0ba5779b6cf398762ca455764c58c2de674a0bd3526baf50a6ecef
7
- data.tar.gz: 771db50df035df9566356e647e162b2462fecd2c17c152e31cb1f22aed2783b4200719694acba0b2625f0d76a45498cf913d16a03769bfac766e5299ae5927d1
6
+ metadata.gz: 353a11c25d79edd2b1148118dc08989f972244f9592f5414a24826418a66789343b32cecf6959fa511237d4131101c761731a1b252fcc62d000aa7a1bb79dad8
7
+ data.tar.gz: 6a9c0ed52a1e93b28435bc87dcd977304c3f15aee5b2f516c84b1683d7de7c2864514e4c7c0ab1946946efc3b04f91be6f2c29ec3a3f4b2e6bb6595971322fb8
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ foreign_key_validation
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
@@ -18,6 +18,12 @@ 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_dependency "rails", ">= 3.2"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rake", "~> 10.1"
25
+ spec.add_development_dependency "rspec-rails", "~> 3.0"
26
+ spec.add_development_dependency "sqlite3", '~> 1.3'
27
+ spec.add_development_dependency "pry", '~> 0.10'
28
+
23
29
  end
@@ -28,7 +28,7 @@ module ForeignKeyValidation
28
28
  keys_to_validate_with = (Array(with).map(&:to_s) if with) || self.column_names.select {|n| n.match(/\w_id/)}
29
29
 
30
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" }
31
+ keys_to_validate_with.reject! {|n| n.to_s == key_to_validate_against.to_s || n.to_s == "#{self.class.name.underscore}_id" }
32
32
 
33
33
  define_method "validate_foreign_keys_on_#{key_to_validate_against}" do
34
34
 
@@ -1,3 +1,3 @@
1
1
  module ForeignKeyValidation
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ class User < ActiveRecord::Base
4
+ has_many :projects
5
+ has_many :ideas
6
+ end
7
+
8
+ class Project < ActiveRecord::Base
9
+ belongs_to :user
10
+ has_many :ideas
11
+ validates_presence_of :user_id, presence: true
12
+ end
13
+
14
+ class Idea < ActiveRecord::Base
15
+ belongs_to :project
16
+ belongs_to :user
17
+ validates_presence_of :user_id, :project_id, presence: true
18
+ end
19
+
20
+ describe "Behaviour without validate_foreign_keys" do
21
+
22
+ let!(:user) { User.create }
23
+ let!(:project) { user.projects.create }
24
+ let!(:idea) { project.ideas.create user: user }
25
+
26
+ it "uses same user ids by default" do
27
+ expect(project.user_id).to eq(user.id)
28
+ expect(user.id).to eq(user.id)
29
+ expect(idea.user_id).to eq(user.id)
30
+ end
31
+
32
+ it "allow to rewrite user id of idea" do
33
+ idea.user_id = 42
34
+ idea.save
35
+ idea.reload
36
+ expect(idea.user_id).to eq(42)
37
+ end
38
+
39
+ it "allow to rewrite user id of project" do
40
+ project.user_id = 42
41
+ project.save
42
+ project.reload
43
+ expect(project.user_id).to eq(42)
44
+ end
45
+
46
+ end
47
+
48
+ describe "Behaviour with validate_foreign_keys" do
49
+
50
+ before do
51
+ Idea.class_eval do
52
+ validate_foreign_keys
53
+ end
54
+ end
55
+
56
+ let!(:user) { User.create }
57
+ let!(:project) { user.projects.create }
58
+ let!(:idea) { project.ideas.create user: user }
59
+
60
+ it "uses same user ids by default" do
61
+ expect(project.user_id).to eq(user.id)
62
+ expect(user.id).to eq(user.id)
63
+ expect(idea.user_id).to eq(user.id)
64
+ end
65
+
66
+ it "does not allow to rewrite user id of idea" do
67
+ idea.user_id = 42
68
+ idea.save
69
+ expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
70
+ expect(idea.reload.user_id).to_not eq(42)
71
+ end
72
+
73
+ it "does allow to rewrite user id of project" do
74
+ project.user_id = 42
75
+ project.save
76
+ expect(project.errors).to be_empty
77
+ expect(project.reload.user_id).to eq(42)
78
+ end
79
+
80
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define(:version => 20130730063732) do
2
+
3
+ create_table "users", :force => true do |t|
4
+ end
5
+
6
+ create_table "projects", :force => true do |t|
7
+ t.integer "user_id"
8
+ end
9
+
10
+ create_table "ideas", :force => true do |t|
11
+ t.integer "user_id"
12
+ t.integer "project_id"
13
+ end
14
+
15
+ end
@@ -0,0 +1,18 @@
1
+ require "rails/all"
2
+ require 'foreign_key_validation'
3
+ require 'rspec/rails'
4
+ require 'pry'
5
+
6
+ RSpec.configure do |config|
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.infer_base_class_for_anonymous_controllers = true
10
+
11
+ # config.order = 'random'
12
+ end
13
+
14
+ setup_sqlite_db = lambda do
15
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
16
+ load "schema.rb"
17
+ end
18
+ silence_stream(STDOUT, &setup_sqlite_db)
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Geißler
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -28,16 +42,58 @@ dependencies:
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ">="
45
+ - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '0'
47
+ version: '10.1'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ">="
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
39
95
  - !ruby/object:Gem::Version
40
- version: '0'
96
+ version: '0.10'
41
97
  description: Protect the foreign keys in your Rails models. Really.
42
98
  email:
43
99
  - marcus3006@gmail.com
@@ -46,6 +102,9 @@ extensions: []
46
102
  extra_rdoc_files: []
47
103
  files:
48
104
  - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
49
108
  - Gemfile
50
109
  - LICENSE
51
110
  - LICENSE.txt
@@ -55,6 +114,9 @@ files:
55
114
  - lib/foreign_key_validation.rb
56
115
  - lib/foreign_key_validation/model_extension.rb
57
116
  - lib/foreign_key_validation/version.rb
117
+ - spec/models/model_spec.rb
118
+ - spec/schema.rb
119
+ - spec/spec_helper.rb
58
120
  homepage: https://github.com/marcusg/foreign_key_validation
59
121
  licenses:
60
122
  - MIT
@@ -79,4 +141,7 @@ rubygems_version: 2.2.2
79
141
  signing_key:
80
142
  specification_version: 4
81
143
  summary: Protect the foreign keys in your Rails models.
82
- test_files: []
144
+ test_files:
145
+ - spec/models/model_spec.rb
146
+ - spec/schema.rb
147
+ - spec/spec_helper.rb