validation_skipper 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +1 -0
- data/lib/validation_skipper/version.rb +3 -0
- data/lib/validation_skipper.rb +28 -0
- data/validation_skipper.gemspec +19 -0
- metadata +54 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nathan Pearson
|
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,77 @@
|
|
1
|
+
# ValidationSkipper
|
2
|
+
|
3
|
+
Every once in a while, it may be necessary to skip some validations.
|
4
|
+
|
5
|
+
For example, you may have a user model that validates name, email, password, etc., but you may want to present a view where the end-user can update his/her name in one screen, but change the password on another screen.
|
6
|
+
|
7
|
+
If the model is validating across all the attributes, you'll need a way around this.
|
8
|
+
|
9
|
+
This gem will help you easily declare which attributes to skip and when to skip them.
|
10
|
+
|
11
|
+
**WARNING:** Skipping validations is not considered best practice (albeit sometimes it is necessary), so skip them at your own risk!
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'validation_skipper'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install validation_skipper
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### In your model:
|
30
|
+
|
31
|
+
Let's say you have a User model with a name, phone, and password.
|
32
|
+
|
33
|
+
##### In your code, you might have validations like these:
|
34
|
+
|
35
|
+
validates :name, :presence => true
|
36
|
+
validates :phone, :presence => true
|
37
|
+
validates :password, :presence => true
|
38
|
+
|
39
|
+
##### Step 1: Declare which validations are allowed to be skipped:
|
40
|
+
|
41
|
+
can_skip_validation_for :name, :phone, :password
|
42
|
+
|
43
|
+
(Note: this does not automatically skip them, but makes them eligible for skipping)
|
44
|
+
|
45
|
+
|
46
|
+
##### Step 2: Change the validations to look like these:
|
47
|
+
|
48
|
+
validates :name, :presence => true, :unless => skip_name_validation?
|
49
|
+
validates :phone, :presence => true, :unless => skip_phone_validation?
|
50
|
+
validates :password, :presence => true, :unless => skip_pasword_validation?
|
51
|
+
|
52
|
+
(Basically just add `:unless => skip_<name of your attribute>_validation?` to each of your validation methods)
|
53
|
+
|
54
|
+
### In your controller:
|
55
|
+
|
56
|
+
Now just pass the names of the fields you want to skip validation for from within your controller, ex:
|
57
|
+
|
58
|
+
def create
|
59
|
+
@user = User.new(params[:user])
|
60
|
+
@user.skip_validation_for :name, :phone
|
61
|
+
Etc...
|
62
|
+
end
|
63
|
+
|
64
|
+
This will now skip the validations for name and phone only, but still validate for password.
|
65
|
+
|
66
|
+
And there you go, anytime you want the same effect elsewhere, just follow this approach.
|
67
|
+
|
68
|
+
Enjoy!
|
69
|
+
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "validation_skipper/version"
|
2
|
+
|
3
|
+
module ValidationSkipper # Creates "attr_accessor" methods and related boolean checker method.
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def can_skip_validation_for(*args)
|
9
|
+
args.each do |attr|
|
10
|
+
send(:attr_accessor, "skip_#{attr}_validation")
|
11
|
+
send(:define_method, "skip_#{attr}_validation?") { send("skip_#{attr}_validation") }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Hook to include ClassMethods as... class methods.
|
17
|
+
def self.included(base)
|
18
|
+
base.extend ClassMethods
|
19
|
+
end
|
20
|
+
|
21
|
+
# Defines a "skip_validation_for" method to use in controllers.
|
22
|
+
def skip_validation_for(*args)
|
23
|
+
args.each { |attr| send("skip_#{attr}_validation=", true) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# include the extension
|
28
|
+
ActiveRecord::Base.send(:include, ValidationSkipper)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validation_skipper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "validation_skipper"
|
8
|
+
gem.version = ValidationSkipper::VERSION
|
9
|
+
gem.authors = ["Nathan Pearson"]
|
10
|
+
gem.email = ["npearson72@gmail.com"]
|
11
|
+
gem.description = %q{If you need a way to easily skip validations on some attributes and only in certain cases, this gem will allow you to do it in a clean and elegant way.}
|
12
|
+
gem.summary = %q{Easily skip validations in Rails}
|
13
|
+
gem.homepage = "https://github.com/npearson72/validation_skipper"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation_skipper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nathan Pearson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: If you need a way to easily skip validations on some attributes and only
|
15
|
+
in certain cases, this gem will allow you to do it in a clean and elegant way.
|
16
|
+
email:
|
17
|
+
- npearson72@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/validation_skipper.rb
|
28
|
+
- lib/validation_skipper/version.rb
|
29
|
+
- validation_skipper.gemspec
|
30
|
+
homepage: https://github.com/npearson72/validation_skipper
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Easily skip validations in Rails
|
54
|
+
test_files: []
|