valle 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/README.md +14 -4
- data/features/configuration.feature +79 -0
- data/features/sets_validations.feature +28 -0
- data/lib/valle.rb +27 -1
- data/lib/valle/abstract_adapter/abstract_column.rb +3 -6
- data/lib/valle/abstract_adapter/column_wrapper.rb +1 -0
- data/lib/valle/hooks.rb +5 -3
- data/lib/valle/{bounds_manager.rb → manager.rb} +1 -1
- data/lib/valle/version.rb +1 -1
- data/valle.gemspec +2 -2
- metadata +13 -11
- data/features/adds_validators.feature +0 -52
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## 0.0.3 / 2012-12-13
|
2
|
+
|
3
|
+
* added primary_key type [Anton Kalyaev]
|
4
|
+
* added missing argument to respond_to? [Anton Kalyaev]
|
5
|
+
* config is now working [Anton Kalyaev]
|
6
|
+
|
1
7
|
## 0.0.2 / 2012-12-11
|
2
8
|
|
3
9
|
* fixed inherited behavior; switched to valid? method (Fixes #3) [Anton Kalyaev]
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# Valle [![Build Status](https://secure.travis-ci.org/kaize/valle.png "Build Status")](http://travis-ci.org/kaize/valle) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/kaize/valle)
|
2
2
|
|
3
|
-
Valle
|
4
|
-
so you
|
3
|
+
Valle automatically sets minimum and maximum values for the fields of your
|
4
|
+
ActiveRecord model(s), so you shouldn't worry, that string length or ID
|
5
|
+
value will exceed the permissible DB limit for this type of field.
|
5
6
|
|
6
7
|
For example, maximum length of the string in PostgreSQL is 255. We will
|
7
8
|
setup the following validator for you, so you don't need to write it by
|
@@ -16,8 +17,12 @@ Example:
|
|
16
17
|
PG::Error: ERROR: value "2147483648" is out of range for type integer
|
17
18
|
: SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
### Supported ActiveRecord field types
|
21
|
+
|
22
|
+
- `:primary_key`
|
23
|
+
- `:integer`
|
24
|
+
- `:string`
|
25
|
+
- `:text`
|
21
26
|
|
22
27
|
## Installation
|
23
28
|
|
@@ -50,6 +55,11 @@ Also, you should be able to turn it off temporary by setting `enabled` option to
|
|
50
55
|
config.enabled = false
|
51
56
|
end
|
52
57
|
|
58
|
+
## Alternatives
|
59
|
+
|
60
|
+
There is a similar gem, called [validates_lengths_from_database](http://github.com/rubiety/validates_lengths_from_database). It solves only one part -
|
61
|
+
applicable to strings. This gem is designed to work with all possible field types.
|
62
|
+
|
53
63
|
## Contributing
|
54
64
|
|
55
65
|
1. Fork it
|
@@ -0,0 +1,79 @@
|
|
1
|
+
@disable-bundler
|
2
|
+
Feature:
|
3
|
+
In order to temporary disable Valle or apply it only to a subset of models,
|
4
|
+
as a user of Rails, I would like to use valle initializer options.
|
5
|
+
|
6
|
+
Background:
|
7
|
+
Given I successfully run `bundle exec rails new testapp --skip-bundle --skip-sprockets --skip-javascript`
|
8
|
+
And I cd to "testapp"
|
9
|
+
And I add "factory_girl_rails" as a dependency
|
10
|
+
And I add "valle" from this project as a dependency
|
11
|
+
And I successfully run `bundle install`
|
12
|
+
And I successfully run `bundle exec rails g model User name:string`
|
13
|
+
And I successfully run `bundle exec rake db:migrate --trace`
|
14
|
+
|
15
|
+
Scenario: Setting enabled option to false disable Valle
|
16
|
+
When I write to "config/initializers/valle.rb" with:
|
17
|
+
"""
|
18
|
+
Valle.configure do |config|
|
19
|
+
config.enabled = false
|
20
|
+
end
|
21
|
+
"""
|
22
|
+
And I write to "test/unit/user_test.rb" with:
|
23
|
+
"""
|
24
|
+
require 'test_helper'
|
25
|
+
|
26
|
+
class UserTest < ActiveSupport::TestCase
|
27
|
+
test "should raise ActiveRecord::StatementInvalid error when name is too long" do
|
28
|
+
user = FactoryGirl.create(:user)
|
29
|
+
user.name = 'a' * 256
|
30
|
+
|
31
|
+
# SQLLite3 do not throw an ActiveRecord::StatementInvalid error
|
32
|
+
# so just check that record gets saved
|
33
|
+
assert user.save
|
34
|
+
end
|
35
|
+
end
|
36
|
+
"""
|
37
|
+
When I successfully run `bundle exec rake test --trace`
|
38
|
+
Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
|
39
|
+
|
40
|
+
Scenario: Using the models option should limit the effect of the Valle to the models specified in the list
|
41
|
+
When I successfully run `bundle exec rails g model Post title:string`
|
42
|
+
And I successfully run `bundle exec rake db:migrate --trace`
|
43
|
+
And I write to "config/initializers/valle.rb" with:
|
44
|
+
"""
|
45
|
+
Valle.configure do |config|
|
46
|
+
config.models = %w(Post)
|
47
|
+
end
|
48
|
+
"""
|
49
|
+
And I write to "test/unit/user_test.rb" with:
|
50
|
+
"""
|
51
|
+
require 'test_helper'
|
52
|
+
|
53
|
+
class UserTest < ActiveSupport::TestCase
|
54
|
+
test "should raise ActiveRecord::StatementInvalid error when name is too long" do
|
55
|
+
user = FactoryGirl.create(:user)
|
56
|
+
user.name = 'a' * 256
|
57
|
+
|
58
|
+
# SQLLite3 do not throw an ActiveRecord::StatementInvalid error
|
59
|
+
# so just check that record gets saved
|
60
|
+
assert user.save
|
61
|
+
end
|
62
|
+
end
|
63
|
+
"""
|
64
|
+
And I write to "test/unit/post_test.rb" with:
|
65
|
+
"""
|
66
|
+
require 'test_helper'
|
67
|
+
|
68
|
+
class PostTest < ActiveSupport::TestCase
|
69
|
+
test "should not save post when title is too long" do
|
70
|
+
post = FactoryGirl.create(:post)
|
71
|
+
post.title = 'a' * 256
|
72
|
+
|
73
|
+
assert !post.save
|
74
|
+
assert_equal 1, post.errors.count
|
75
|
+
end
|
76
|
+
end
|
77
|
+
"""
|
78
|
+
When I successfully run `bundle exec rake test --trace`
|
79
|
+
Then the output should contain "2 tests, 3 assertions, 0 failures, 0 errors"
|
@@ -0,0 +1,28 @@
|
|
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 --trace`
|
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
|
+
test "should not save user when name is too long" do
|
19
|
+
user = FactoryGirl.create(:user)
|
20
|
+
user.name = 'a' * 256
|
21
|
+
|
22
|
+
assert !user.save
|
23
|
+
assert_equal 1, user.errors.count
|
24
|
+
end
|
25
|
+
end
|
26
|
+
"""
|
27
|
+
When I successfully run `bundle exec rake test --trace`
|
28
|
+
Then the output should contain "1 tests, 2 assertions, 0 failures, 0 errors"
|
data/lib/valle.rb
CHANGED
@@ -4,7 +4,7 @@ module Valle
|
|
4
4
|
extend Configuration
|
5
5
|
|
6
6
|
# core
|
7
|
-
autoload :
|
7
|
+
autoload :Manager, 'valle/manager'
|
8
8
|
autoload :ValidationSetter, 'valle/validation_setter'
|
9
9
|
|
10
10
|
# extensions
|
@@ -18,6 +18,32 @@ module Valle
|
|
18
18
|
|
19
19
|
# hooks
|
20
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 turned gem on only for certain models,
|
37
|
+
# we need to check whether the model is in 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[:models].nil? ||
|
44
|
+
options[:models].is_a?(Array) && options[:models].include?(model_name)
|
45
|
+
end
|
46
|
+
end
|
21
47
|
end
|
22
48
|
|
23
49
|
## if not using Railtie, call `Valle::Hooks.init` directly
|
@@ -19,15 +19,12 @@ module Valle
|
|
19
19
|
@original_column = original_column
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
# Proxy all methods missing to original column
|
24
|
-
#
|
25
|
-
def method_missing(method_name, *arguments, &block)
|
22
|
+
def method_missing(method_name, *arguments, &block) # :nodoc:
|
26
23
|
@original_column.send(method_name, *arguments, &block)
|
27
24
|
end
|
28
25
|
|
29
|
-
def respond_to_method_missing?(method_name, include_private = false)
|
30
|
-
@original_column.respond_to?(method_name)
|
26
|
+
def respond_to_method_missing?(method_name, include_private = false) # :nodoc:
|
27
|
+
@original_column.respond_to?(method_name, include_private)
|
31
28
|
end
|
32
29
|
|
33
30
|
##
|
data/lib/valle/hooks.rb
CHANGED
@@ -8,7 +8,7 @@ module Valle
|
|
8
8
|
#
|
9
9
|
def init
|
10
10
|
ActiveSupport.on_load(:active_record) do
|
11
|
-
Valle::Hooks.extend_inherited_method
|
11
|
+
Valle::Hooks.extend_inherited_method if Valle.enabled?
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -20,7 +20,9 @@ module Valle
|
|
20
20
|
class << self
|
21
21
|
def inherited_with_valle_validators(subclass)
|
22
22
|
inherited_without_valle_validators(subclass)
|
23
|
-
Valle
|
23
|
+
if Valle.can_process_model?(subclass.model_name)
|
24
|
+
Valle::Hooks.extend_ar_validations_valid_method(subclass)
|
25
|
+
end
|
24
26
|
end
|
25
27
|
|
26
28
|
alias_method_chain :inherited, :valle_validators
|
@@ -41,7 +43,7 @@ module Valle
|
|
41
43
|
|
42
44
|
subclass.class_eval do
|
43
45
|
def valid_with_valle_validators?(context = nil)
|
44
|
-
self.class.valle_validators ||= Valle::
|
46
|
+
self.class.valle_validators ||= Valle::Manager.add_validators(self.class)
|
45
47
|
valid_without_valle_validators?(context)
|
46
48
|
end
|
47
49
|
|
data/lib/valle/version.rb
CHANGED
data/valle.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/valle/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Anton Kalyaev"]
|
6
6
|
gem.email = ["anton.kalyaev@gmail.com"]
|
7
|
-
gem.description = %q{Valle
|
8
|
-
gem.summary = %q{
|
7
|
+
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.}
|
8
|
+
gem.summary = %q{Built-in limit validations for your ActiveRecord model.}
|
9
9
|
gem.homepage = "http://github.com/kaize/valle"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -43,8 +43,9 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
-
description: Valle
|
47
|
-
so you
|
46
|
+
description: Valle automatically sets minimum and maximum values for the fields of
|
47
|
+
your ActiveRecord model(s), so you shouldn't worry, that string length or ID value
|
48
|
+
will exceed the permissible limit.
|
48
49
|
email:
|
49
50
|
- anton.kalyaev@gmail.com
|
50
51
|
executables: []
|
@@ -59,7 +60,8 @@ files:
|
|
59
60
|
- LICENSE
|
60
61
|
- README.md
|
61
62
|
- Rakefile
|
62
|
-
- features/
|
63
|
+
- features/configuration.feature
|
64
|
+
- features/sets_validations.feature
|
63
65
|
- features/step_definitions/rails_steps.rb
|
64
66
|
- features/support/env.rb
|
65
67
|
- lib/valle.rb
|
@@ -68,9 +70,9 @@ files:
|
|
68
70
|
- lib/valle/abstract_adapter/character_limited_column.rb
|
69
71
|
- lib/valle/abstract_adapter/column_wrapper.rb
|
70
72
|
- lib/valle/abstract_adapter/unlimited_column.rb
|
71
|
-
- lib/valle/bounds_manager.rb
|
72
73
|
- lib/valle/configuration.rb
|
73
74
|
- lib/valle/hooks.rb
|
75
|
+
- lib/valle/manager.rb
|
74
76
|
- lib/valle/railtie.rb
|
75
77
|
- lib/valle/validation_setter.rb
|
76
78
|
- lib/valle/version.rb
|
@@ -92,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
segments:
|
94
96
|
- 0
|
95
|
-
hash:
|
97
|
+
hash: 1309560054681721189
|
96
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
99
|
none: false
|
98
100
|
requirements:
|
@@ -101,16 +103,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
103
|
version: '0'
|
102
104
|
segments:
|
103
105
|
- 0
|
104
|
-
hash:
|
106
|
+
hash: 1309560054681721189
|
105
107
|
requirements: []
|
106
108
|
rubyforge_project:
|
107
109
|
rubygems_version: 1.8.24
|
108
110
|
signing_key:
|
109
111
|
specification_version: 3
|
110
|
-
summary:
|
111
|
-
fields
|
112
|
+
summary: Built-in limit validations for your ActiveRecord model.
|
112
113
|
test_files:
|
113
|
-
- features/
|
114
|
+
- features/configuration.feature
|
115
|
+
- features/sets_validations.feature
|
114
116
|
- features/step_definitions/rails_steps.rb
|
115
117
|
- features/support/env.rb
|
116
118
|
- test/lib/abstract_adapter/character_limited_column_test.rb
|
@@ -1,52 +0,0 @@
|
|
1
|
-
Feature: adds validators
|
2
|
-
Background:
|
3
|
-
When I successfully run `bundle exec rails new testapp`
|
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
|
-
When I successfully run `bundle install`
|
8
|
-
And I write to "db/migrate/1_create_users.rb" with:
|
9
|
-
"""
|
10
|
-
class CreateUsers < ActiveRecord::Migration
|
11
|
-
def self.up
|
12
|
-
create_table :users do |t|
|
13
|
-
t.string :name
|
14
|
-
t.text :bio
|
15
|
-
t.integer :age
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
"""
|
20
|
-
When I successfully run `bundle exec rake db:migrate --trace`
|
21
|
-
And I write to "app/models/user.rb" with:
|
22
|
-
"""
|
23
|
-
class User < ActiveRecord::Base
|
24
|
-
end
|
25
|
-
"""
|
26
|
-
|
27
|
-
@disable-bundler
|
28
|
-
Scenario: generate a rails 3 application and try out automatically injected validations
|
29
|
-
When I write to "test/factories.rb" with:
|
30
|
-
"""
|
31
|
-
FactoryGirl.define do
|
32
|
-
factory :user do
|
33
|
-
name "John"
|
34
|
-
bio "A nice write up about this guy"
|
35
|
-
age 22
|
36
|
-
end
|
37
|
-
end
|
38
|
-
"""
|
39
|
-
When I write to "test/unit/user_test.rb" with:
|
40
|
-
"""
|
41
|
-
require 'test_helper'
|
42
|
-
|
43
|
-
class UserTest < ActiveSupport::TestCase
|
44
|
-
test "should not save user when name is too long" do
|
45
|
-
user = FactoryGirl.create(:user)
|
46
|
-
user.name = 'a' * 256
|
47
|
-
assert !user.save
|
48
|
-
end
|
49
|
-
end
|
50
|
-
"""
|
51
|
-
When I successfully run `bundle exec rake test --trace`
|
52
|
-
Then the output should contain "1 tests, 1 assertions, 0 failures, 0 errors"
|