model_guard 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +145 -0
- data/LICENSE +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/lib/model_guard/concerns/create_or_update_guard.rb +70 -0
- data/lib/model_guard/concerns/destroy_guard.rb +42 -0
- data/lib/model_guard/version.rb +3 -0
- data/lib/model_guard.rb +4 -0
- data/model_guard.gemspec +32 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c29591b1550e24e865cea207fd16c5411c82590af263b183e6e57e49aeeb2926
|
4
|
+
data.tar.gz: 517f5c5aaeea46026e48db66f9c282760fee77c6ba1b965d88dd08048a45734f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54d4f71aafac8ba06ea5b3122a3d1597748137c87e63642cca5f294373cfc0451ef9fd0f9690b0f3cfa14f980e41180b42bcb36575007753b9ee857cc96693a6
|
7
|
+
data.tar.gz: 299343b6d88f4fd44a91f0b136f7c105e74e326400f4bb3a6e761667c97c80f048023516ecd79736310410f6ac90c9e9c900a83ba88ceead8cd34bf1c4ad4104
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
model_guard (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (7.1.2)
|
10
|
+
actionview (= 7.1.2)
|
11
|
+
activesupport (= 7.1.2)
|
12
|
+
nokogiri (>= 1.8.5)
|
13
|
+
racc
|
14
|
+
rack (>= 2.2.4)
|
15
|
+
rack-session (>= 1.0.1)
|
16
|
+
rack-test (>= 0.6.3)
|
17
|
+
rails-dom-testing (~> 2.2)
|
18
|
+
rails-html-sanitizer (~> 1.6)
|
19
|
+
actionview (7.1.2)
|
20
|
+
activesupport (= 7.1.2)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.11)
|
23
|
+
rails-dom-testing (~> 2.2)
|
24
|
+
rails-html-sanitizer (~> 1.6)
|
25
|
+
activemodel (7.1.2)
|
26
|
+
activesupport (= 7.1.2)
|
27
|
+
activerecord (7.1.2)
|
28
|
+
activemodel (= 7.1.2)
|
29
|
+
activesupport (= 7.1.2)
|
30
|
+
timeout (>= 0.4.0)
|
31
|
+
activesupport (7.1.2)
|
32
|
+
base64
|
33
|
+
bigdecimal
|
34
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
35
|
+
connection_pool (>= 2.2.5)
|
36
|
+
drb
|
37
|
+
i18n (>= 1.6, < 2)
|
38
|
+
minitest (>= 5.1)
|
39
|
+
mutex_m
|
40
|
+
tzinfo (~> 2.0)
|
41
|
+
base64 (0.2.0)
|
42
|
+
bigdecimal (3.1.4)
|
43
|
+
builder (3.2.4)
|
44
|
+
concurrent-ruby (1.2.2)
|
45
|
+
connection_pool (2.4.1)
|
46
|
+
crass (1.0.6)
|
47
|
+
diff-lcs (1.5.0)
|
48
|
+
drb (2.2.0)
|
49
|
+
ruby2_keywords
|
50
|
+
erubi (1.12.0)
|
51
|
+
factory_bot (6.4.2)
|
52
|
+
activesupport (>= 5.0.0)
|
53
|
+
faker (3.2.2)
|
54
|
+
i18n (>= 1.8.11, < 2)
|
55
|
+
i18n (1.14.1)
|
56
|
+
concurrent-ruby (~> 1.0)
|
57
|
+
io-console (0.6.0)
|
58
|
+
irb (1.9.1)
|
59
|
+
rdoc
|
60
|
+
reline (>= 0.3.8)
|
61
|
+
loofah (2.22.0)
|
62
|
+
crass (~> 1.0.2)
|
63
|
+
nokogiri (>= 1.12.0)
|
64
|
+
minitest (5.20.0)
|
65
|
+
mutex_m (0.2.0)
|
66
|
+
nokogiri (1.15.5-arm64-darwin)
|
67
|
+
racc (~> 1.4)
|
68
|
+
nokogiri (1.15.5-x86_64-linux)
|
69
|
+
racc (~> 1.4)
|
70
|
+
psych (5.1.1.1)
|
71
|
+
stringio
|
72
|
+
racc (1.7.3)
|
73
|
+
rack (3.0.8)
|
74
|
+
rack-session (2.0.0)
|
75
|
+
rack (>= 3.0.0)
|
76
|
+
rack-test (2.1.0)
|
77
|
+
rack (>= 1.3)
|
78
|
+
rackup (2.1.0)
|
79
|
+
rack (>= 3)
|
80
|
+
webrick (~> 1.8)
|
81
|
+
rails-dom-testing (2.2.0)
|
82
|
+
activesupport (>= 5.0.0)
|
83
|
+
minitest
|
84
|
+
nokogiri (>= 1.6)
|
85
|
+
rails-html-sanitizer (1.6.0)
|
86
|
+
loofah (~> 2.21)
|
87
|
+
nokogiri (~> 1.14)
|
88
|
+
railties (7.1.2)
|
89
|
+
actionpack (= 7.1.2)
|
90
|
+
activesupport (= 7.1.2)
|
91
|
+
irb
|
92
|
+
rackup (>= 1.0.0)
|
93
|
+
rake (>= 12.2)
|
94
|
+
thor (~> 1.0, >= 1.2.2)
|
95
|
+
zeitwerk (~> 2.6)
|
96
|
+
rake (13.1.0)
|
97
|
+
rdoc (6.6.0)
|
98
|
+
psych (>= 4.0.0)
|
99
|
+
reline (0.4.0)
|
100
|
+
io-console (~> 0.5)
|
101
|
+
rspec-core (3.12.2)
|
102
|
+
rspec-support (~> 3.12.0)
|
103
|
+
rspec-expectations (3.12.3)
|
104
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
105
|
+
rspec-support (~> 3.12.0)
|
106
|
+
rspec-mocks (3.12.6)
|
107
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
108
|
+
rspec-support (~> 3.12.0)
|
109
|
+
rspec-rails (6.1.0)
|
110
|
+
actionpack (>= 6.1)
|
111
|
+
activesupport (>= 6.1)
|
112
|
+
railties (>= 6.1)
|
113
|
+
rspec-core (~> 3.12)
|
114
|
+
rspec-expectations (~> 3.12)
|
115
|
+
rspec-mocks (~> 3.12)
|
116
|
+
rspec-support (~> 3.12)
|
117
|
+
rspec-support (3.12.1)
|
118
|
+
ruby2_keywords (0.0.5)
|
119
|
+
shoulda-matchers (5.3.0)
|
120
|
+
activesupport (>= 5.2.0)
|
121
|
+
sqlite3 (1.6.8-arm64-darwin)
|
122
|
+
sqlite3 (1.6.8-x86_64-linux)
|
123
|
+
stringio (3.0.9)
|
124
|
+
thor (1.3.0)
|
125
|
+
timeout (0.4.1)
|
126
|
+
tzinfo (2.0.6)
|
127
|
+
concurrent-ruby (~> 1.0)
|
128
|
+
webrick (1.8.1)
|
129
|
+
zeitwerk (2.6.12)
|
130
|
+
|
131
|
+
PLATFORMS
|
132
|
+
arm64-darwin-22
|
133
|
+
x86_64-linux
|
134
|
+
|
135
|
+
DEPENDENCIES
|
136
|
+
activerecord (~> 7)
|
137
|
+
factory_bot
|
138
|
+
faker
|
139
|
+
model_guard!
|
140
|
+
rspec-rails
|
141
|
+
shoulda-matchers
|
142
|
+
sqlite3
|
143
|
+
|
144
|
+
BUNDLED WITH
|
145
|
+
2.4.10
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Martin Moen Wulffeld
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# model_guard
|
2
|
+
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/model_guard.svg)](https://rubygems.org/gems/model_guard)
|
4
|
+
[![Build Status](https://github.com/wulffeld/model_guard/workflows/CI/badge.svg?branch=main)](https://github.com/wulffeld/model_guard/actions?query=workflow%3ACI)
|
5
|
+
[![Gem Downloads](https://img.shields.io/gem/dt/model_guard.svg)](https://rubygems.org/gems/model_guard)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/7b3c646f87ca2d6d691c/maintainability)](https://codeclimate.com/github/wulffeld/model_guard)
|
7
|
+
|
8
|
+
Two small concerns for ActiveRecord that can guard against direct creates, updates and destroys. Handy if you want to force the use of directing all
|
9
|
+
persistence through service objects and guard against accidental CRUD operations
|
10
|
+
in the console.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Include the gem in your Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem "model_guard"
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### Create / update protection
|
23
|
+
|
24
|
+
Protects against #save, #save!, #update, #update!. Instead use #guarded_save, #guarded_save!, #guarded_update, #guarded_update! or #guarded_create.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
include ModelGuard::CreateOrUpdateGuard
|
29
|
+
end
|
30
|
+
|
31
|
+
CreateGuardedUser.guarded_create(name: "Joe")
|
32
|
+
user.guarded_update(name: "Joe")
|
33
|
+
```
|
34
|
+
|
35
|
+
or
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
user.allow_direct_create_or_update!
|
39
|
+
user.save
|
40
|
+
```
|
41
|
+
|
42
|
+
### Destroy protection
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class User < ActiveRecord::Base
|
46
|
+
include ModelGuard::DestroyGuard
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Will raise an exception when calling `destroy` on an instance of `User`. Instead use `guarded_destroy`, `guarded_destroy!` or `guarded_delete`.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
user.guarded_destroy
|
54
|
+
user.guarded_destroy!
|
55
|
+
```
|
56
|
+
|
57
|
+
## Support
|
58
|
+
|
59
|
+
Submit suggestions or feature requests as a GitHub Issue or Pull
|
60
|
+
Request (preferred). If you send a pull request, remember to update the
|
61
|
+
corresponding unit tests and/or add new tests.
|
62
|
+
|
63
|
+
## Versioning
|
64
|
+
|
65
|
+
Semantic Versioning 2.0 as defined at <http://semver.org>.
|
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module ModelGuard
|
2
|
+
class DirectCreateOrUpdateException < Exception; end
|
3
|
+
|
4
|
+
module CreateOrUpdateGuard
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_eval do
|
9
|
+
attr_accessor :model_guard_save_okay
|
10
|
+
|
11
|
+
before_create do
|
12
|
+
raise(DirectCreateOrUpdateException.new("Can't create object #{self.class.to_s} directly.")) unless @model_guard_save_okay
|
13
|
+
end
|
14
|
+
|
15
|
+
after_create do
|
16
|
+
disallow_direct_create_or_update!
|
17
|
+
end
|
18
|
+
|
19
|
+
before_update do
|
20
|
+
raise(DirectCreateOrUpdateException.new("Can't update object #{self.class.to_s} directly.")) unless @model_guard_save_okay
|
21
|
+
end
|
22
|
+
|
23
|
+
after_update do
|
24
|
+
disallow_direct_create_or_update!
|
25
|
+
end
|
26
|
+
|
27
|
+
def allow_direct_create_or_update!
|
28
|
+
@model_guard_save_okay = true
|
29
|
+
end
|
30
|
+
|
31
|
+
def disallow_direct_create_or_update!
|
32
|
+
@model_guard_save_okay = false
|
33
|
+
end
|
34
|
+
|
35
|
+
def guarded_save
|
36
|
+
allow_direct_create_or_update!
|
37
|
+
save
|
38
|
+
end
|
39
|
+
|
40
|
+
def guarded_save!
|
41
|
+
allow_direct_create_or_update!
|
42
|
+
save!
|
43
|
+
end
|
44
|
+
|
45
|
+
def guarded_update(attributes)
|
46
|
+
allow_direct_create_or_update!
|
47
|
+
update(attributes)
|
48
|
+
end
|
49
|
+
|
50
|
+
def guarded_update!(attributes)
|
51
|
+
allow_direct_create_or_update!
|
52
|
+
update!(attributes)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.guarded_create(attributes = nil, &block)
|
56
|
+
if attributes.is_a?(Array)
|
57
|
+
attributes.collect do |attr|
|
58
|
+
guarded_create(attr, &block)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
object = new(attributes, &block)
|
62
|
+
object.allow_direct_create_or_update!
|
63
|
+
object.save!
|
64
|
+
object
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ModelGuard
|
2
|
+
class DirectDestroyException < Exception; end
|
3
|
+
|
4
|
+
module DestroyGuard
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_eval do
|
9
|
+
attr_accessor :model_guard_destroy_okay
|
10
|
+
|
11
|
+
before_destroy do
|
12
|
+
raise(DirectDestroyException.new("Can't destroy #{self.class.to_s} directly.")) unless @model_guard_destroy_okay
|
13
|
+
end
|
14
|
+
|
15
|
+
def allow_direct_destroy!
|
16
|
+
@model_guard_destroy_okay = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def disallow_direct_destroy!
|
20
|
+
@model_guard_destroy_okay = false
|
21
|
+
end
|
22
|
+
|
23
|
+
def guarded_destroy
|
24
|
+
allow_direct_destroy! and destroy
|
25
|
+
end
|
26
|
+
|
27
|
+
def guarded_destroy!
|
28
|
+
allow_direct_destroy! and destroy!
|
29
|
+
end
|
30
|
+
|
31
|
+
def guarded_delete
|
32
|
+
allow_direct_destroy! and delete
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete(**args)
|
36
|
+
raise(DirectDestroyException.new("Can't delete #{self.class.to_s} directly.")) unless @model_guard_destroy_okay
|
37
|
+
super(args)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/model_guard.rb
ADDED
data/model_guard.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "lib/model_guard/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "model_guard"
|
5
|
+
spec.version = ModelGuard::VERSION
|
6
|
+
spec.authors = ["Martin Moen Wulffeld"]
|
7
|
+
spec.email = ["martin@wulffeld.dk"]
|
8
|
+
|
9
|
+
spec.summary = "Various concerns for ActiveModel."
|
10
|
+
spec.description = "Sometimes you want to protect your models from being updated or destroyed directly. This gem provides concerns for that."
|
11
|
+
spec.homepage = "https://github.com/wulffeld/model_guard"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = ">= 3.0"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/wulffeld/model_guard"
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/wulffeld/model_guard/CHANGELOG.md"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "activerecord", "~> 7"
|
31
|
+
spec.add_development_dependency "sqlite3"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_guard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Moen Wulffeld
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
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: Sometimes you want to protect your models from being updated or destroyed
|
42
|
+
directly. This gem provides concerns for that.
|
43
|
+
email:
|
44
|
+
- martin@wulffeld.dk
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/model_guard.rb
|
56
|
+
- lib/model_guard/concerns/create_or_update_guard.rb
|
57
|
+
- lib/model_guard/concerns/destroy_guard.rb
|
58
|
+
- lib/model_guard/version.rb
|
59
|
+
- model_guard.gemspec
|
60
|
+
homepage: https://github.com/wulffeld/model_guard
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/wulffeld/model_guard
|
65
|
+
source_code_uri: https://github.com/wulffeld/model_guard
|
66
|
+
changelog_uri: https://github.com/wulffeld/model_guard/CHANGELOG.md
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.4.10
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Various concerns for ActiveModel.
|
86
|
+
test_files: []
|