valle 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +7 -44
- data/.dockerignore +0 -2
- data/.gitignore +0 -17
- data/.travis.yml +0 -21
- data/CHANGELOG.md +0 -58
- data/CONTRIBUTING.md +0 -33
- data/Dockerfile +0 -13
- data/Gemfile +0 -15
- data/LICENSE +0 -22
- data/Makefile +0 -7
- data/README.md +0 -115
- data/Rakefile +0 -18
- data/features/configuration.feature +0 -105
- data/features/sets_validations.feature +0 -31
- data/features/step_definitions/rails_steps.rb +0 -7
- data/features/support/env.rb +0 -7
- data/lib/valle.rb +0 -66
- data/lib/valle/abstract_adapter/abstract_column.rb +0 -45
- data/lib/valle/abstract_adapter/byte_limited_column.rb +0 -27
- data/lib/valle/abstract_adapter/character_limited_column.rb +0 -14
- data/lib/valle/abstract_adapter/column_wrapper.rb +0 -55
- data/lib/valle/abstract_adapter/unlimited_column.rb +0 -12
- data/lib/valle/configuration.rb +0 -30
- data/lib/valle/hooks.rb +0 -68
- data/lib/valle/manager.rb +0 -23
- data/lib/valle/railtie.rb +0 -7
- data/lib/valle/validation_setter.rb +0 -26
- data/test/lib/abstract_adapter/character_limited_column_test.rb +0 -25
- data/test/lib/abstract_adapter/column_wrapper_test.rb +0 -19
- data/test/test_helper.rb +0 -12
- data/valle.gemspec +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8efd60d9bd234ca36a726e2be05fd04474a8bc27
|
4
|
+
data.tar.gz: 9aa8800270686961886b2d9f287883f10f56eff4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb443ad52b18025dd993dbdc9092b4186d42900c3c9b9ffaf1c4f9b6c3261f65128ee2e9aa363cf600a7256a19133eb1b8d379603429cbb2e267e4575562f221
|
7
|
+
data.tar.gz: 6d61b74cb5d33921eb69825c77c51c795dd98e090b434919221b43f530520c123f78e0cb03988ebca66ae8e7faf70128844d2d0af4c13fc40c9fcdc679413dd9
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Kalyaev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -46,37 +46,7 @@ email:
|
|
46
46
|
executables: []
|
47
47
|
extensions: []
|
48
48
|
extra_rdoc_files: []
|
49
|
-
files:
|
50
|
-
- ".dockerignore"
|
51
|
-
- ".gitignore"
|
52
|
-
- ".travis.yml"
|
53
|
-
- CHANGELOG.md
|
54
|
-
- CONTRIBUTING.md
|
55
|
-
- Dockerfile
|
56
|
-
- Gemfile
|
57
|
-
- LICENSE
|
58
|
-
- Makefile
|
59
|
-
- README.md
|
60
|
-
- Rakefile
|
61
|
-
- features/configuration.feature
|
62
|
-
- features/sets_validations.feature
|
63
|
-
- features/step_definitions/rails_steps.rb
|
64
|
-
- features/support/env.rb
|
65
|
-
- lib/valle.rb
|
66
|
-
- lib/valle/abstract_adapter/abstract_column.rb
|
67
|
-
- lib/valle/abstract_adapter/byte_limited_column.rb
|
68
|
-
- lib/valle/abstract_adapter/character_limited_column.rb
|
69
|
-
- lib/valle/abstract_adapter/column_wrapper.rb
|
70
|
-
- lib/valle/abstract_adapter/unlimited_column.rb
|
71
|
-
- lib/valle/configuration.rb
|
72
|
-
- lib/valle/hooks.rb
|
73
|
-
- lib/valle/manager.rb
|
74
|
-
- lib/valle/railtie.rb
|
75
|
-
- lib/valle/validation_setter.rb
|
76
|
-
- test/lib/abstract_adapter/character_limited_column_test.rb
|
77
|
-
- test/lib/abstract_adapter/column_wrapper_test.rb
|
78
|
-
- test/test_helper.rb
|
79
|
-
- valle.gemspec
|
49
|
+
files: []
|
80
50
|
homepage: http://github.com/kaize/valle
|
81
51
|
licenses:
|
82
52
|
- MIT
|
@@ -97,15 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
67
|
version: '0'
|
98
68
|
requirements: []
|
99
69
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.6.
|
70
|
+
rubygems_version: 2.6.8
|
101
71
|
signing_key:
|
102
72
|
specification_version: 4
|
103
73
|
summary: Built-in limit validations for your ActiveRecord model.
|
104
|
-
test_files:
|
105
|
-
- features/configuration.feature
|
106
|
-
- features/sets_validations.feature
|
107
|
-
- features/step_definitions/rails_steps.rb
|
108
|
-
- features/support/env.rb
|
109
|
-
- test/lib/abstract_adapter/character_limited_column_test.rb
|
110
|
-
- test/lib/abstract_adapter/column_wrapper_test.rb
|
111
|
-
- test/test_helper.rb
|
74
|
+
test_files: []
|
data/.dockerignore
DELETED
data/.gitignore
DELETED
data/.travis.yml
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
sudo: false
|
2
|
-
|
3
|
-
rvm:
|
4
|
-
- 2.3.0
|
5
|
-
- ruby-head
|
6
|
-
- rbx-2
|
7
|
-
- jruby-head
|
8
|
-
|
9
|
-
matrix:
|
10
|
-
allow_failures:
|
11
|
-
- rvm: ruby-head
|
12
|
-
- rvm: rbx-2
|
13
|
-
- rvm: jruby-head
|
14
|
-
fast_finish: true
|
15
|
-
|
16
|
-
branches:
|
17
|
-
only:
|
18
|
-
- master
|
19
|
-
|
20
|
-
before_install:
|
21
|
-
- gem update bundler
|
data/CHANGELOG.md
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
## 1.1.1 / 2016-03-31
|
2
|
-
|
3
|
-
* fixed DEPRECATION WARNING: alias_method_chain is deprecated (#26) [Anton Kalyaev]
|
4
|
-
|
5
|
-
## 1.1.0 / 2016-03-18
|
6
|
-
|
7
|
-
* fixed #23: issue with config.chache_classes=true [a.nikishin]
|
8
|
-
|
9
|
-
## 1.0.1 / 2014-07-22
|
10
|
-
|
11
|
-
* fix has_and_belongs_to_many causes Model to crash on initialization (#18) [Anton Kalyaev]
|
12
|
-
|
13
|
-
## 1.0.0 / 2014-01-14
|
14
|
-
|
15
|
-
* attributes option changed to exclude_attributes [Anton Kalyaev]
|
16
|
-
* models option changed to exclude_models [Anton Kalyaev]
|
17
|
-
|
18
|
-
## 0.2.3 / 2013-12-16
|
19
|
-
|
20
|
-
* fixes issue #12 with breaking rake db:migrate:reset [Marcus Geißler]
|
21
|
-
|
22
|
-
## 0.2.2 / 2013-11-28
|
23
|
-
|
24
|
-
* fix relation does not exist error (fixes #10) [qblake]
|
25
|
-
|
26
|
-
## 0.2.1 / 2013-11-27
|
27
|
-
|
28
|
-
* fixed uninitialized constant AR::SchemaMigration [Anton Kalyaev]
|
29
|
-
* added limits for 6 byte integer [Anton Kalyaev]
|
30
|
-
|
31
|
-
## 0.2.0 / 2013-11-16
|
32
|
-
|
33
|
-
* added attributes option [Anton Kalyaev]
|
34
|
-
|
35
|
-
## 0.1.0 / 2013-11-08
|
36
|
-
|
37
|
-
* fixed Stack too deep error [Anton Kalyaev]
|
38
|
-
* enabled option now works [Anton Kalyaev]
|
39
|
-
* fixed db:migrate error [Anton Kalyaev]
|
40
|
-
* dot not override AR#valid? method [Anton Kalyaev]
|
41
|
-
|
42
|
-
## 0.0.3 / 2012-12-13
|
43
|
-
|
44
|
-
* added primary_key type [Anton Kalyaev]
|
45
|
-
* added missing argument to respond_to? [Anton Kalyaev]
|
46
|
-
* config is now working [Anton Kalyaev]
|
47
|
-
|
48
|
-
## 0.0.2 / 2012-12-11
|
49
|
-
|
50
|
-
* fixed inherited behavior; switched to valid? method (Fixes #3) [Anton Kalyaev]
|
51
|
-
* added support for integer columns [Anton Kalyaev]
|
52
|
-
* added 3 types of columns: character_limited, byte_limited and unlimited [Anton Kalyaev]
|
53
|
-
* removed engine [Anton Kalyaev]
|
54
|
-
* fix typos in README.md [Aleksandr Ivanov]
|
55
|
-
|
56
|
-
## 0.0.1 / 2012-12-09
|
57
|
-
|
58
|
-
* Initial release (with basic strings support)
|
data/CONTRIBUTING.md
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
# Contributing
|
2
|
-
|
3
|
-
We love pull requests. Here's a quick guide.
|
4
|
-
|
5
|
-
Fork, then clone the repo:
|
6
|
-
|
7
|
-
git clone git@github.com:your-username/valle.git
|
8
|
-
|
9
|
-
Make sure the tests pass:
|
10
|
-
|
11
|
-
rake
|
12
|
-
|
13
|
-
Make your change. Add tests for your change. Make the tests pass:
|
14
|
-
|
15
|
-
rake
|
16
|
-
|
17
|
-
Push to your fork and [submit a pull request][pr].
|
18
|
-
|
19
|
-
[pr]: https://github.com/kaize/valle/compare/
|
20
|
-
|
21
|
-
At this point you're waiting on us. We like to at least comment on pull requests
|
22
|
-
within three business days (and, typically, one business day). We may suggest
|
23
|
-
some changes or improvements or alternatives.
|
24
|
-
|
25
|
-
Some things that will increase the chance that your pull request is accepted:
|
26
|
-
|
27
|
-
* Write tests.
|
28
|
-
* Follow common [style guides][style].
|
29
|
-
* Write a [good commit message][commit].
|
30
|
-
* squash your commits before sending PR.
|
31
|
-
|
32
|
-
[style]: https://github.com/thoughtbot/guides/tree/master/style
|
33
|
-
[commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
|
data/Dockerfile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
FROM ruby:latest
|
2
|
-
|
3
|
-
# fixes cucumber bug where it cannot find bundler
|
4
|
-
RUN gem install bundler --no-ri --no-rdoc
|
5
|
-
|
6
|
-
RUN mkdir -p /usr/src/lib
|
7
|
-
WORKDIR /usr/src/lib
|
8
|
-
|
9
|
-
COPY Gemfile* /usr/src/lib/
|
10
|
-
COPY *.gemspec /usr/src/lib/
|
11
|
-
RUN bundle install
|
12
|
-
|
13
|
-
COPY . /usr/src/lib/
|
data/Gemfile
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in valle.gemspec
|
4
|
-
gemspec
|
5
|
-
|
6
|
-
gem 'rake'
|
7
|
-
|
8
|
-
group :test do
|
9
|
-
gem 'rails', '~> 4.2.6'
|
10
|
-
|
11
|
-
gem 'minitest', '~> 5'
|
12
|
-
gem 'minitest-reporters'
|
13
|
-
gem 'cucumber', '~> 2.3.2'
|
14
|
-
gem 'aruba', '~> 0.14.1'
|
15
|
-
end
|
data/LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2012 - 2016 Anton Kalyaev
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Makefile
DELETED
data/README.md
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
# Valle [![Build Status](https://travis-ci.org/akalyaev/valle.svg?branch=master)](https://travis-ci.org/akalyaev/valle) [![Code Climate](https://codeclimate.com/github/akalyaev/valle/badges/gpa.svg)](https://codeclimate.com/github/akalyaev/valle) [![Gem Version](https://badge.fury.io/rb/valle.svg)](https://badge.fury.io/rb/valle)
|
2
|
-
|
3
|
-
Valle automatically creates validations for the minimum and maximum values of fields in your ActiveRecord model(s). No more worrying that string lengths or ID values will exceed the permissible DB limits!
|
4
|
-
|
5
|
-
For example, the maximum length of the `string` type in PostgreSQL is 255. Valle creates the following validator for you, so you no longer need to write it by hand:
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
validates :field_name, length: { maximum: 255 }
|
9
|
-
```
|
10
|
-
|
11
|
-
Note: If you do not do this (and usually you are) and try to enter 2147483648 into a field of type `integer` (see the [Numeric types](http://www.postgresql.org/docs/9.2/static/datatype-numeric.html) section of PostgreSQL docs), you will get a 500 error.
|
12
|
-
|
13
|
-
Example:
|
14
|
-
|
15
|
-
PG::Error: ERROR: value "2147483648" is out of range for type integer
|
16
|
-
: SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
|
17
|
-
|
18
|
-
### Supported ActiveRecord field types
|
19
|
-
|
20
|
-
- `:primary_key`
|
21
|
-
- `:integer`
|
22
|
-
- `:string`
|
23
|
-
- `:text`
|
24
|
-
|
25
|
-
## Installation
|
26
|
-
|
27
|
-
Add this line to your application's Gemfile:
|
28
|
-
|
29
|
-
gem 'valle'
|
30
|
-
|
31
|
-
And then execute:
|
32
|
-
|
33
|
-
$ bundle
|
34
|
-
|
35
|
-
Or install it yourself:
|
36
|
-
|
37
|
-
$ gem install valle
|
38
|
-
|
39
|
-
If you are using other framework than Rails (e.g. Sinatra), call `Valle::Hooks.init` method during the boot process.
|
40
|
-
|
41
|
-
## Usage
|
42
|
-
|
43
|
-
By default, this gem adds validators to all your ActiveRecord models. If that is the behavior you want, you don't need to tweak it.
|
44
|
-
|
45
|
-
However, you can skip some of them by adding the file `config/initializers/valle.rb` containing:
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
Valle.configure do |config|
|
49
|
-
config.exclude_models = %w(Post)
|
50
|
-
end
|
51
|
-
```
|
52
|
-
|
53
|
-
Also, you can disable it temporarily by setting the `enabled` configuration option to `false`.
|
54
|
-
|
55
|
-
```ruby
|
56
|
-
Valle.configure do |config|
|
57
|
-
config.enabled = false
|
58
|
-
end
|
59
|
-
```
|
60
|
-
|
61
|
-
### Disabling Valle on specific attributes
|
62
|
-
|
63
|
-
There are cases where you need to skip validation for a particular attribute (see [#4](https://github.com/kaize/valle/issues/4)). For example, *CarrierWave* stores images temporarily in attributes, so calling `save` on them will fail because of its LengthValidator (255 characters maximum). You can disable Valle for such fields using the `exclude_attributes` configuration option:
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
Valle.configure do |config|
|
67
|
-
config.exclude_attributes = {
|
68
|
-
'User' => %w(image)
|
69
|
-
}
|
70
|
-
end
|
71
|
-
```
|
72
|
-
|
73
|
-
## Alternatives
|
74
|
-
|
75
|
-
There is a similar gem, called [validates_lengths_from_database](http://github.com/rubiety/validates_lengths_from_database). It solves only one part of the problem — applicable to strings. Valle, however, is designed to work with all possible field types.
|
76
|
-
|
77
|
-
## Docker container
|
78
|
-
|
79
|
-
If you have [docker](http://www.docker.com/) installed on your machine, you can
|
80
|
-
spin up a sandbox and run test suite in a few seconds:
|
81
|
-
|
82
|
-
```
|
83
|
-
$ docker build -t valle .
|
84
|
-
$ docker run -it --rm -v "$PWD":/usr/src/lib valle bundle exec rake
|
85
|
-
```
|
86
|
-
|
87
|
-
Or simply use Makefile commands:
|
88
|
-
|
89
|
-
```
|
90
|
-
$ make docker_build
|
91
|
-
$ make docker_test
|
92
|
-
```
|
93
|
-
|
94
|
-
## Contributing
|
95
|
-
|
96
|
-
1. Fork it
|
97
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
99
|
-
4. Run test suite (`rake test_suite`)
|
100
|
-
5. Push to the branch (`git push origin my-new-feature`)
|
101
|
-
6. Create new Pull Request
|
102
|
-
|
103
|
-
## Credits
|
104
|
-
|
105
|
-
Original authors:
|
106
|
-
|
107
|
-
- [Anton Kalyaev](http://github.com/akalyaev)
|
108
|
-
- [Andrew Kulakov](http://github.com/Andrew8xx8)
|
109
|
-
- [Alexander Kirillov](http://github.com/saratovsource)
|
110
|
-
|
111
|
-
Thank you to all our amazing [contributors](http://github.com/kaize/valle/contributors)!
|
112
|
-
|
113
|
-
## License
|
114
|
-
|
115
|
-
Valle is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
require "bundler/gem_tasks"
|
3
|
-
require 'rake/testtask'
|
4
|
-
require 'cucumber/rake/task'
|
5
|
-
|
6
|
-
Rake::TestTask.new do |t|
|
7
|
-
t.libs << "test"
|
8
|
-
t.test_files = FileList['test/lib/**/*_test.rb']
|
9
|
-
t.verbose = true
|
10
|
-
end
|
11
|
-
|
12
|
-
Cucumber::Rake::Task.new(:cucumber) do |t|
|
13
|
-
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'progress')]
|
14
|
-
end
|
15
|
-
|
16
|
-
task test_suite: [:test, :cucumber]
|
17
|
-
|
18
|
-
task default: :test_suite
|
@@ -1,105 +0,0 @@
|
|
1
|
-
Feature:
|
2
|
-
In order to temporary disable Valle or apply it only to a subset of models,
|
3
|
-
as a user of Rails, I would like to use valle initializer options.
|
4
|
-
|
5
|
-
Background:
|
6
|
-
Given I successfully run `bundle exec rails new testapp --skip-bundle --skip-sprockets --skip-javascript`
|
7
|
-
And I cd to "testapp"
|
8
|
-
And I add "factory_girl_rails" as a dependency
|
9
|
-
And I add "valle" from this project as a dependency
|
10
|
-
And I successfully run `bundle install`
|
11
|
-
And I successfully run `bundle exec rails g model User name:string`
|
12
|
-
And I successfully run `bundle exec rake db:migrate -v`
|
13
|
-
|
14
|
-
Scenario: Setting enabled option to false disable Valle
|
15
|
-
When I write to "config/initializers/valle.rb" with:
|
16
|
-
"""
|
17
|
-
Valle.configure do |config|
|
18
|
-
config.enabled = false
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
And I write to "test/unit/user_test.rb" with:
|
22
|
-
"""
|
23
|
-
require 'test_helper'
|
24
|
-
|
25
|
-
class UserTest < ActiveSupport::TestCase
|
26
|
-
test "should raise ActiveRecord::StatementInvalid error when name is too long" do
|
27
|
-
user = FactoryGirl.create(:user)
|
28
|
-
user.name = 'a' * 256
|
29
|
-
|
30
|
-
# SQLLite3 do not throw an ActiveRecord::StatementInvalid error
|
31
|
-
# so just check that record gets saved
|
32
|
-
assert user.save
|
33
|
-
end
|
34
|
-
end
|
35
|
-
"""
|
36
|
-
When I successfully run `bundle exec rake test -v`
|
37
|
-
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors"
|
38
|
-
|
39
|
-
Scenario: Using the exclude_models option should skip the models specified in the list
|
40
|
-
When I successfully run `bundle exec rails g model Post title:string`
|
41
|
-
And I successfully run `bundle exec rake db:migrate -v`
|
42
|
-
And I write to "config/initializers/valle.rb" with:
|
43
|
-
"""
|
44
|
-
Valle.configure do |config|
|
45
|
-
config.exclude_models = %w(User)
|
46
|
-
end
|
47
|
-
"""
|
48
|
-
And I write to "test/unit/user_test.rb" with:
|
49
|
-
"""
|
50
|
-
require 'test_helper'
|
51
|
-
|
52
|
-
class UserTest < ActiveSupport::TestCase
|
53
|
-
test "should raise ActiveRecord::StatementInvalid error when name is too long" do
|
54
|
-
user = FactoryGirl.create(:user)
|
55
|
-
user.name = 'a' * 256
|
56
|
-
|
57
|
-
# SQLLite3 do not throw an ActiveRecord::StatementInvalid error
|
58
|
-
# so just check that record gets saved
|
59
|
-
assert user.save
|
60
|
-
end
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
And I write to "test/unit/post_test.rb" with:
|
64
|
-
"""
|
65
|
-
require 'test_helper'
|
66
|
-
|
67
|
-
class PostTest < ActiveSupport::TestCase
|
68
|
-
test "should not save post when title is too long" do
|
69
|
-
post = FactoryGirl.create(:post)
|
70
|
-
post.title = 'a' * 256
|
71
|
-
|
72
|
-
assert !post.save
|
73
|
-
assert_equal 1, post.errors.count
|
74
|
-
end
|
75
|
-
end
|
76
|
-
"""
|
77
|
-
When I successfully run `bundle exec rake test -v`
|
78
|
-
Then the output should contain "2 runs, 3 assertions, 0 failures, 0 errors"
|
79
|
-
|
80
|
-
Scenario: Using exclude_attributes option should allow us to skip some attributes
|
81
|
-
When I write to "config/initializers/valle.rb" with:
|
82
|
-
"""
|
83
|
-
Valle.configure do |config|
|
84
|
-
config.exclude_attributes = {
|
85
|
-
'User' => %w(name)
|
86
|
-
}
|
87
|
-
end
|
88
|
-
"""
|
89
|
-
And I write to "test/unit/user_test.rb" with:
|
90
|
-
"""
|
91
|
-
require 'test_helper'
|
92
|
-
|
93
|
-
class UserTest < ActiveSupport::TestCase
|
94
|
-
test "should raise ActiveRecord::StatementInvalid error when name is too long" do
|
95
|
-
user = FactoryGirl.create(:user)
|
96
|
-
user.name = 'a' * 256
|
97
|
-
|
98
|
-
# SQLLite3 do not throw an ActiveRecord::StatementInvalid error
|
99
|
-
# so just check that record gets saved
|
100
|
-
assert user.save
|
101
|
-
end
|
102
|
-
end
|
103
|
-
"""
|
104
|
-
When I successfully run `bundle exec rake test -v`
|
105
|
-
Then the output should contain "1 runs, 1 assertions, 0 failures, 0 errors"
|
@@ -1,31 +0,0 @@
|
|
1
|
-
Feature: sets validations
|
2
|
-
Background:
|
3
|
-
Given I successfully run `bundle exec rails new testapp --skip-bundle --skip-sprockets --skip-javascript`
|
4
|
-
And I cd to "testapp"
|
5
|
-
And I add "factory_girl_rails" as a dependency
|
6
|
-
And I add "valle" from this project as a dependency
|
7
|
-
And I successfully run `bundle install`
|
8
|
-
And I successfully run `bundle exec rails g model User name:string`
|
9
|
-
And I successfully run `bundle exec rake db:migrate -v`
|
10
|
-
|
11
|
-
@disable-bundler
|
12
|
-
Scenario: Using Valle automatically sets validations
|
13
|
-
When I write to "test/unit/user_test.rb" with:
|
14
|
-
"""
|
15
|
-
require 'test_helper'
|
16
|
-
|
17
|
-
class UserTest < ActiveSupport::TestCase
|
18
|
-
setup do
|
19
|
-
@user = FactoryGirl.create(:user)
|
20
|
-
end
|
21
|
-
|
22
|
-
test "should not save user when name is too long" do
|
23
|
-
@user.name = 'a' * 256
|
24
|
-
|
25
|
-
assert !@user.save
|
26
|
-
assert_equal 1, @user.errors.count
|
27
|
-
end
|
28
|
-
end
|
29
|
-
"""
|
30
|
-
When I successfully run `bundle exec rake test -v`
|
31
|
-
Then the output should contain "1 runs, 2 assertions, 0 failures, 0 errors"
|
@@ -1,7 +0,0 @@
|
|
1
|
-
When /^I add "([^"]+)" from this project as a dependency$/ do |gem_name|
|
2
|
-
append_to_file('Gemfile', %{gem "#{gem_name}", :path => "#{PROJECT_ROOT}"\n})
|
3
|
-
end
|
4
|
-
|
5
|
-
When /^I add "([^"]+)" as a dependency$/ do |gem_name|
|
6
|
-
append_to_file('Gemfile', %{gem "#{gem_name}"\n})
|
7
|
-
end
|
data/features/support/env.rb
DELETED
data/lib/valle.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'valle/configuration'
|
2
|
-
|
3
|
-
module Valle
|
4
|
-
extend Configuration
|
5
|
-
|
6
|
-
# core
|
7
|
-
autoload :Manager, 'valle/manager'
|
8
|
-
autoload :ValidationSetter, 'valle/validation_setter'
|
9
|
-
|
10
|
-
# extensions
|
11
|
-
module AbstractAdapter
|
12
|
-
autoload :ColumnWrapper, 'valle/abstract_adapter/column_wrapper'
|
13
|
-
autoload :AbstractColumn, 'valle/abstract_adapter/abstract_column'
|
14
|
-
autoload :ByteLimitedColumn, 'valle/abstract_adapter/byte_limited_column'
|
15
|
-
autoload :CharacterLimitedColumn, 'valle/abstract_adapter/character_limited_column'
|
16
|
-
autoload :UnlimitedColumn, 'valle/abstract_adapter/unlimited_column'
|
17
|
-
end
|
18
|
-
|
19
|
-
# hooks
|
20
|
-
autoload :Hooks, 'valle/hooks'
|
21
|
-
|
22
|
-
class << self
|
23
|
-
|
24
|
-
##
|
25
|
-
# Is gem enabled (true by default)
|
26
|
-
#
|
27
|
-
# @see Valle::Configuration
|
28
|
-
#
|
29
|
-
def enabled?
|
30
|
-
options[:enabled]
|
31
|
-
end
|
32
|
-
|
33
|
-
##
|
34
|
-
# Can we process this model
|
35
|
-
#
|
36
|
-
# If the user wants to exclude some models,
|
37
|
-
# we need to check whether the model is on the list.
|
38
|
-
#
|
39
|
-
# @param [String] model_name the model name
|
40
|
-
# @see Valle::Configuration
|
41
|
-
#
|
42
|
-
def can_process_model?(model_name)
|
43
|
-
options[:exclude_models].nil? ||
|
44
|
-
options[:exclude_models].is_a?(Array) && !options[:exclude_models].include?(model_name)
|
45
|
-
end
|
46
|
-
|
47
|
-
##
|
48
|
-
# Can we process this attribute
|
49
|
-
#
|
50
|
-
# If the user wants to skip some attributes, we need to check
|
51
|
-
# whether we should add validators to that column or not.
|
52
|
-
#
|
53
|
-
# @param [String] model_name model name
|
54
|
-
# @param [String] attribute attribute name
|
55
|
-
# @see Valle::Configuration
|
56
|
-
#
|
57
|
-
def can_process_column?(model_name, attribute)
|
58
|
-
options[:exclude_attributes].nil? ||
|
59
|
-
!options[:exclude_attributes].has_key?(model_name) ||
|
60
|
-
options[:exclude_attributes][model_name].is_a?(Array) && !options[:exclude_attributes][model_name].include?(attribute)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# if not using Railtie, call `Valle::Hooks.init` directly
|
66
|
-
require 'valle/railtie' if defined? Rails
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
module AbstractAdapter
|
3
|
-
class AbstractColumn
|
4
|
-
|
5
|
-
##
|
6
|
-
# Adds more functionality to the standard ActiveRecord::ConnectionAdapters::Column
|
7
|
-
#
|
8
|
-
# @param [ActiveRecord::ConnectionAdapters::Column] original_column the original column
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# c = AbstractAdapter::AbstractColumn.new(original_column)
|
12
|
-
#
|
13
|
-
# c.minimum
|
14
|
-
# => -128
|
15
|
-
# c.maximum
|
16
|
-
# => 127
|
17
|
-
#
|
18
|
-
def initialize(original_column)
|
19
|
-
@original_column = original_column
|
20
|
-
end
|
21
|
-
|
22
|
-
def method_missing(method_name, *arguments, &block) # :nodoc:
|
23
|
-
@original_column.send(method_name, *arguments, &block)
|
24
|
-
end
|
25
|
-
|
26
|
-
def respond_to_method_missing?(method_name, include_private = false) # :nodoc:
|
27
|
-
@original_column.respond_to?(method_name, include_private)
|
28
|
-
end
|
29
|
-
|
30
|
-
##
|
31
|
-
# Get maximum possible value/length for this column
|
32
|
-
#
|
33
|
-
def maximum
|
34
|
-
raise NotImplementedError.new("You must implement maximum method.")
|
35
|
-
end
|
36
|
-
|
37
|
-
##
|
38
|
-
# Get minimum value/length for this column
|
39
|
-
#
|
40
|
-
def minimum
|
41
|
-
raise NotImplementedError.new("You must implement minimum method.")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
module AbstractAdapter
|
3
|
-
class ByteLimitedColumn < AbstractColumn
|
4
|
-
def maximum
|
5
|
-
case limit
|
6
|
-
when 1; 127
|
7
|
-
when 2; 32767
|
8
|
-
when 3; 8388607
|
9
|
-
when 4; 2147483647
|
10
|
-
when 6; 140737488355327
|
11
|
-
when 8; 9223372036854775807
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def minimum
|
16
|
-
case limit
|
17
|
-
when 1; -128
|
18
|
-
when 2; -32768
|
19
|
-
when 3; -8388608
|
20
|
-
when 4; -2147483648
|
21
|
-
when 6; -140737488355328
|
22
|
-
when 8; -9223372036854775808
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
module AbstractAdapter
|
3
|
-
class ColumnWrapper
|
4
|
-
|
5
|
-
class << self
|
6
|
-
|
7
|
-
##
|
8
|
-
# Wraps original column
|
9
|
-
#
|
10
|
-
# @param [ActiveRecord::ConnectionAdapters::Column] original_column the original column
|
11
|
-
#
|
12
|
-
def wrap(original_column)
|
13
|
-
case
|
14
|
-
when limit_in_bytes?(original_column)
|
15
|
-
ByteLimitedColumn.new(original_column)
|
16
|
-
when limit_in_characters?(original_column)
|
17
|
-
CharacterLimitedColumn.new(original_column)
|
18
|
-
else
|
19
|
-
UnlimitedColumn.new(original_column)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
##
|
26
|
-
# Determines whether the limit's method returned value is count of bytes
|
27
|
-
#
|
28
|
-
# Limit is number of bytes for :binary and :integer columns.
|
29
|
-
# @see http://apidock.com/rails/ActiveRecord/ConnectionAdapters/TableDefinition/column
|
30
|
-
#
|
31
|
-
def limit_in_bytes?(column)
|
32
|
-
case column.type
|
33
|
-
when :primary_key; true
|
34
|
-
when :integer; true
|
35
|
-
else false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
##
|
40
|
-
# Determines whether the limit's method returned value is count of characters
|
41
|
-
#
|
42
|
-
# Limit is number of characters for :string and :text columns
|
43
|
-
# @see http://apidock.com/rails/ActiveRecord/ConnectionAdapters/TableDefinition/column
|
44
|
-
#
|
45
|
-
def limit_in_characters?(column)
|
46
|
-
case column.type
|
47
|
-
when :string; true
|
48
|
-
when :text; true
|
49
|
-
else false
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/lib/valle/configuration.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
-
|
3
|
-
module Valle
|
4
|
-
module Configuration
|
5
|
-
|
6
|
-
mattr_accessor :options
|
7
|
-
|
8
|
-
self.options = {
|
9
|
-
enabled: true, # gem is enabled by default
|
10
|
-
exclude_models: nil, # selects all AR models by default
|
11
|
-
exclude_attributes: nil, # adds validators to all attributes by default
|
12
|
-
}
|
13
|
-
|
14
|
-
def configure
|
15
|
-
yield self
|
16
|
-
end
|
17
|
-
|
18
|
-
def enabled=(value)
|
19
|
-
self.options[:enabled] = value
|
20
|
-
end
|
21
|
-
|
22
|
-
def exclude_models=(collection)
|
23
|
-
self.options[:exclude_models] = collection
|
24
|
-
end
|
25
|
-
|
26
|
-
def exclude_attributes=(mapping)
|
27
|
-
self.options[:exclude_attributes] = mapping
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/lib/valle/hooks.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
|
3
|
-
##
|
4
|
-
# Extends the ActiveRecord's inherited method
|
5
|
-
#
|
6
|
-
module ActiveRecordExt
|
7
|
-
def inherited(subclass)
|
8
|
-
super
|
9
|
-
if Valle::Hooks.can_add_validators?(subclass, self)
|
10
|
-
Valle::Hooks.add_validators(subclass)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class Hooks
|
16
|
-
|
17
|
-
class << self
|
18
|
-
|
19
|
-
##
|
20
|
-
# Runs all the hooks, required for this gem
|
21
|
-
#
|
22
|
-
def init
|
23
|
-
ActiveSupport.on_load(:active_record) do
|
24
|
-
Valle::Hooks.run if Valle.enabled?
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
##
|
29
|
-
# Runs all the hooks, required for this gem
|
30
|
-
#
|
31
|
-
def run
|
32
|
-
ActiveRecord::Base.singleton_class.prepend(Valle::ActiveRecordExt)
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# Adds validators to subclass
|
37
|
-
#
|
38
|
-
# @param [ActiveRecord::Base] subclass the AR::Base child class
|
39
|
-
# @note ActiveRecord::Validations should be defined at this point
|
40
|
-
#
|
41
|
-
def add_validators(subclass)
|
42
|
-
Valle::Manager.add_validators(subclass)
|
43
|
-
end
|
44
|
-
|
45
|
-
##
|
46
|
-
# Check whenether we should add validators to subclass
|
47
|
-
#
|
48
|
-
# @param [ActiveRecord::Base] subclass the AR::Base child class
|
49
|
-
# @param [ActiveRecord::Base] inherited_from_class the AR::Base parent class
|
50
|
-
# @note ActiveRecord::Validations should be defined at this point
|
51
|
-
#
|
52
|
-
def can_add_validators?(subclass, inherited_from_class)
|
53
|
-
# skip AR::SchemaMigration (AR >= 4.X)
|
54
|
-
return false if (defined?(ActiveRecord::SchemaMigration) && subclass == ActiveRecord::SchemaMigration)
|
55
|
-
|
56
|
-
begin
|
57
|
-
model_name = subclass.model_name
|
58
|
-
|
59
|
-
Valle.can_process_model?(model_name) &&
|
60
|
-
inherited_from_class == ActiveRecord::Base &&
|
61
|
-
subclass.table_exists?
|
62
|
-
rescue ArgumentError
|
63
|
-
false
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
data/lib/valle/manager.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
class Manager
|
3
|
-
|
4
|
-
class << self
|
5
|
-
|
6
|
-
##
|
7
|
-
# Add validators for all columns of a given class
|
8
|
-
#
|
9
|
-
# @param [ActiveRecord::Base] klass the AR model class
|
10
|
-
#
|
11
|
-
def add_validators(klass)
|
12
|
-
columns = klass.columns
|
13
|
-
columns.each do |original_column|
|
14
|
-
next unless Valle.can_process_column?(klass.model_name.to_s, original_column.name)
|
15
|
-
|
16
|
-
column = AbstractAdapter::ColumnWrapper.wrap(original_column)
|
17
|
-
ValidationSetter.add_validator(column, klass)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/valle/railtie.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module Valle
|
2
|
-
class ValidationSetter
|
3
|
-
class << self
|
4
|
-
|
5
|
-
##
|
6
|
-
# Adds validator to the klass column depending on its type
|
7
|
-
#
|
8
|
-
# @param [AbstractAdapter::AbstractColumn] column the column
|
9
|
-
# @param [ActiveRecord::Base] klass the AR model class
|
10
|
-
#
|
11
|
-
def add_validator(column, klass)
|
12
|
-
options = {}
|
13
|
-
options[:minimum] = column.minimum if column.minimum
|
14
|
-
options[:maximum] = column.maximum if column.maximum
|
15
|
-
return false unless options.present?
|
16
|
-
|
17
|
-
case column.type
|
18
|
-
when :string, :text
|
19
|
-
klass.validates column.name, length: options
|
20
|
-
when :integer
|
21
|
-
klass.validates column.name, numericality: options
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class CharacterLimitedColumnTest < TestCase
|
4
|
-
|
5
|
-
def test_maximum_should_return_value_for_string_column
|
6
|
-
original_column = ::ActiveRecord::ConnectionAdapters::Column.new("test_column", "", ::ActiveRecord::Type::String.new(limit: 255))
|
7
|
-
column = Valle::AbstractAdapter::CharacterLimitedColumn.new(original_column)
|
8
|
-
|
9
|
-
assert column.maximum
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_maximum_should_return_correct_value_for_string_column_if_user_redefine_limit
|
13
|
-
original_column = ::ActiveRecord::ConnectionAdapters::Column.new("test_column", "", ::ActiveRecord::Type::String.new(limit: 200))
|
14
|
-
column = Valle::AbstractAdapter::CharacterLimitedColumn.new(original_column)
|
15
|
-
|
16
|
-
assert_equal 200, column.maximum
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_minimum_should_return_nil
|
20
|
-
original_column = ::ActiveRecord::ConnectionAdapters::Column.new("test_column", "", ::ActiveRecord::Type::String.new)
|
21
|
-
column = Valle::AbstractAdapter::CharacterLimitedColumn.new(original_column)
|
22
|
-
|
23
|
-
assert_nil column.minimum
|
24
|
-
end
|
25
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require "active_record"
|
3
|
-
|
4
|
-
class ColumnWrapperTest < TestCase
|
5
|
-
|
6
|
-
def test_wrap_should_return_instance_of_character_limited_column_for_string_column
|
7
|
-
column = ::ActiveRecord::ConnectionAdapters::Column.new("test_column", "", ::ActiveRecord::Type::String.new(limit: 255))
|
8
|
-
wrapped_column = Valle::AbstractAdapter::ColumnWrapper.wrap(column)
|
9
|
-
|
10
|
-
assert wrapped_column.is_a?(Valle::AbstractAdapter::CharacterLimitedColumn)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_wrap_should_return_instance_of_byte_limited_column_for_int_column
|
14
|
-
column = ::ActiveRecord::ConnectionAdapters::Column.new("test_column", 25, ::ActiveRecord::Type::Integer.new)
|
15
|
-
wrapped_column = Valle::AbstractAdapter::ColumnWrapper.wrap(column)
|
16
|
-
|
17
|
-
assert wrapped_column.is_a?(Valle::AbstractAdapter::ByteLimitedColumn)
|
18
|
-
end
|
19
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
Bundler.require
|
3
|
-
|
4
|
-
require "active_record"
|
5
|
-
require "active_support/core_ext/hash/keys"
|
6
|
-
|
7
|
-
require 'minitest/autorun'
|
8
|
-
require "minitest/reporters"
|
9
|
-
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(:color => true)]
|
10
|
-
|
11
|
-
class TestCase < MiniTest::Test
|
12
|
-
end
|
data/valle.gemspec
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
Gem::Specification.new do |gem|
|
4
|
-
gem.authors = ["Anton Kalyaev"]
|
5
|
-
gem.email = ["anton.kalyaev@gmail.com"]
|
6
|
-
gem.description = %q{Valle automatically sets minimum and maximum values for the fields of your ActiveRecord model(s), so you shouldn't worry, that string length or ID value will exceed the permissible limit.}
|
7
|
-
gem.summary = %q{Built-in limit validations for your ActiveRecord model.}
|
8
|
-
gem.homepage = "http://github.com/kaize/valle"
|
9
|
-
gem.license = "MIT"
|
10
|
-
|
11
|
-
gem.files = `git ls-files`.split($\)
|
12
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
-
gem.name = "valle"
|
15
|
-
gem.require_paths = ["lib"]
|
16
|
-
gem.version = "1.1.2"
|
17
|
-
|
18
|
-
gem.add_dependency 'activerecord', '>= 3.0'
|
19
|
-
gem.add_dependency 'activesupport'
|
20
|
-
end
|