active_model_extensions 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/active_model_extensions.gemspec +26 -0
- data/lib/active_model_extensions.rb +7 -0
- data/lib/active_model_extensions/instance_validatable.rb +19 -0
- data/lib/active_model_extensions/validation_alertable.rb +40 -0
- data/lib/active_model_extensions/version.rb +3 -0
- data/spec/instance_validatable_spec.rb +113 -0
- data/spec/validation_alertable_spec.rb +46 -0
- metadata +130 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Daniel Luxemburg
|
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
|
+
# ActiveModelExtensions
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_model_extensions'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_model_extensions
|
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,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_model_extensions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_model_extensions"
|
8
|
+
spec.version = ActiveModelExtensions::VERSION
|
9
|
+
spec.authors = ["Daniel Luxemburg"]
|
10
|
+
spec.email = ["daniel.luxemburg@gmail.com"]
|
11
|
+
spec.description = %q{Some Extension::Extensions for your ActiveModel::Models}
|
12
|
+
spec.summary = %q{InstanceValidatable, ValidationAlertable, ActiveStateful}
|
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_dependency "rails", "~> 4.0.0.beta1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveModelExtensions
|
2
|
+
module InstanceValidatable
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
validate :instance_validations
|
8
|
+
end
|
9
|
+
|
10
|
+
def instance_validators
|
11
|
+
@instance_validators ||= []
|
12
|
+
end
|
13
|
+
|
14
|
+
def instance_validations
|
15
|
+
validates_with *@instance_validators
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveModelExtensions
|
2
|
+
module ValidationAlertable
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
extend ActiveModel::Callbacks
|
6
|
+
|
7
|
+
included do
|
8
|
+
define_model_callbacks :run_validations
|
9
|
+
before_run_validations :clear_alerts
|
10
|
+
end
|
11
|
+
|
12
|
+
def alerts
|
13
|
+
@alerts ||= ActiveModel::Errors.new(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def has_alerts?
|
17
|
+
clear_alerts
|
18
|
+
valid?
|
19
|
+
alerts.count > 0
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def clear_alerts
|
25
|
+
alerts.clear
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class AlertingValidator < ActiveModel::Validator
|
31
|
+
def setup(klass)
|
32
|
+
unless klass.method_defined?(:alerts)
|
33
|
+
klass.send(:include,ValidationAlertable)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.require(:default,:development)
|
3
|
+
|
4
|
+
class MyModel
|
5
|
+
|
6
|
+
include ActiveModel::Model
|
7
|
+
|
8
|
+
attr_accessor :field
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
class MyInstanceValidatedModel < MyModel
|
13
|
+
|
14
|
+
include ActiveModelExtensions::InstanceValidatable
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class MyValidator < ActiveModel::Validator
|
19
|
+
|
20
|
+
def validate(record)
|
21
|
+
unless record.field == 27
|
22
|
+
record.errors.add(:field,'must not be blank')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "Active Model validation without extensions" do
|
29
|
+
|
30
|
+
context "when preformed extenally" do
|
31
|
+
|
32
|
+
it "fails to make the model invalid" do
|
33
|
+
m = MyModel.new
|
34
|
+
v = MyValidator.new
|
35
|
+
v.validate(m)
|
36
|
+
expect(m).to be_valid
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when added via singleton class" do
|
42
|
+
|
43
|
+
it "fails to make the model invalid" do
|
44
|
+
m = MyModel.new
|
45
|
+
|
46
|
+
class << m
|
47
|
+
validates_with MyValidator
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(m).to be_valid
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "when added via '#validates_with'" do
|
57
|
+
|
58
|
+
it "fails to make the model invalid" do
|
59
|
+
m = MyModel.new
|
60
|
+
|
61
|
+
m.validates_with MyValidator
|
62
|
+
|
63
|
+
expect(m).to be_valid
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when added by creating a new annonymous class" do
|
70
|
+
|
71
|
+
it "does make the model invalid" do
|
72
|
+
|
73
|
+
MyNewModel = Class.new(MyModel) do
|
74
|
+
validates_with MyValidator
|
75
|
+
end
|
76
|
+
|
77
|
+
m = MyNewModel.new
|
78
|
+
|
79
|
+
expect(m).to_not be_valid
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
describe ActiveModelExtensions::InstanceValidatable do
|
89
|
+
|
90
|
+
context "when used to add a validator" do
|
91
|
+
|
92
|
+
it "should make the model invalid" do
|
93
|
+
|
94
|
+
m = MyInstanceValidatedModel.new
|
95
|
+
|
96
|
+
m.instance_validators << MyValidator
|
97
|
+
|
98
|
+
expect(m).to_not be_valid
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
it "but only when added" do
|
103
|
+
|
104
|
+
m = MyInstanceValidatedModel.new
|
105
|
+
|
106
|
+
expect(m).to be_valid
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.require(:default,:development)
|
3
|
+
|
4
|
+
class MyAlertingValidator < ActiveModelExtensions::AlertingValidator
|
5
|
+
|
6
|
+
def validate(record)
|
7
|
+
unless record.field == 72
|
8
|
+
record.alerts.add(:field,"is #{record.field} instead of 72, watch yourself")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class MyAlertingModel < MyModel
|
15
|
+
|
16
|
+
validates_with MyAlertingValidator
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
describe ActiveModelExtensions::ValidationAlertable do
|
23
|
+
|
24
|
+
it "should add an alert when appropriate, but not make the model invalid" do
|
25
|
+
|
26
|
+
m = MyAlertingModel.new(field:71)
|
27
|
+
|
28
|
+
expect(m).to be_valid
|
29
|
+
expect(m).to have_alerts
|
30
|
+
expect(m.alerts.count).to eq(1)
|
31
|
+
expect(m.alerts.get(:field)).to eq(["is 71 instead of 72, watch yourself"])
|
32
|
+
expect(m.alerts.full_messages).to eq(["Field is 71 instead of 72, watch yourself"])
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not add an alert when validator doesn't need to" do
|
37
|
+
|
38
|
+
m = MyAlertingModel.new(field:72)
|
39
|
+
|
40
|
+
expect(m).to be_valid
|
41
|
+
expect(m).to_not have_alerts
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Luxemburg
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.0.beta1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.0.0.beta1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Some Extension::Extensions for your ActiveModel::Models
|
79
|
+
email:
|
80
|
+
- daniel.luxemburg@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- active_model_extensions.gemspec
|
91
|
+
- lib/active_model_extensions.rb
|
92
|
+
- lib/active_model_extensions/instance_validatable.rb
|
93
|
+
- lib/active_model_extensions/validation_alertable.rb
|
94
|
+
- lib/active_model_extensions/version.rb
|
95
|
+
- spec/instance_validatable_spec.rb
|
96
|
+
- spec/validation_alertable_spec.rb
|
97
|
+
homepage: ''
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: 1396170205535577088
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: 1396170205535577088
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.24
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: InstanceValidatable, ValidationAlertable, ActiveStateful
|
128
|
+
test_files:
|
129
|
+
- spec/instance_validatable_spec.rb
|
130
|
+
- spec/validation_alertable_spec.rb
|