validates_quantity 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9897f2038a83c90cd66743d4e08f8559c047f6fb
4
+ data.tar.gz: 4343283db1de10b72d84752be5d68d07d8fc27b6
5
+ SHA512:
6
+ metadata.gz: 469c9a79b86194ac591ace1f7c223bb515366ad3c5ce55561e1f348b1e1235383bafcd3fd26657351d6842d9e04c6fcf4f054f767c7f09a80227fa850224ffd8
7
+ data.tar.gz: 6b07c85db2046269f31d7c13305f4152c5e032847c06cbf0a27e304eb3ce06729296abf3083f8bf3c3ff51f2bffa7b4b7a979e16f6169011ed9bc39a4d11a82c
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validates_quantity.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 mathieumahe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mathieu Mahé
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,47 @@
1
+ # ValidatesQuantity
2
+
3
+ Add to Rails the possibility to validate the quantity of an associate model
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'validates_quantity'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install validates_quantity
18
+
19
+ ## Usage
20
+
21
+ Call `validates_quantity_of` in an ActiveRecord class and pass the name of the association to validate.
22
+
23
+
24
+
25
+ class Topic < ActiveRecord::Base
26
+ validates_quantity_of :tags, :less_than_or_equal_to => 5
27
+ end
28
+
29
+ Available keys are :
30
+
31
+ Keys | Operation
32
+ --------------------------------------|----------------
33
+ greater_than | >
34
+ greater_than_or_equal_to | >=
35
+ equal_to | ==
36
+ less_than | <
37
+ less_than_or_equal_to | <=
38
+ other_than | !=
39
+
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/mathieumahe/validates_quantity/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ greater_than: "must contain more than %{count} elements"
5
+ greater_than_or_equal_to: "must contain %{count} or more elements"
6
+ equal_to: "must contain %{count} elements"
7
+ less_than: "must contain less than %{count} elements"
8
+ less_than_or_equal_to: "must contain %{count} or less elements"
9
+ other_than: "must not contain %{count} elements"
10
+ odd: "must contain an odd number of elements"
11
+ even: "must contain an even number of elements"
@@ -0,0 +1,4 @@
1
+ module ValidatesQuantity
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,57 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class QuantityValidator < EachValidator
4
+ CHECKS = { greater_than: :>, greater_than_or_equal_to: :>=,
5
+ equal_to: :==, less_than: :<, less_than_or_equal_to: :<=,
6
+ odd: :odd?, even: :even?, other_than: :!= }.freeze
7
+
8
+ def check_validity!
9
+ keys = CHECKS.keys - [:odd, :even]
10
+ options.slice(*keys).each do |option, value|
11
+ unless value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol)
12
+ raise ArgumentError, ":#{option} must be a number, a symbol or a proc"
13
+ end
14
+ end
15
+ end
16
+
17
+ def validate_each(record, attr_name, associations)
18
+ return if options[:allow_blank] and associations.allow_blank?
19
+
20
+ value = associations.size
21
+ options.slice(*CHECKS.keys).each do |option, option_value|
22
+ case option
23
+ when :odd, :even
24
+ unless value.to_i.send(CHECKS[option])
25
+ record.errors.add(attr_name, option, filtered_options(value))
26
+ end
27
+ else
28
+ case option_value
29
+ when Proc
30
+ option_value = option_value.call(record)
31
+ when Symbol
32
+ option_value = record.send(option_value)
33
+ end
34
+
35
+ unless value.send(CHECKS[option], option_value)
36
+ record.errors.add(attr_name, option, filtered_options(value).merge!(count: option_value))
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ def filtered_options(value)
45
+ filtered = options.except(*CHECKS.keys)
46
+ filtered[:value] = value
47
+ filtered
48
+ end
49
+ end
50
+
51
+ module HelperMethods
52
+ def validates_quantity_of(*attr_names)
53
+ validates_with QuantityValidator, _merge_attributes(attr_names)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesQuantity
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_model/validator'
2
+
3
+ require "validates_quantity/engine" if defined? Rails
4
+ require "validates_quantity/version"
5
+ require "validates_quantity/quantity_validator"
data/spec/models.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Tag < ActiveRecord::Base
2
+ end
3
+
4
+ class Topic < ActiveRecord::Base
5
+ has_many :tags_topics
6
+ has_many :tags, through: :tags_topics
7
+ end
8
+
9
+ class TagsTopic < ActiveRecord::Base
10
+ belongs_to :tag
11
+ belongs_to :topic
12
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,19 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :tags, :force => true do |t|
5
+ t.string :name
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :topics, :force => true do |t|
10
+ t.string :name
11
+ t.timestamps
12
+ end
13
+
14
+ create_table :tags_topics, :force => true do |t|
15
+ t.belongs_to :tag
16
+ t.belongs_to :topic
17
+ t.timestamps
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+ require 'validates_quantity'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
5
+
6
+ load File.dirname(__FILE__) + '/schema.rb'
7
+ require File.dirname(__FILE__) + '/models.rb'
@@ -0,0 +1,142 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveModel::Validations::QuantityValidator do
4
+ before(:each) do
5
+ Topic.clear_validators!
6
+ (1..5).each { |i| Tag.new(id: i).save unless Tag.where(id: i).first }
7
+ end
8
+
9
+ it "No validation : should work" do
10
+ t = Topic.new(tag_ids: [1, 2])
11
+
12
+ expect(t.save).to eq(true)
13
+ end
14
+
15
+ it "greater_than validation : should work" do
16
+ Topic.validates_quantity_of :tags, :greater_than => 2
17
+ t = Topic.new(tag_ids: [1, 2, 3])
18
+
19
+ expect(t.save).to eq(true)
20
+ end
21
+
22
+ it "greater_than validation : should not work" do
23
+ Topic.validates_quantity_of :tags, :greater_than => 4
24
+ t = Topic.new(tag_ids: [1, 2, 3])
25
+
26
+ expect(t.save).to eq(false)
27
+ end
28
+
29
+ it "greater_than_or_equal_to validation : should work" do
30
+ Topic.validates_quantity_of :tags, :greater_than_or_equal_to => 3
31
+ t = Topic.new(tag_ids: [1, 2, 3])
32
+
33
+ expect(t.save).to eq(true)
34
+ end
35
+
36
+ it "greater_than_or_equal_to validation : should not work" do
37
+ Topic.validates_quantity_of :tags, :greater_than_or_equal_to => 5
38
+ t = Topic.new(tag_ids: [1, 2, 3])
39
+
40
+ expect(t.save).to eq(false)
41
+ end
42
+
43
+ it "equal_to validation : should work" do
44
+ Topic.validates_quantity_of :tags, :equal_to => 3
45
+ t = Topic.new(tag_ids: [1, 2, 3])
46
+
47
+ expect(t.save).to eq(true)
48
+ end
49
+
50
+ it "equal_to validation : should not work" do
51
+ Topic.validates_quantity_of :tags, :equal_to => 4
52
+ t = Topic.new(tag_ids: [1, 2, 3])
53
+
54
+ expect(t.save).to eq(false)
55
+ end
56
+
57
+ it "less_than_or_equal_to validation : should work" do
58
+ Topic.validates_quantity_of :tags, :less_than_or_equal_to => 3
59
+ t = Topic.new(tag_ids: [1, 2, 3])
60
+
61
+ expect(t.save).to eq(true)
62
+ end
63
+
64
+ it "less_than_or_equal_to validation : should not work" do
65
+ Topic.validates_quantity_of :tags, :less_than_or_equal_to => 2
66
+ t = Topic.new(tag_ids: [1, 2, 3])
67
+
68
+ expect(t.save).to eq(false)
69
+ end
70
+
71
+ it "less_than validation : should work" do
72
+ Topic.validates_quantity_of :tags, :less_than => 4
73
+ t = Topic.new(tag_ids: [1, 2, 3])
74
+
75
+ expect(t.save).to eq(true)
76
+ end
77
+
78
+ it "less_than validation : should not work" do
79
+ Topic.validates_quantity_of :tags, :less_than => 2
80
+ t = Topic.new(tag_ids: [1, 2, 3])
81
+
82
+ expect(t.save).to eq(false)
83
+ end
84
+
85
+ it "odd validation : should work" do
86
+ Topic.validates_quantity_of :tags, :odd => true
87
+ t = Topic.new(tag_ids: [1, 2, 3])
88
+
89
+ expect(t.save).to eq(true)
90
+ end
91
+
92
+ it "odd validation : should not work" do
93
+ Topic.validates_quantity_of :tags, :odd => true
94
+ t = Topic.new(tag_ids: [1, 2])
95
+
96
+ expect(t.save).to eq(false)
97
+ end
98
+
99
+ it "even validation : should work" do
100
+ Topic.validates_quantity_of :tags, :even => true
101
+ t = Topic.new(tag_ids: [1, 2])
102
+
103
+ expect(t.save).to eq(true)
104
+ end
105
+
106
+ it "even validation : should not work" do
107
+ Topic.validates_quantity_of :tags, :even => true
108
+ t = Topic.new(tag_ids: [1, 2, 3])
109
+
110
+ expect(t.save).to eq(false)
111
+ end
112
+
113
+ it "other_than validation : should work" do
114
+ Topic.validates_quantity_of :tags, :other_than => 2
115
+ t = Topic.new(tag_ids: [1, 2, 3])
116
+
117
+ expect(t.save).to eq(true)
118
+ end
119
+
120
+ it "lambda validation : should work" do
121
+ Topic.validates_quantity_of :tags, :equal_to => -> (*) {2 + 1}
122
+ t = Topic.new(tag_ids: [1, 2, 3])
123
+
124
+ expect(t.save).to eq(true)
125
+ end
126
+
127
+ it "lambda validation : should not work" do
128
+ Topic.validates_quantity_of :tags, :equal_to => -> (*) {2 + 2}
129
+ t = Topic.new(tag_ids: [1, 2, 3])
130
+
131
+ expect(t.save).to eq(false)
132
+ end
133
+
134
+ # it "Error messages" do
135
+ # Topic.validates_quantity_of :tags, :equal_to => -> (*) {2 + 1}
136
+ # t = Topic.new(tag_ids: [1, 2])
137
+ # t.save
138
+
139
+ # expect(t.errors[:tags]).to eq(["The number of Tags must be equal to 3"])
140
+ # end
141
+
142
+ end
@@ -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 'validates_quantity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "validates_quantity"
8
+ spec.version = ValidatesQuantity::VERSION
9
+ spec.authors = ["Mathieu Mahé"]
10
+ spec.email = ["mathieu.mahe@gmail.com"]
11
+ spec.summary = %q{Add to Rails the possibility to validate the quantity of an associate model}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "activerecord", "~> 4.0.0"
25
+ spec.add_development_dependency "sqlite3"
26
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_quantity
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mathieu Mahé
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - mathieu.mahe@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - config/locales/en.yml
98
+ - lib/validates_quantity.rb
99
+ - lib/validates_quantity/engine.rb
100
+ - lib/validates_quantity/quantity_validator.rb
101
+ - lib/validates_quantity/version.rb
102
+ - spec/models.rb
103
+ - spec/schema.rb
104
+ - spec/spec_helper.rb
105
+ - spec/validates_quantity_spec.rb
106
+ - validates_quantity.gemspec
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Add to Rails the possibility to validate the quantity of an associate model
131
+ test_files:
132
+ - spec/models.rb
133
+ - spec/schema.rb
134
+ - spec/spec_helper.rb
135
+ - spec/validates_quantity_spec.rb