active_model-attributes_validation 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ee406c2309685f9046c58c1104b7af4a02d95dc
4
+ data.tar.gz: a332ca64f12db8d7e53646930cee3d55fcf51c05
5
+ SHA512:
6
+ metadata.gz: 993074a0cd9736f296c29669b14c99d2fbb93a495af6842ea108348a21846e6df77246b65245cbcfc6783a43c92ab1c662bdefedfa7b00128df011110b0279c2
7
+ data.tar.gz: ddcf3b9297a74c3f1563f40208661c8938ba0274ed361279a5638869aab6815c989fd145203672d7bd004ad3479104ebc8f5ebe4a53b4a365682ead5ecc288e2
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model-attributes_validation.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 dm1try
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.
@@ -0,0 +1,64 @@
1
+ # ActiveModel::AttributesValidation
2
+
3
+ Add `attributes_valid?` method in your model to validate a specific attribute/attributes. Can be easily used with ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_model-attributes_validation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_model-attributes_validation
18
+
19
+ ## Usage
20
+
21
+ You can include it:
22
+ ```ruby
23
+ class Model
24
+ include ActiveModel::Model
25
+ include ActiveModel::AttributesValidation
26
+ end
27
+ ```
28
+ For ActiveRecord it will be loaded automatically.
29
+
30
+ Example:
31
+ ```ruby
32
+ class Person
33
+ include ActiveModel::Model
34
+ include ActiveModel::AttributesValidation
35
+
36
+ attr_accessor :name, :age
37
+ validates_presence_of :name
38
+ validates_presence_of :age
39
+ end
40
+
41
+ person = Person.new
42
+ person.attributes_valid?(:name) # => false
43
+ person.errors.messages # => {:name=>["can't be blank"]}
44
+
45
+ person.attributes_valid?(:name, :age) # => false
46
+ person.errors.messages # => {:name=>["can't be blank"], :age=>["can't be blank"]}
47
+
48
+ # standard full validation
49
+ person.valid? # => false
50
+ person.errors.messages # => {:name=>["can't be blank"], :age=>["can't be blank"]}
51
+
52
+ person.name = 'bill'
53
+ person.attributes_valid?(:name) # => true
54
+ person.errors.messages # => {}
55
+ ```
56
+ See specs for more information.
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'active_model/attributes_validation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model-attributes_validation"
8
+ spec.version = ActiveModel::AttributesValidation::VERSION
9
+ spec.authors = ["dm1try"]
10
+ spec.email = ["me@dmitry.it"]
11
+ spec.description = %q{Validates a specific attribute on ActiveModel, ActiveRecord}
12
+ spec.summary = %q{Validates a specific attribute on ActiveModel, ActiveRecord}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activemodel", ">= 3.2"
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
+ end
@@ -0,0 +1,38 @@
1
+ require "active_model"
2
+ require "active_model/attributes_validation/version"
3
+
4
+ module ActiveModel
5
+ module AttributesValidation
6
+
7
+ def attributes_valid?(*attrs)
8
+ errors.clear
9
+
10
+ attr_validators = self.class.validators_on(*attrs).uniq
11
+ specific_validators = attr_validators.collect do |validator|
12
+ validator = validator.dup
13
+ validated_attributes = validator.attributes & attrs
14
+ validator.instance_variable_set(:@attributes, validated_attributes)
15
+ validator
16
+ end
17
+
18
+ self_validation = ->{ specific_validators.each { |validator| validator.validate(self) } }
19
+
20
+ if self.class.ancestors.include?(ActiveModel::Validations::Callbacks)
21
+ run_callbacks(:validation) { self_validation.call }
22
+ else
23
+ self_validation.call
24
+ end
25
+
26
+ errors.empty?
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ if defined? ActiveRecord
33
+ module ActiveRecord
34
+ class Base
35
+ include ActiveModel::AttributesValidation
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module AttributesValidation
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe "attributes validation" do
4
+
5
+ before :all do
6
+ class Person
7
+ include ActiveModel::Model
8
+
9
+ attr_accessor :name, :age
10
+ validates_presence_of :name
11
+ validates_presence_of :age
12
+ end
13
+ end
14
+ subject(:person){ Person.new }
15
+
16
+ context "extended model" do
17
+ before do
18
+ class Person
19
+ include ActiveModel::AttributesValidation
20
+ end
21
+ end
22
+
23
+ it { should respond_to :attributes_valid? }
24
+ end
25
+
26
+ context "#attributes_valid?" do
27
+ context "for a specific attribute" do
28
+ it 'returns false if validation only this attribute fails' do
29
+ person.name = nil
30
+ expect(person.attributes_valid? :name).to be_false
31
+ end
32
+
33
+ it 'returns true if only this attribute valid' do
34
+ person.name = 'name'
35
+ person.age = nil
36
+ expect(person.attributes_valid? :name).to be_true
37
+ end
38
+ end
39
+ context 'errors' do
40
+ it 'fills errors only for this attribute' do
41
+ person.name = person.age = nil
42
+ person.attributes_valid? :name
43
+
44
+ expect(person.errors.size).to eq 1
45
+ expect(person.errors[:name]).to eq ["can't be blank"]
46
+ expect(person.errors[:age]).to be_empty
47
+ end
48
+
49
+ it 'clears errors between calls' do
50
+ person.name = nil
51
+ person.attributes_valid? :name
52
+
53
+ expect(person.errors.size).to eq 1
54
+
55
+ person.name = 'name'
56
+ person.attributes_valid? :name
57
+ expect(person.errors.size).to eq 0
58
+ end
59
+
60
+ it 'return same errors as #valid? if all attributes present in params' do
61
+ person.name = person.age = nil
62
+ person.valid?
63
+ standard_errors = person.errors.dup
64
+
65
+ person.attributes_valid? :name, :age
66
+ expect(standard_errors.messages).to eq person.errors.messages
67
+ end
68
+ end
69
+
70
+ context 'validation callbacks' do
71
+ it 'do not if they not present' do
72
+ person.stub(:run_callbacks)
73
+ person.attributes_valid? :name
74
+ expect(person).not_to have_received(:run_callbacks)
75
+ end
76
+
77
+ it 'run if they present' do
78
+ class Person
79
+ include ActiveModel::Validations::Callbacks
80
+ end
81
+ person.stub(:run_callbacks)
82
+ person.attributes_valid? :name
83
+ expect(person).to have_received(:run_callbacks)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'active_model/attributes_validation'
5
+
6
+ require 'rubygems'
7
+ require 'bundler'
8
+ Bundler.setup :default, :test
9
+ I18n.enforce_available_locales = false
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model-attributes_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - dm1try
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-05 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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
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: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ description: Validates a specific attribute on ActiveModel, ActiveRecord
70
+ email:
71
+ - me@dmitry.it
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - active_model-attributes_validation.gemspec
82
+ - lib/active_model/attributes_validation.rb
83
+ - lib/active_model/attributes_validation/version.rb
84
+ - spec/attributes_validation_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.1.11
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Validates a specific attribute on ActiveModel, ActiveRecord
110
+ test_files:
111
+ - spec/attributes_validation_spec.rb
112
+ - spec/spec_helper.rb
113
+ has_rdoc: