active_record_auto_validations 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +8 -0
- data/app/assets/config/auto_validations_manifest.js +1 -0
- data/app/assets/stylesheets/auto_validations/application.css +15 -0
- data/app/controllers/auto_validations/application_controller.rb +4 -0
- data/app/helpers/auto_validations/application_helper.rb +4 -0
- data/app/jobs/auto_validations/application_job.rb +4 -0
- data/app/mailers/auto_validations/application_mailer.rb +6 -0
- data/app/models/auto_validations/application_record.rb +5 -0
- data/app/models/concerns/auto_validations/model_concern.rb +7 -0
- data/app/services/auto_validations/auto_validate_model_class.rb +51 -0
- data/app/views/layouts/auto_validations/application.html.erb +15 -0
- data/config/routes.rb +2 -0
- data/lib/active_record_auto_validations/engine.rb +5 -0
- data/lib/active_record_auto_validations/version.rb +3 -0
- data/lib/active_record_auto_validations.rb +6 -0
- data/lib/tasks/auto_validations_tasks.rake +4 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f0f8bdbdb695e04d78b7e03badf31c5bda8ba83a299c64e05bd674f7ebee5309
|
4
|
+
data.tar.gz: d0db6d6605807a16c0cef57488011e440da7f148c74e499c69473576054df564
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a2888303307eb73773a4136d5d1d4d30f1b25476d6ee5047b8a6399e2d94e36944d8d0c1c3cc9765f16e4879425fc185fcd39c001e15024f567bc94f13f25bb
|
7
|
+
data.tar.gz: ab897c30fa03d44438ae8393bf35583bc1c471b0dfe046a94282f14c54b8be157f03a063b9df5bc4f02c575e6eaf3eed0b00b3651383c43d6dc230afba9d78be
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2022 kaspernj
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# ActiveRecordAutoValidations
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "active_record_auto_validations"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install active_record_auto_validations
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
//= link_directory ../stylesheets/active_record_auto_validations .css
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class ActiveRecordAutoValidations::AutoValidateModelClass < ApplicationService
|
2
|
+
attr_reader :model_class
|
3
|
+
|
4
|
+
def initialize(model_class:)
|
5
|
+
@model_class = model_class
|
6
|
+
end
|
7
|
+
|
8
|
+
def perform
|
9
|
+
begin
|
10
|
+
columns
|
11
|
+
rescue ActiveRecord::StatementInvalid => e
|
12
|
+
# Database is probably not running - we need to ignore this to make stuff like db:migrate, db:schema:load work
|
13
|
+
Rails.logger.error "AutoValidate: Ignoring error while loading columns, because database might not be initialized: #{e.message}"
|
14
|
+
return succeed!
|
15
|
+
end
|
16
|
+
|
17
|
+
insert_active_record_auto_validations!
|
18
|
+
succeed!
|
19
|
+
end
|
20
|
+
|
21
|
+
def columns
|
22
|
+
@columns ||= model_class.columns
|
23
|
+
end
|
24
|
+
|
25
|
+
def insert_active_record_auto_validations!
|
26
|
+
columns.each do |column|
|
27
|
+
next if column.name == "id" || column.name == "created_at" || column.name == "updated_at"
|
28
|
+
|
29
|
+
auto_validate_pesence_on_column!(column) if auto_validate_presence_on_column?(column)
|
30
|
+
auto_validate_max_length_on_column!(column) if auto_validate_max_length_on_column?(column)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def auto_validate_presence_on_column?(column)
|
35
|
+
!column.null && !column.name.end_with?("_id") && column.default.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def auto_validate_pesence_on_column!(column)
|
39
|
+
Rails.logger.info "AutoValidate: Adding presence validation to #{model_class.table_name}##{column.name}"
|
40
|
+
model_class.validates column.name.to_sym, presence: true
|
41
|
+
end
|
42
|
+
|
43
|
+
def auto_validate_max_length_on_column?(column)
|
44
|
+
column.type == :string && column.limit
|
45
|
+
end
|
46
|
+
|
47
|
+
def auto_validate_max_length_on_column!(column)
|
48
|
+
Rails.logger.info "AutoValidate: Adding maxlength of #{column.limit} validation to #{model_class.table_name}##{column.name}"
|
49
|
+
model_class.validates column.name.to_sym, allow_blank: true, length: {maximum: column.limit}
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Auto validations</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= stylesheet_link_tag "active_record_auto_validations/application", media: "all" %>
|
9
|
+
</head>
|
10
|
+
<body>
|
11
|
+
|
12
|
+
<%= yield %>
|
13
|
+
|
14
|
+
</body>
|
15
|
+
</html>
|
data/config/routes.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_auto_validations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kaspernj
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-19 00:00:00.000000000 Z
|
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: 7.0.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.3
|
27
|
+
description: Scans ActiveRecord models and adds automatic validations based on null,
|
28
|
+
max length etc.
|
29
|
+
email:
|
30
|
+
- k@spernj.org
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- app/assets/config/auto_validations_manifest.js
|
39
|
+
- app/assets/stylesheets/auto_validations/application.css
|
40
|
+
- app/controllers/auto_validations/application_controller.rb
|
41
|
+
- app/helpers/auto_validations/application_helper.rb
|
42
|
+
- app/jobs/auto_validations/application_job.rb
|
43
|
+
- app/mailers/auto_validations/application_mailer.rb
|
44
|
+
- app/models/auto_validations/application_record.rb
|
45
|
+
- app/models/concerns/auto_validations/model_concern.rb
|
46
|
+
- app/services/auto_validations/auto_validate_model_class.rb
|
47
|
+
- app/views/layouts/auto_validations/application.html.erb
|
48
|
+
- config/routes.rb
|
49
|
+
- lib/active_record_auto_validations.rb
|
50
|
+
- lib/active_record_auto_validations/engine.rb
|
51
|
+
- lib/active_record_auto_validations/version.rb
|
52
|
+
- lib/tasks/auto_validations_tasks.rake
|
53
|
+
homepage: https://github.com/kaspernj/active_record_auto_validations
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
homepage_uri: https://github.com/kaspernj/active_record_auto_validations
|
58
|
+
source_code_uri: https://github.com/kaspernj/active_record_auto_validations
|
59
|
+
changelog_uri: https://github.com/kaspernj/active_record_auto_validations
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.3.7
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Scans ActiveRecord models and adds automatic validations based on null, max
|
79
|
+
length etc.
|
80
|
+
test_files: []
|