sanitization 1.0.2 → 1.1.3
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 +4 -4
- data/.github/workflows/main.yml +35 -0
- data/Appraisals +9 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +24 -23
- data/README.md +49 -15
- data/gemfiles/rails_6.gemfile +14 -0
- data/gemfiles/rails_6.gemfile.lock +68 -0
- data/gemfiles/rails_7.gemfile +14 -0
- data/gemfiles/rails_7.gemfile.lock +66 -0
- data/lib/sanitization/active_record_extension.rb +32 -19
- data/lib/sanitization/configuration.rb +64 -0
- data/lib/sanitization/version.rb +1 -1
- data/lib/sanitization.rb +1 -0
- data/sanitization.gemspec +4 -3
- metadata +29 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f6205f9747ede56298a9f328432e9b429a22cfddc4553dd208acbb0358f52f38
|
|
4
|
+
data.tar.gz: 53f878df3e4a8d6988115cf4ededfa7c86d80dd2421bced1557aee34d3c2129c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 63dbe605e5f06b74f943b3a06c1632a287e9b3be699a1357a7846c7eecc1ed48a47bced5d69828735085aeff9fc03b669acd9691ad34406224d80631b3cd91de
|
|
7
|
+
data.tar.gz: 7634312dc87b1f209702b9080e70446da6c584afb33bfd71c2fef88bebfe687d173259c0245004d2e3804a8ec312aeb7aec5e038a2d6e20c93531b43d0275be4
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
2
|
+
# They are provided by a third-party and are governed by
|
|
3
|
+
# separate terms of service, privacy policy, and support
|
|
4
|
+
# documentation.
|
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
|
7
|
+
|
|
8
|
+
name: Ruby
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [ main ]
|
|
13
|
+
pull_request:
|
|
14
|
+
branches: [ main ]
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
test:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
strategy:
|
|
20
|
+
matrix:
|
|
21
|
+
ruby-version: ['2.7', '3.0']
|
|
22
|
+
|
|
23
|
+
steps:
|
|
24
|
+
- uses: actions/checkout@v2
|
|
25
|
+
- name: Set up Ruby
|
|
26
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
|
27
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
|
28
|
+
uses: ruby/setup-ruby@v1
|
|
29
|
+
with:
|
|
30
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
31
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
32
|
+
- name: Install dependencies
|
|
33
|
+
run: bundle exec appraisal install
|
|
34
|
+
- name: Run tests
|
|
35
|
+
run: bundle exec appraisal rspec
|
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# 1.1.3
|
|
2
|
+
* Official support for Rails 7
|
|
3
|
+
|
|
4
|
+
# 1.1.2
|
|
5
|
+
* Bug fixes
|
|
6
|
+
|
|
7
|
+
# 1.1.1
|
|
8
|
+
* Changed `sanitization` method to `sanitizes` as the new preferred way. `sanitization` still works and is an alias of `sanitizes`.
|
|
9
|
+
|
|
10
|
+
# 1.1.0
|
|
11
|
+
* **BREAKING CHANGE:** By default, Sanitization now does nothing. A configuration block should be used to set your desired defaults. Add `Sanitization.simple_defaults!` to `config/initializers/sanitization.rb` for version 1.0.x defaults.
|
|
12
|
+
* Added support for configuration block.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# 1.0.0
|
|
16
|
+
* Initial Release
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
sanitization (1.
|
|
4
|
+
sanitization (1.1.3)
|
|
5
5
|
activerecord
|
|
6
6
|
activesupport
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
activemodel (
|
|
12
|
-
activesupport (=
|
|
13
|
-
activerecord (
|
|
14
|
-
activemodel (=
|
|
15
|
-
activesupport (=
|
|
16
|
-
activesupport (
|
|
11
|
+
activemodel (7.0.0)
|
|
12
|
+
activesupport (= 7.0.0)
|
|
13
|
+
activerecord (7.0.0)
|
|
14
|
+
activemodel (= 7.0.0)
|
|
15
|
+
activesupport (= 7.0.0)
|
|
16
|
+
activesupport (7.0.0)
|
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
|
-
i18n (>=
|
|
19
|
-
minitest (
|
|
20
|
-
tzinfo (~>
|
|
21
|
-
|
|
18
|
+
i18n (>= 1.6, < 2)
|
|
19
|
+
minitest (>= 5.1)
|
|
20
|
+
tzinfo (~> 2.0)
|
|
21
|
+
appraisal (2.4.1)
|
|
22
|
+
bundler
|
|
23
|
+
rake
|
|
24
|
+
thor (>= 0.14.0)
|
|
22
25
|
byebug (11.1.3)
|
|
23
|
-
concurrent-ruby (1.1.
|
|
26
|
+
concurrent-ruby (1.1.9)
|
|
27
|
+
cucumber-ci-environment (8.0.1)
|
|
24
28
|
diff-lcs (1.4.4)
|
|
25
|
-
i18n (1.8.
|
|
29
|
+
i18n (1.8.11)
|
|
26
30
|
concurrent-ruby (~> 1.0)
|
|
27
|
-
minitest (5.
|
|
31
|
+
minitest (5.15.0)
|
|
28
32
|
rake (12.3.3)
|
|
29
33
|
rspec (3.10.0)
|
|
30
34
|
rspec-core (~> 3.10.0)
|
|
@@ -40,24 +44,21 @@ GEM
|
|
|
40
44
|
rspec-support (~> 3.10.0)
|
|
41
45
|
rspec-support (3.10.2)
|
|
42
46
|
sqlite3 (1.4.2)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
thread_safe (0.3.6)
|
|
47
|
-
tzinfo (1.2.8)
|
|
48
|
-
thread_safe (~> 0.1)
|
|
49
|
-
zeitwerk (2.4.2)
|
|
47
|
+
thor (1.1.0)
|
|
48
|
+
tzinfo (2.0.4)
|
|
49
|
+
concurrent-ruby (~> 1.0)
|
|
50
50
|
|
|
51
51
|
PLATFORMS
|
|
52
52
|
ruby
|
|
53
53
|
|
|
54
54
|
DEPENDENCIES
|
|
55
|
+
appraisal (~> 2.4.1)
|
|
55
56
|
byebug
|
|
57
|
+
cucumber-ci-environment
|
|
56
58
|
rake (~> 12.0)
|
|
57
59
|
rspec (~> 3.0)
|
|
58
60
|
sanitization!
|
|
59
61
|
sqlite3
|
|
60
|
-
temping (~> 3.0)
|
|
61
62
|
|
|
62
63
|
BUNDLED WITH
|
|
63
|
-
2.
|
|
64
|
+
2.2.22
|
data/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Sanitization makes it easy to store slightly cleaner strings to your database.
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
### Features (all optional):
|
|
6
7
|
|
|
7
8
|
- White space stripping
|
|
@@ -9,14 +10,37 @@ Sanitization makes it easy to store slightly cleaner strings to your database.
|
|
|
9
10
|
- Empty string to nil (if database column supports it)
|
|
10
11
|
- Change casing (ie. upcase, downcase, titlecase, etc)
|
|
11
12
|
|
|
13
|
+
|
|
12
14
|
### Defaults
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
By default, Sanitization has all options disabled. It is recommended you use a configuration block to set
|
|
17
|
+
sensitive defaults for your projects.
|
|
18
|
+
|
|
19
|
+
For example, I use:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
# config/initializers/sanitization.rb
|
|
23
|
+
|
|
24
|
+
Sanitization.configure do |config|
|
|
25
|
+
config.strip = true
|
|
26
|
+
config.collapse = true
|
|
27
|
+
config.nullify = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# or you can use the following shortcut instead:
|
|
31
|
+
|
|
32
|
+
Sanitization.simple_defaults!
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Configuration Options
|
|
37
|
+
|
|
38
|
+
- Strip leading & training white spaces (`strip: true|false`)
|
|
39
|
+
- Collapse consecutive spaces (`collapse: true|false`)
|
|
40
|
+
- Store empty strings as `null` if the database column allows it (`nullify: true|false`)
|
|
17
41
|
- All String columns are sanitized (`only: nil, except: nil`)
|
|
18
|
-
-
|
|
19
|
-
-
|
|
42
|
+
- Also sanitize strings of type `text` (`include_text_type: true|false`)
|
|
43
|
+
- Change casing: (`case: :none|:up|:down|:custom`)
|
|
20
44
|
|
|
21
45
|
|
|
22
46
|
## Installation
|
|
@@ -29,51 +53,61 @@ bundle add sanitization
|
|
|
29
53
|
## Usage
|
|
30
54
|
|
|
31
55
|
```ruby
|
|
56
|
+
|
|
57
|
+
# Assuming the following configuration block:
|
|
58
|
+
Sanitization.configure do |config|
|
|
59
|
+
config.strip = true
|
|
60
|
+
config.collapse = true
|
|
61
|
+
config.nullify = true
|
|
62
|
+
end
|
|
63
|
+
|
|
32
64
|
# Default settings for all strings
|
|
33
65
|
class Person < ApplicationModel
|
|
34
|
-
|
|
66
|
+
sanitizes
|
|
35
67
|
# is equivalent to:
|
|
36
|
-
|
|
68
|
+
sanitizes strip: true, collapse: true, include_text_type: false
|
|
37
69
|
end
|
|
38
70
|
|
|
39
71
|
# Default settings for all strings, except a specific column
|
|
40
72
|
class Person < ApplicationModel
|
|
41
|
-
|
|
73
|
+
sanitizes except: :alias
|
|
42
74
|
end
|
|
43
75
|
|
|
44
76
|
# Default settings + titlecase for specific columns
|
|
45
77
|
class Person < ApplicationModel
|
|
46
|
-
|
|
78
|
+
sanitizes only: [:first_name, :last_name], case: :title
|
|
47
79
|
end
|
|
48
80
|
|
|
49
81
|
# Complex example. All these lines could be used in combination.
|
|
50
82
|
class Person
|
|
51
83
|
# Apply default settings and `titlecase` to all string columns, except `description`.
|
|
52
|
-
|
|
84
|
+
sanitizes case: :title, except: :description
|
|
53
85
|
|
|
54
86
|
# Keep previous settings, but specify `upcase` for 2 columns.
|
|
55
|
-
|
|
87
|
+
sanitizes only: [:first_name, :last_name], case: :up
|
|
56
88
|
|
|
57
89
|
# Keep previous settings, but specify `downcase` for a single column.
|
|
58
|
-
|
|
90
|
+
sanitizes only: :email, case: :downcase
|
|
59
91
|
|
|
60
92
|
# Apply default settings to column `description`, of type `text`. By default, `text` type is NOT sanitized.
|
|
61
|
-
|
|
93
|
+
sanitizes only: :description, include_text_type: true
|
|
62
94
|
|
|
63
95
|
# Disable collapsing for `do_not_collapse`.
|
|
64
|
-
|
|
96
|
+
sanitizes only: :do_not_collapse, collapse: false
|
|
65
97
|
|
|
66
98
|
# Sanitize with a custom casing method named `leetcase` for the `133t` column.
|
|
67
99
|
# Don't nullify empty strings.
|
|
68
|
-
|
|
100
|
+
sanitizes only: '1337', case: :leet, nullify: false
|
|
69
101
|
end
|
|
70
102
|
|
|
71
103
|
```
|
|
72
104
|
|
|
105
|
+
|
|
73
106
|
## Development
|
|
74
107
|
|
|
75
108
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
76
109
|
|
|
110
|
+
|
|
77
111
|
## License
|
|
78
112
|
|
|
79
113
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "rake", "~> 12.0"
|
|
6
|
+
gem "rspec", "~> 3.0"
|
|
7
|
+
gem "appraisal", "~> 2.4.1"
|
|
8
|
+
gem "cucumber-ci-environment"
|
|
9
|
+
gem "sqlite3"
|
|
10
|
+
gem "byebug"
|
|
11
|
+
gem "activerecord", "~> 6.1.4"
|
|
12
|
+
gem "activesupport", "~> 6.1.4"
|
|
13
|
+
|
|
14
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: ..
|
|
3
|
+
specs:
|
|
4
|
+
sanitization (1.1.2)
|
|
5
|
+
activerecord
|
|
6
|
+
activesupport
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (6.1.4.1)
|
|
12
|
+
activesupport (= 6.1.4.1)
|
|
13
|
+
activerecord (6.1.4.1)
|
|
14
|
+
activemodel (= 6.1.4.1)
|
|
15
|
+
activesupport (= 6.1.4.1)
|
|
16
|
+
activesupport (6.1.4.1)
|
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
|
+
i18n (>= 1.6, < 2)
|
|
19
|
+
minitest (>= 5.1)
|
|
20
|
+
tzinfo (~> 2.0)
|
|
21
|
+
zeitwerk (~> 2.3)
|
|
22
|
+
appraisal (2.4.1)
|
|
23
|
+
bundler
|
|
24
|
+
rake
|
|
25
|
+
thor (>= 0.14.0)
|
|
26
|
+
byebug (11.1.3)
|
|
27
|
+
concurrent-ruby (1.1.9)
|
|
28
|
+
cucumber-ci-environment (8.0.1)
|
|
29
|
+
diff-lcs (1.4.4)
|
|
30
|
+
i18n (1.8.11)
|
|
31
|
+
concurrent-ruby (~> 1.0)
|
|
32
|
+
minitest (5.14.4)
|
|
33
|
+
rake (12.3.3)
|
|
34
|
+
rspec (3.10.0)
|
|
35
|
+
rspec-core (~> 3.10.0)
|
|
36
|
+
rspec-expectations (~> 3.10.0)
|
|
37
|
+
rspec-mocks (~> 3.10.0)
|
|
38
|
+
rspec-core (3.10.1)
|
|
39
|
+
rspec-support (~> 3.10.0)
|
|
40
|
+
rspec-expectations (3.10.1)
|
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
42
|
+
rspec-support (~> 3.10.0)
|
|
43
|
+
rspec-mocks (3.10.2)
|
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
|
+
rspec-support (~> 3.10.0)
|
|
46
|
+
rspec-support (3.10.3)
|
|
47
|
+
sqlite3 (1.4.2)
|
|
48
|
+
thor (1.1.0)
|
|
49
|
+
tzinfo (2.0.4)
|
|
50
|
+
concurrent-ruby (~> 1.0)
|
|
51
|
+
zeitwerk (2.5.1)
|
|
52
|
+
|
|
53
|
+
PLATFORMS
|
|
54
|
+
arm64-darwin-21
|
|
55
|
+
|
|
56
|
+
DEPENDENCIES
|
|
57
|
+
activerecord (~> 6.1.4)
|
|
58
|
+
activesupport (~> 6.1.4)
|
|
59
|
+
appraisal (~> 2.4.1)
|
|
60
|
+
byebug
|
|
61
|
+
cucumber-ci-environment
|
|
62
|
+
rake (~> 12.0)
|
|
63
|
+
rspec (~> 3.0)
|
|
64
|
+
sanitization!
|
|
65
|
+
sqlite3
|
|
66
|
+
|
|
67
|
+
BUNDLED WITH
|
|
68
|
+
2.2.22
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "rake", "~> 12.0"
|
|
6
|
+
gem "rspec", "~> 3.0"
|
|
7
|
+
gem "appraisal", "~> 2.4.1"
|
|
8
|
+
gem "cucumber-ci-environment"
|
|
9
|
+
gem "sqlite3"
|
|
10
|
+
gem "byebug"
|
|
11
|
+
gem "activerecord", "~> 7.0"
|
|
12
|
+
gem "activesupport", "~> 7.0"
|
|
13
|
+
|
|
14
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: ..
|
|
3
|
+
specs:
|
|
4
|
+
sanitization (1.1.2)
|
|
5
|
+
activerecord
|
|
6
|
+
activesupport
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
activemodel (7.0.0)
|
|
12
|
+
activesupport (= 7.0.0)
|
|
13
|
+
activerecord (7.0.0)
|
|
14
|
+
activemodel (= 7.0.0)
|
|
15
|
+
activesupport (= 7.0.0)
|
|
16
|
+
activesupport (7.0.0)
|
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
|
+
i18n (>= 1.6, < 2)
|
|
19
|
+
minitest (>= 5.1)
|
|
20
|
+
tzinfo (~> 2.0)
|
|
21
|
+
appraisal (2.4.1)
|
|
22
|
+
bundler
|
|
23
|
+
rake
|
|
24
|
+
thor (>= 0.14.0)
|
|
25
|
+
byebug (11.1.3)
|
|
26
|
+
concurrent-ruby (1.1.9)
|
|
27
|
+
cucumber-ci-environment (8.0.1)
|
|
28
|
+
diff-lcs (1.5.0)
|
|
29
|
+
i18n (1.8.11)
|
|
30
|
+
concurrent-ruby (~> 1.0)
|
|
31
|
+
minitest (5.15.0)
|
|
32
|
+
rake (12.3.3)
|
|
33
|
+
rspec (3.10.0)
|
|
34
|
+
rspec-core (~> 3.10.0)
|
|
35
|
+
rspec-expectations (~> 3.10.0)
|
|
36
|
+
rspec-mocks (~> 3.10.0)
|
|
37
|
+
rspec-core (3.10.1)
|
|
38
|
+
rspec-support (~> 3.10.0)
|
|
39
|
+
rspec-expectations (3.10.1)
|
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
41
|
+
rspec-support (~> 3.10.0)
|
|
42
|
+
rspec-mocks (3.10.2)
|
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
44
|
+
rspec-support (~> 3.10.0)
|
|
45
|
+
rspec-support (3.10.3)
|
|
46
|
+
sqlite3 (1.4.2)
|
|
47
|
+
thor (1.1.0)
|
|
48
|
+
tzinfo (2.0.4)
|
|
49
|
+
concurrent-ruby (~> 1.0)
|
|
50
|
+
|
|
51
|
+
PLATFORMS
|
|
52
|
+
arm64-darwin-21
|
|
53
|
+
|
|
54
|
+
DEPENDENCIES
|
|
55
|
+
activerecord (~> 7.0)
|
|
56
|
+
activesupport (~> 7.0)
|
|
57
|
+
appraisal (~> 2.4.1)
|
|
58
|
+
byebug
|
|
59
|
+
cucumber-ci-environment
|
|
60
|
+
rake (~> 12.0)
|
|
61
|
+
rspec (~> 3.0)
|
|
62
|
+
sanitization!
|
|
63
|
+
sqlite3
|
|
64
|
+
|
|
65
|
+
BUNDLED WITH
|
|
66
|
+
2.2.22
|
|
@@ -9,17 +9,18 @@ module Sanitization
|
|
|
9
9
|
attr_accessor :sanitization__store
|
|
10
10
|
|
|
11
11
|
private
|
|
12
|
-
def
|
|
12
|
+
def sanitizes(options = {})
|
|
13
13
|
# Skip initialization if table is not yet created. For example, during migrations.
|
|
14
|
-
|
|
14
|
+
begin
|
|
15
|
+
return unless ActiveRecord::Base.connection.data_source_exists?(self.table_name)
|
|
16
|
+
rescue ActiveRecord::NoDatabaseError
|
|
17
|
+
return
|
|
18
|
+
end
|
|
15
19
|
|
|
16
20
|
self.sanitization__store ||= {}
|
|
17
21
|
|
|
18
|
-
options[:only]
|
|
19
|
-
options[:except]
|
|
20
|
-
options[:strip] = !!(options[:strip].nil? ? true : options[:strip])
|
|
21
|
-
options[:collapse] = !!(options[:collapse].nil? ? true : options[:collapse])
|
|
22
|
-
options[:nullify] = !!(options[:nullify].nil? ? true : options[:nullify])
|
|
22
|
+
options[:only] = Array.wrap(options[:only])
|
|
23
|
+
options[:except] = Array.wrap(options[:except])
|
|
23
24
|
|
|
24
25
|
unless options[:case].nil?
|
|
25
26
|
raise ArgumentError.new("Invalid type for `case`: #{options[:case].class}") \
|
|
@@ -41,8 +42,13 @@ module Sanitization
|
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
if options[:case]
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
@valid_case_methods ||= String.new.methods.map { |m|
|
|
46
|
+
m.to_s if m.to_s =~ /case$/
|
|
47
|
+
}.compact
|
|
48
|
+
|
|
49
|
+
raise ArgumentError.new("Method not found: `:#{options[:case]}`. " +
|
|
50
|
+
"Valid methods are: :#{@valid_case_methods.join(', :')}") \
|
|
51
|
+
unless @valid_case_methods.include?(options[:case]) || options[:case] == :none
|
|
46
52
|
end
|
|
47
53
|
|
|
48
54
|
columns_to_format.each do |col|
|
|
@@ -55,12 +61,7 @@ module Sanitization
|
|
|
55
61
|
before_save :sanitization__format_strings
|
|
56
62
|
EOV
|
|
57
63
|
end
|
|
58
|
-
|
|
59
|
-
def valid_case_methods
|
|
60
|
-
String.new.methods.map { |m|
|
|
61
|
-
m.to_s if m.to_s =~ /case$/
|
|
62
|
-
}.compact
|
|
63
|
-
end
|
|
64
|
+
alias sanitization sanitizes
|
|
64
65
|
end # module ClassMethods
|
|
65
66
|
|
|
66
67
|
module InstanceMethods
|
|
@@ -95,9 +96,9 @@ module Sanitization
|
|
|
95
96
|
def sanitization__format_column(col_name, col_formatting)
|
|
96
97
|
return unless self[col_name].is_a?(String)
|
|
97
98
|
|
|
98
|
-
self[col_name].strip! if col_formatting
|
|
99
|
+
self[col_name].strip! if value_or_default(col_formatting, :strip)
|
|
99
100
|
|
|
100
|
-
if col_formatting
|
|
101
|
+
if value_or_default(col_formatting, :collapse)
|
|
101
102
|
if MULTIBYTE_SUPPORTED && Encoding.compatible?(self[col_name], MULTIBYTE_BLANK)
|
|
102
103
|
self[col_name].gsub!(/#{MULTIBYTE_BLANK}+/, " ")
|
|
103
104
|
else
|
|
@@ -105,15 +106,27 @@ module Sanitization
|
|
|
105
106
|
end
|
|
106
107
|
end
|
|
107
108
|
|
|
108
|
-
if col_formatting
|
|
109
|
+
if value_or_default(col_formatting, :nullify) && !self[col_name].nil? && self[col_name].to_s.empty? && \
|
|
109
110
|
self.class.columns.select { |c| c.name == col_name }.first.null
|
|
110
111
|
return self[col_name] = nil
|
|
111
112
|
end
|
|
112
113
|
|
|
113
|
-
|
|
114
|
+
case_formatting_method = value_or_default(col_formatting, :case)
|
|
115
|
+
if !case_formatting_method.nil? && case_formatting_method != :none
|
|
116
|
+
self[col_name] = self[col_name].send(case_formatting_method)
|
|
117
|
+
end
|
|
118
|
+
|
|
114
119
|
self[col_name]
|
|
115
120
|
end
|
|
116
121
|
|
|
122
|
+
def value_or_default(col_formatting, transform)
|
|
123
|
+
if col_formatting[transform].nil?
|
|
124
|
+
Sanitization.configuration[transform]
|
|
125
|
+
else
|
|
126
|
+
col_formatting[transform]
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
117
130
|
|
|
118
131
|
end # module InstanceMethods
|
|
119
132
|
end # module ActiveRecordExt
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Sanitization
|
|
2
|
+
class << self
|
|
3
|
+
def configuration
|
|
4
|
+
@configuration ||= Configuration.new
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def configuration=(c)
|
|
8
|
+
@configuration = c
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.configure
|
|
13
|
+
yield(configuration)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.simple_defaults!
|
|
17
|
+
self.configuration.simple!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Configuration
|
|
21
|
+
DEFAULTS = {
|
|
22
|
+
strip: false,
|
|
23
|
+
collapse: false,
|
|
24
|
+
case: :none,
|
|
25
|
+
nullify: false,
|
|
26
|
+
include_text_type: false
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
SIMPLE_DEFAULTS = {
|
|
30
|
+
strip: true,
|
|
31
|
+
collapse: true,
|
|
32
|
+
case: :none,
|
|
33
|
+
nullify: true,
|
|
34
|
+
include_text_type: false
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
attr_accessor *DEFAULTS.keys
|
|
38
|
+
|
|
39
|
+
def initialize(opts = {})
|
|
40
|
+
opts = DEFAULTS.merge((opts || {}).slice(DEFAULTS.keys))
|
|
41
|
+
|
|
42
|
+
opts.each_pair do |k,v|
|
|
43
|
+
self.instance_variable_set("@#{k}", v)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def [](k)
|
|
48
|
+
raise ArgumentError.new("Invalid parameter: #{k}") unless DEFAULTS.keys.include?(k)
|
|
49
|
+
self.instance_variable_get("@#{k}")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def clear!
|
|
53
|
+
DEFAULTS.each_pair do |k,v|
|
|
54
|
+
self.instance_variable_set("@#{k}", v)
|
|
55
|
+
end; self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def simple!
|
|
59
|
+
SIMPLE_DEFAULTS.each_pair do |k,v|
|
|
60
|
+
self.instance_variable_set("@#{k}", v)
|
|
61
|
+
end; self
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/sanitization/version.rb
CHANGED
data/lib/sanitization.rb
CHANGED
data/sanitization.gemspec
CHANGED
|
@@ -6,11 +6,11 @@ Gem::Specification.new do |spec|
|
|
|
6
6
|
spec.authors = ["Carl Mercier"]
|
|
7
7
|
spec.email = ["foss@carlmercier.com"]
|
|
8
8
|
|
|
9
|
-
spec.summary = %q{}
|
|
10
|
-
spec.description = %q{}
|
|
9
|
+
spec.summary = %q{Sanitization makes it easy to store slightly cleaner strings to your database.}
|
|
10
|
+
spec.description = %q{Sanitization makes it easy to store slightly cleaner strings to your database.}
|
|
11
11
|
spec.homepage = "https://github.com/cmer/sanitization"
|
|
12
12
|
spec.license = "MIT"
|
|
13
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.
|
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
|
14
14
|
|
|
15
15
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
16
16
|
|
|
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
|
|
|
28
28
|
spec.require_paths = ["lib"]
|
|
29
29
|
spec.add_runtime_dependency "activerecord"
|
|
30
30
|
spec.add_runtime_dependency "activesupport"
|
|
31
|
+
spec.add_development_dependency "appraisal"
|
|
31
32
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sanitization
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Carl Mercier
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-
|
|
11
|
+
date: 2021-12-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -38,15 +38,33 @@ dependencies:
|
|
|
38
38
|
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
|
-
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: appraisal
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: Sanitization makes it easy to store slightly cleaner strings to your
|
|
56
|
+
database.
|
|
42
57
|
email:
|
|
43
58
|
- foss@carlmercier.com
|
|
44
59
|
executables: []
|
|
45
60
|
extensions: []
|
|
46
61
|
extra_rdoc_files: []
|
|
47
62
|
files:
|
|
63
|
+
- ".github/workflows/main.yml"
|
|
48
64
|
- ".gitignore"
|
|
49
65
|
- ".rspec"
|
|
66
|
+
- Appraisals
|
|
67
|
+
- CHANGELOG.md
|
|
50
68
|
- Gemfile
|
|
51
69
|
- Gemfile.lock
|
|
52
70
|
- LICENSE.txt
|
|
@@ -54,8 +72,13 @@ files:
|
|
|
54
72
|
- Rakefile
|
|
55
73
|
- bin/console
|
|
56
74
|
- bin/setup
|
|
75
|
+
- gemfiles/rails_6.gemfile
|
|
76
|
+
- gemfiles/rails_6.gemfile.lock
|
|
77
|
+
- gemfiles/rails_7.gemfile
|
|
78
|
+
- gemfiles/rails_7.gemfile.lock
|
|
57
79
|
- lib/sanitization.rb
|
|
58
80
|
- lib/sanitization/active_record_extension.rb
|
|
81
|
+
- lib/sanitization/configuration.rb
|
|
59
82
|
- lib/sanitization/version.rb
|
|
60
83
|
- sanitization.gemspec
|
|
61
84
|
homepage: https://github.com/cmer/sanitization
|
|
@@ -74,15 +97,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
74
97
|
requirements:
|
|
75
98
|
- - ">="
|
|
76
99
|
- !ruby/object:Gem::Version
|
|
77
|
-
version: 2.
|
|
100
|
+
version: 2.7.0
|
|
78
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
102
|
requirements:
|
|
80
103
|
- - ">="
|
|
81
104
|
- !ruby/object:Gem::Version
|
|
82
105
|
version: '0'
|
|
83
106
|
requirements: []
|
|
84
|
-
rubygems_version: 3.
|
|
107
|
+
rubygems_version: 3.2.22
|
|
85
108
|
signing_key:
|
|
86
109
|
specification_version: 4
|
|
87
|
-
summary:
|
|
110
|
+
summary: Sanitization makes it easy to store slightly cleaner strings to your database.
|
|
88
111
|
test_files: []
|