at_most 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +4 -0
- data/at_most.gemspec +26 -0
- data/lib/at_most.rb +30 -0
- data/lib/at_most/version.rb +3 -0
- data/spec/active_record_spec_helper.rb +19 -0
- data/spec/en.yml +7 -0
- data/spec/models/user.rb +5 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/user_spec.rb +43 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5bd7ccf7a5bea7d3f9f4c2764ab736c644d108b7
|
4
|
+
data.tar.gz: 495c24dd455301c852a017a6908ce29eb517e120
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e173a6ca74f8f4de2371c31fc577b05de12a5bede44d73d5ecdc1814e6f132f2b0b5edb07452a053ba643f927db7bdef6a897917bedd38ec85e17222a7fd4d8
|
7
|
+
data.tar.gz: fc21d8009d3531793e8733ef297fb5c6c319c3c9326f10a79891ad01a88933209b1c71bf91c7b357956d304694f47c478e10d044b967936760e044f1fdd71547
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Kristian Freeman
|
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,85 @@
|
|
1
|
+
# at_most
|
2
|
+
|
3
|
+
[![Travis
|
4
|
+
badge](https://travis-ci.org/imkmf/at_most.png)](https://travis-ci.org/imkmf/at_most)
|
5
|
+
|
6
|
+
[![Code
|
7
|
+
Climate](https://codeclimate.com/github/imkmf/at_most.png)](https://codeclimate.com/github/imkmf/at_most)
|
8
|
+
|
9
|
+
[![Coverage
|
10
|
+
Status](https://coveralls.io/repos/imkmf/at_most/badge.png)](https://coveralls.io/r/imkmf/at_most)
|
11
|
+
|
12
|
+
A simple extension to ActiveRecord to allow limiting of model creation via
|
13
|
+
validation. This gem was extracted out of a Rails app function that limited the
|
14
|
+
number of users that could be created with CRUD access to a different AR model.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'at_most'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install at_most
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# app/models/user.rb
|
34
|
+
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
at_most(5)
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
```shell
|
42
|
+
|
43
|
+
# after fifth User instance, in IRB
|
44
|
+
> User.create!
|
45
|
+
ActiveRecord::RecordInvalid: Validation failed: The maximum number of users has
|
46
|
+
been reached.
|
47
|
+
```
|
48
|
+
|
49
|
+
It's really that simple.
|
50
|
+
|
51
|
+
# Errors
|
52
|
+
|
53
|
+
You can specify custom errors in the model, or through i18n:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# app/models/user.rb
|
57
|
+
|
58
|
+
class User < ActiveRecord::Base
|
59
|
+
at_most(5, { message: "Sorry, all our spots are full!" })
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
There are two i18n options: a **per model** setting, and a *global* setting.
|
64
|
+
|
65
|
+
```yaml
|
66
|
+
en:
|
67
|
+
activerecord:
|
68
|
+
errors:
|
69
|
+
models:
|
70
|
+
users:
|
71
|
+
at_most: "All of our spots are full :(:("
|
72
|
+
books:
|
73
|
+
at_most: "I can't read all of these!"
|
74
|
+
at_most: "Something went terribly wrong."
|
75
|
+
```
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
Running tests -- the default `rake` task runs the Rspec suite.
|
80
|
+
|
81
|
+
1. Fork it
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/at_most.gemspec
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 'at_most/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "at_most"
|
8
|
+
spec.version = AtMost::VERSION
|
9
|
+
spec.authors = ["Kristian Freeman"]
|
10
|
+
spec.email = ["kristian@kristianfreeman.com"]
|
11
|
+
spec.description = %q{Limit your ActiveRecord models}
|
12
|
+
spec.summary = %q{A simple extension to ActiveRecord to allow limiting of model creation via validation. This gem was extracted out of a Rails app function that limited the number of users that could be created with CRUD access to a different AR model.}
|
13
|
+
spec.homepage = "https://github.com/imkmf/at_most"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "activerecord"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "coveralls"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
end
|
data/lib/at_most.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "at_most/version"
|
2
|
+
require "active_record/base"
|
3
|
+
|
4
|
+
def downcase_and_pluralize(input)
|
5
|
+
return "#{ input.to_s.downcase }s"
|
6
|
+
end
|
7
|
+
|
8
|
+
def validation_error(model, message)
|
9
|
+
message || i18n_error(model) || "Maximum number of #{model.class}s has been reached"
|
10
|
+
end
|
11
|
+
|
12
|
+
def i18n_error(model)
|
13
|
+
if !I18n.t("activerecord.errors.models.#{ downcase_and_pluralize(model.class) }.at_most").include?("missing")
|
14
|
+
I18n.t("activerecord.errors.models.#{ downcase_and_pluralize(model.class) }.at_most")
|
15
|
+
elsif !I18n.t("at_most").include?("missing")
|
16
|
+
I18n.t("at_most")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class ActiveRecord::Base
|
21
|
+
def self.at_most(limiter, options = {})
|
22
|
+
validate do |model|
|
23
|
+
@all = self.class.all
|
24
|
+
@count = @all.is_a?(ActiveRecord::Relation) ? @all.size : @all.count
|
25
|
+
if @count >= limiter
|
26
|
+
model.errors.add :base, validation_error(model, options[:message])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
5
|
+
ActiveRecord::Migration.create_table :users do |t|
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.around do |example|
|
11
|
+
ActiveRecord::Base.transaction do
|
12
|
+
example.run
|
13
|
+
raise ActiveRecord::Rollback
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
I18n.load_path = Dir['spec/*.yml']
|
19
|
+
I18n.backend.load_translations
|
data/spec/en.yml
ADDED
data/spec/models/user.rb
ADDED
data/spec/spec_helper.rb
ADDED
data/spec/user_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_record_spec_helper'
|
2
|
+
require 'models/user'
|
3
|
+
|
4
|
+
describe User do
|
5
|
+
def three_times(klass)
|
6
|
+
3.times do
|
7
|
+
klass.create
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class UserWithError < User
|
12
|
+
at_most(3, { message: "KHAAAN" })
|
13
|
+
end
|
14
|
+
|
15
|
+
class Book < User; end
|
16
|
+
|
17
|
+
describe 'at_most(3)' do
|
18
|
+
it 'should not fail when under the user limit' do
|
19
|
+
User.create.should be_valid
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should make objects up to the limit without failing' do
|
23
|
+
lambda { three_times(User) }.should_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should fail on the fourth creation' do
|
27
|
+
three_times(User)
|
28
|
+
User.create.should_not be_valid
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should accept a custom error in the method' do
|
32
|
+
three_times(UserWithError)
|
33
|
+
# en.activerecord.errors.models.users.at_most
|
34
|
+
UserWithError.create.errors.inspect.should include("KHAAAN")
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should prefer a specific i18n option' do
|
38
|
+
three_times(Book)
|
39
|
+
# en.at_most
|
40
|
+
Book.create.errors.inspect.should include("Fallback")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: at_most
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kristian Freeman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: coveralls
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Limit your ActiveRecord models
|
98
|
+
email:
|
99
|
+
- kristian@kristianfreeman.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- .travis.yml
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- at_most.gemspec
|
112
|
+
- lib/at_most.rb
|
113
|
+
- lib/at_most/version.rb
|
114
|
+
- spec/active_record_spec_helper.rb
|
115
|
+
- spec/en.yml
|
116
|
+
- spec/models/user.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/user_spec.rb
|
119
|
+
homepage: https://github.com/imkmf/at_most
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.0.3
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: A simple extension to ActiveRecord to allow limiting of model creation via
|
143
|
+
validation. This gem was extracted out of a Rails app function that limited the
|
144
|
+
number of users that could be created with CRUD access to a different AR model.
|
145
|
+
test_files:
|
146
|
+
- spec/active_record_spec_helper.rb
|
147
|
+
- spec/en.yml
|
148
|
+
- spec/models/user.rb
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/user_spec.rb
|