warn_if 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/validators/warning_validator.rb +19 -0
- data/lib/warn_if.rb +5 -0
- data/lib/warn_if/model_warning.rb +16 -0
- data/lib/warn_if/model_warnings.rb +29 -0
- data/lib/warn_if/version.rb +3 -0
- data/lib/warn_if/warn_if.rb +27 -0
- data/warn_if.gemspec +24 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ian Whitney
|
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,29 @@
|
|
1
|
+
# WarnIf2
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'warn_if2'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install warn_if2
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class WarningValidator < ActiveModel::Validator
|
2
|
+
def validate(record)
|
3
|
+
if warning_condition?(record)
|
4
|
+
record.warnings[:base] << ModelWarning.new(options[:label],options[:message],options[:severity],new_condition?(record))
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def warning_condition?(record)
|
9
|
+
if options[:condition].respond_to?(:call)
|
10
|
+
options[:condition].call(record)
|
11
|
+
else
|
12
|
+
record.method(options[:condition]).call
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def new_condition?(record)
|
17
|
+
options[:new_condition].call(record)
|
18
|
+
end
|
19
|
+
end
|
data/lib/warn_if.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class ModelWarning
|
2
|
+
attr_accessor :label, :message, :severity
|
3
|
+
def initialize(label, message, severity, new_warning=false)
|
4
|
+
self.label = label
|
5
|
+
self.message = message
|
6
|
+
self.severity = severity
|
7
|
+
self.new_warning = new_warning
|
8
|
+
end
|
9
|
+
|
10
|
+
def new?
|
11
|
+
new_warning
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
attr_accessor :new_warning
|
16
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class ModelWarnings < Hash
|
2
|
+
def initialize
|
3
|
+
super
|
4
|
+
self[:base] = []
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def all
|
9
|
+
values.flatten
|
10
|
+
end
|
11
|
+
|
12
|
+
def clear
|
13
|
+
# self.keys.each {|k| self[k] = []}
|
14
|
+
super
|
15
|
+
self[:base] = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def clear?
|
19
|
+
keys == [:base] && self[:base] == []
|
20
|
+
end
|
21
|
+
|
22
|
+
def new
|
23
|
+
all.select {|w| w.new?}
|
24
|
+
end
|
25
|
+
|
26
|
+
def with_severity(severity)
|
27
|
+
all.select {|w| w.severity == severity}
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module WarnIf
|
2
|
+
module ClassMethods
|
3
|
+
def warn_if(options={})
|
4
|
+
validates_with WarningValidator, options
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
def warnings
|
10
|
+
@warnings ||= ModelWarnings.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def warned?
|
14
|
+
!warnings.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?(context = nil)
|
18
|
+
warnings.clear
|
19
|
+
super(context)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.included(receiver)
|
24
|
+
receiver.extend ClassMethods
|
25
|
+
receiver.send :include, InstanceMethods
|
26
|
+
end
|
27
|
+
end
|
data/warn_if.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'warn_if/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "warn_if"
|
8
|
+
spec.version = WarnIf::VERSION
|
9
|
+
spec.authors = ["Ian Whitney","Davin Lagerroos"]
|
10
|
+
spec.email = ["iwhitney@ssa-i.org","dlagerroos@ssa-i.org"]
|
11
|
+
spec.description = %q{Provides warnings to ActiveRecord validiations.}
|
12
|
+
spec.summary = %q{Provides warnings to ActiveRecord validiations. Warnings can be viewed but do not prevent saving.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
24
|
+
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: warn_if
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ian Whitney
|
9
|
+
- Davin Lagerroos
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-03-27 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.3'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.3'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rake
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
description: Provides warnings to ActiveRecord validiations.
|
48
|
+
email:
|
49
|
+
- iwhitney@ssa-i.org
|
50
|
+
- dlagerroos@ssa-i.org
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/validators/warning_validator.rb
|
61
|
+
- lib/warn_if.rb
|
62
|
+
- lib/warn_if/model_warning.rb
|
63
|
+
- lib/warn_if/model_warnings.rb
|
64
|
+
- lib/warn_if/version.rb
|
65
|
+
- lib/warn_if/warn_if.rb
|
66
|
+
- warn_if.gemspec
|
67
|
+
homepage: ''
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.24
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Provides warnings to ActiveRecord validiations. Warnings can be viewed but
|
92
|
+
do not prevent saving.
|
93
|
+
test_files: []
|