presence_at_least_one_validator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ .bundle
2
+ Gemfile.lock
3
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in presence_at_least_one_validator.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 nohup brasil
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # presence_at_least_one_validator
2
+
3
+ Validates presence of at least one attribute
4
+
5
+ ## Installation
6
+
7
+ gem "presence_at_least_one_validator"
8
+
9
+ ## Usage
10
+
11
+ class ExpenseReport < ActiveRecord::Base
12
+ validates :company, :service_provider, :presence_at_least_one => true
13
+ end
14
+
15
+ ## License
16
+
17
+ Copyright (c) 2012 nohup brasil, released under the MIT license
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ end
@@ -0,0 +1,29 @@
1
+ class PresenceAtLeastOneValidator < ActiveModel::EachValidator
2
+ # Validates that AT LEAST ONE of the specified attributes are not blank (as defined by Object#blank?).
3
+ #
4
+ # ==== Examples
5
+ #
6
+ # class Product < ActiveRecord::Base
7
+ # validates :name, :price, :presence_at_least_one => true
8
+ # end
9
+ #
10
+ # >> product = Product.new
11
+ # >> product.valid?
12
+ # => false
13
+ # >> [product.errors[:name], :product.errors[:price]]
14
+ # => ['presence_at_least_one translate message error', 'presence_at_least_one translate message error']
15
+ #
16
+ # >> product.name = 'Donate a Macbook to Tomas!'
17
+ # >> product.valid?
18
+ # => true
19
+ #
20
+ def validate(record)
21
+ if all_specified_attributes_are_blank?(record)
22
+ attributes.each { |attribute| record.errors.add(attribute, :presence_at_least_one) }
23
+ end
24
+ end
25
+
26
+ def all_specified_attributes_are_blank?(record)
27
+ attributes.all? { |attribute| record.send(attribute).blank? }
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "presence_at_least_one_validator"
4
+ s.version = "0.1.0"
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["Tomás D'Stefano", "Gabriel Sobrinho"]
7
+ s.email = ["tomas_stefano@successoft.com", "gabriel.sobrinho@gmail.com"]
8
+ s.homepage = "https://github.com/nohupbrasil/presence_at_least_one_validator"
9
+ s.summary = %q{Presence at least one validation for ActiveModel}
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_dependency 'activemodel', ">= 3.0"
17
+ s.add_development_dependency 'rake', '>= 0.8.7'
18
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class PresenceAtLeastOneValidatorTest < Test::Unit::TestCase
4
+ def test_none_attributes
5
+ assert expense_report.invalid?
6
+ end
7
+
8
+ def test_one_attribute
9
+ assert expense_report(:customer => 'nohup').valid?
10
+ assert expense_report(:service_provider => 'nohup').valid?
11
+ end
12
+
13
+ def test_all_attributes
14
+ assert expense_report(:customer => 'nohup', :service_provider => 'nohup').valid?
15
+ end
16
+
17
+ protected
18
+
19
+ def expense_report(attributes = {})
20
+ ExpenseReport.new(attributes)
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_model'
4
+ require 'presence_at_least_one_validator'
5
+ require 'ostruct'
6
+
7
+ class ExpenseReport < OpenStruct
8
+ include ActiveModel::Validations
9
+
10
+ validates :customer, :service_provider, :presence_at_least_one => true
11
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: presence_at_least_one_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tomás D'Stefano
9
+ - Gabriel Sobrinho
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activemodel
17
+ requirement: &70289045679980 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70289045679980
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70289045679500 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.7
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70289045679500
37
+ description:
38
+ email:
39
+ - tomas_stefano@successoft.com
40
+ - gabriel.sobrinho@gmail.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - MIT-LICENSE
48
+ - README.markdown
49
+ - Rakefile
50
+ - lib/presence_at_least_one_validator.rb
51
+ - presence_at_least_one_validator.gemspec
52
+ - test/presence_at_least_one_validator_test.rb
53
+ - test/test_helper.rb
54
+ homepage: https://github.com/nohupbrasil/presence_at_least_one_validator
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.11
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Presence at least one validation for ActiveModel
78
+ test_files:
79
+ - test/presence_at_least_one_validator_test.rb
80
+ - test/test_helper.rb