prevent_blankification_validator 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/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/lib/active_model/validations/prevent_blankification_validator.rb +35 -0
- data/lib/prevent_blankification_validator.rb +9 -0
- data/lib/prevent_blankification_validator/version.rb +3 -0
- data/locales/de.yml +4 -0
- data/locales/en.yml +4 -0
- data/prevent_blankification_validator.gemspec +27 -0
- data/spec/prevent_blankification_validator_spec.rb +49 -0
- data/spec/spec_helper.rb +24 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a1c71eeffc12d50966e67010d6558a9f6d113b3
|
4
|
+
data.tar.gz: ba6937486917c4dfb09611d7327369087a828316
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19c657f5ba95ae8020c2dd9a17b5d526117d1dd4f4448a10888c4fea6b797ea5e2aa7e539ca786795ff3270046b9c2affbfb8aba41e72bb22c8360427594af8f
|
7
|
+
data.tar.gz: 4cbdf5a2a05ab2719d0808244253d4fc63893c9184d09902e213ed8595ba0491b87f16ea8c5fdbd872da1d338eb17952d03dc6a2826f8c56bcbc97e52d8975a3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
prevent_blankification
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Gebhard Wöstemeyer
|
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/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# PreventBlankificationValidator
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/gewo/prevent_blankification_validator.png)](https://travis-ci.org/gewo/prevent_blankification_validator/)
|
4
|
+
|
5
|
+
Rails 3 Validator, that prevents attributes from being changed to a blank value
|
6
|
+
once they're present.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'prevent_blankification_validator'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install prevent_blankification_validator
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class MyModel
|
26
|
+
validates :value, prevent_blankification: true
|
27
|
+
end
|
28
|
+
|
29
|
+
model.value = 'present'
|
30
|
+
model.save # => true
|
31
|
+
|
32
|
+
model.value = ''
|
33
|
+
model.valid? # => false
|
34
|
+
|
35
|
+
model.value = "\r \n "
|
36
|
+
model.valid? # => false
|
37
|
+
|
38
|
+
model.value = nil
|
39
|
+
model.valid? # => false
|
40
|
+
|
41
|
+
model.value = 'present as well'
|
42
|
+
model.valid? # => true
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
|
6
|
+
# This validator prevents fields that were previously set to be blanked.
|
7
|
+
# Thus, we can allow blank fields, but once a field is set the value cannot
|
8
|
+
# be deleted again.
|
9
|
+
#
|
10
|
+
# The record#attribute _must_ have dirty tracking enabled. With Mongoid all
|
11
|
+
# Documents get that by default.
|
12
|
+
#
|
13
|
+
# Usage: validates :field, prevent_blankification: true
|
14
|
+
class PreventBlankificationValidator < ActiveModel::EachValidator
|
15
|
+
def validate_each(record, attribute, value)
|
16
|
+
must_have_dirty_tracking_enabled!(record, attribute)
|
17
|
+
|
18
|
+
dirty_value = record.send "#{attribute}_was"
|
19
|
+
|
20
|
+
if value.blank? && dirty_value.present?
|
21
|
+
record.errors.add(attribute, :cannot_be_blanked)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def must_have_dirty_tracking_enabled!(record, attribute)
|
28
|
+
unless record.respond_to? "#{attribute}_was"
|
29
|
+
raise "#{record.class.name} doesn't seem to have dirty tracking " +
|
30
|
+
"enabled for #{attribute}!"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'active_support/i18n'
|
2
|
+
require "prevent_blankification_validator/version"
|
3
|
+
require 'active_model/validations/prevent_blankification_validator'
|
4
|
+
|
5
|
+
module PreventBlankificationValidator
|
6
|
+
end
|
7
|
+
|
8
|
+
I18n.load_path += Dir[File.expand_path(File.join(File.dirname(__FILE__),
|
9
|
+
'../locales', '*.yml')).to_s]
|
data/locales/de.yml
ADDED
data/locales/en.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'prevent_blankification_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "prevent_blankification_validator"
|
8
|
+
s.version = PreventBlankificationValidator::VERSION
|
9
|
+
s.authors = ["Gebhard Wöstemeyer"]
|
10
|
+
s.email = ["g.woestemeyer@gmail.com"]
|
11
|
+
s.description = %q{Rails 3 Validator, that prevents attributes from being changed to a blank value once they're present.}
|
12
|
+
s.summary = %q{Rails 3 Validator, that prevents attributes from being changed to a blank value once they're present.}
|
13
|
+
s.homepage = "https://github.com/gewo/prevent_blankification_validator/"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
s.test_files = s.files.grep(%r{^spec/})
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'activemodel', ['~> 3.0']
|
22
|
+
|
23
|
+
s.add_development_dependency 'activesupport', ['~> 3.0']
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
s.add_development_dependency 'rspec'
|
26
|
+
s.add_development_dependency 'simplecov'
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModel::Validations::PreventBlankificationValidator do
|
4
|
+
before :each do
|
5
|
+
@validator = ActiveModel::Validations::PreventBlankificationValidator.
|
6
|
+
new({ attributes: {} })
|
7
|
+
|
8
|
+
@mock = mock('model')
|
9
|
+
@mock.stub('errors').and_return []
|
10
|
+
@mock.errors.stub('[]').and_return {}
|
11
|
+
end
|
12
|
+
|
13
|
+
BLANKS = [nil, '', " \n "]
|
14
|
+
|
15
|
+
context "when attribute_was blank" do
|
16
|
+
BLANKS.each do |attribute_was|
|
17
|
+
it "passes validation when attribute_was == '#{attribute_was.inspect}'" do
|
18
|
+
@mock.stub('attribute_was').and_return attribute_was
|
19
|
+
@mock.should_not_receive :errors
|
20
|
+
@validator.validate_each(@mock, 'attribute', '')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when attribute_was !blank" do
|
26
|
+
before :each do
|
27
|
+
@mock.stub('attribute_was').and_return 'I was'
|
28
|
+
end
|
29
|
+
|
30
|
+
BLANKS.each do |new_value|
|
31
|
+
it "has errors when new_value == '#{new_value.inspect}'" do
|
32
|
+
@mock.errors.should_receive :add
|
33
|
+
@validator.validate_each(@mock, 'attribute', new_value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "passes validation when value is set to non-blank" do
|
38
|
+
@mock.should_not_receive :errors
|
39
|
+
@validator.validate_each(@mock, 'attribute', 'so I am')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "raises an error when dirty tracking is disabled" do
|
44
|
+
@mock.stub('respond_to?').with('attribute_was').and_return(false)
|
45
|
+
expect {
|
46
|
+
@validator.validate_each(@mock, 'attribute', 'so I am')
|
47
|
+
}.to raise_error(/have dirty tracking enabled/)
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
if RUBY_VERSION =~ /1\.9/ && RUBY_ENGINE == 'ruby'
|
2
|
+
require 'simplecov'
|
3
|
+
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter "/spec"
|
6
|
+
end
|
7
|
+
|
8
|
+
SimpleCov.at_exit do
|
9
|
+
File.open(File.join(SimpleCov.coverage_path, 'percent.txt'), 'w') do |f|
|
10
|
+
f.write SimpleCov.result.covered_percent
|
11
|
+
end
|
12
|
+
SimpleCov.result.format!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'active_model'
|
17
|
+
require 'prevent_blankification_validator'
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
21
|
+
config.run_all_when_everything_filtered = true
|
22
|
+
config.filter_run :focus
|
23
|
+
config.order = 'random'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prevent_blankification_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gebhard Wöstemeyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Rails 3 Validator, that prevents attributes from being changed to a blank
|
84
|
+
value once they're present.
|
85
|
+
email:
|
86
|
+
- g.woestemeyer@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .ruby-gemset
|
94
|
+
- .ruby-version
|
95
|
+
- .travis.yml
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- lib/active_model/validations/prevent_blankification_validator.rb
|
101
|
+
- lib/prevent_blankification_validator.rb
|
102
|
+
- lib/prevent_blankification_validator/version.rb
|
103
|
+
- locales/de.yml
|
104
|
+
- locales/en.yml
|
105
|
+
- prevent_blankification_validator.gemspec
|
106
|
+
- spec/prevent_blankification_validator_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
homepage: https://github.com/gewo/prevent_blankification_validator/
|
109
|
+
licenses: []
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.0.3
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Rails 3 Validator, that prevents attributes from being changed to a blank
|
131
|
+
value once they're present.
|
132
|
+
test_files:
|
133
|
+
- spec/prevent_blankification_validator_spec.rb
|
134
|
+
- spec/spec_helper.rb
|