activemodel_unique_errors 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/.travis.yml +25 -0
- data/CHANGELOG +3 -0
- data/Gemfile +21 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +43 -0
- data/Rakefile +34 -0
- data/VERSION +1 -0
- data/activemodel_unique_errors.gemspec +61 -0
- data/lib/activemodel_unique_errors.rb +28 -0
- data/spec/activemodel_unique_errors_spec.rb +91 -0
- data/spec/spec_helper.rb +13 -0
- metadata +128 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.0
|
7
|
+
- ruby-head
|
8
|
+
- jruby-18mode
|
9
|
+
- jruby-19mode
|
10
|
+
- jruby-head
|
11
|
+
|
12
|
+
env:
|
13
|
+
- "ACTIVEMODEL_VERSION=3.2.19"
|
14
|
+
- "ACTIVEMODEL_VERSION=4.0.8"
|
15
|
+
- "ACTIVEMODEL_VERSION=4.1.4"
|
16
|
+
|
17
|
+
matrix:
|
18
|
+
exclude:
|
19
|
+
- rvm: jruby-18mode
|
20
|
+
env: "ACTIVEMODEL_VERSION=4.0.8"
|
21
|
+
- rvm: jruby-18mode
|
22
|
+
env: "ACTIVEMODEL_VERSION=4.1.4"
|
23
|
+
allow_failures:
|
24
|
+
- rvm: ruby-head
|
25
|
+
- rvm: jruby-head
|
data/CHANGELOG
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
|
4
|
+
activemodel_version = ENV['ACTIVEMODEL_VERSION'] || 'default'
|
5
|
+
|
6
|
+
current_version = case activemodel_version
|
7
|
+
when 'default'
|
8
|
+
'~> 4.1.4'
|
9
|
+
else
|
10
|
+
"~> #{activemodel_version}"
|
11
|
+
end
|
12
|
+
|
13
|
+
gem 'activemodel', current_version
|
14
|
+
|
15
|
+
# Add dependencies to develop your gem here.
|
16
|
+
# Include everything needed to run rake, tests, features, etc.
|
17
|
+
group :development do
|
18
|
+
gem 'bundler', '~> 1.0'
|
19
|
+
gem 'jeweler', '~> 2.0.1'
|
20
|
+
gem 'rspec', '~> 2.8.0'
|
21
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 soeren mothes
|
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.
|
data/README.markdown
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# activemodel_unique_error
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/soemo/activemodel_unique_errors.svg?branch=master)](https://travis-ci.org/soemo/activemodel_unique_errors)
|
4
|
+
|
5
|
+
Only add a error messages for one model attribute if this error message not exists for the model attribute
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'activemodel_unique_errors'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install activemodel_unique_errors
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
```ruby
|
23
|
+
user = User.new
|
24
|
+
user.errors.size # 0
|
25
|
+
|
26
|
+
msg = 'This error text must be uniq!'
|
27
|
+
user.errors.add(:login, msg)
|
28
|
+
user.errors.add(:login, msg)
|
29
|
+
user.errors[:login]= msg
|
30
|
+
|
31
|
+
|
32
|
+
user.errors[:login] # ['Login This error text must be uniq!'] - only one unique error for this attribute and message
|
33
|
+
user.errors.size # 1
|
34
|
+
|
35
|
+
user.errors.add(:login, 'New error message')
|
36
|
+
user.errors[:login] # ['Login This error text must be uniq!', 'Login New error message']
|
37
|
+
user.errors.size # 2
|
38
|
+
```
|
39
|
+
|
40
|
+
## Copyright
|
41
|
+
|
42
|
+
Copyright (c) 2014 soeren mothes. See LICENSE.txt for
|
43
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "activemodel_unique_errors"
|
18
|
+
gem.homepage = "http://github.com/soemo/activemodel_unique_errors"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Uniqe model errors}
|
21
|
+
gem.description = %Q{Only add a error messages for one model attribute if this error message not exists for the model attribute}
|
22
|
+
gem.email = "soeren@mothes.org"
|
23
|
+
gem.authors = ["soeren mothes"]
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
26
|
+
|
27
|
+
require 'rspec/core'
|
28
|
+
require 'rspec/core/rake_task'
|
29
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
30
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "activemodel_unique_errors"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["soeren mothes"]
|
12
|
+
s.date = "2014-08-10"
|
13
|
+
s.description = "Only add a error messages for one model attribute if this error message not exists for the model attribute"
|
14
|
+
s.email = "soeren@mothes.org"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"CHANGELOG",
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.markdown"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".rspec",
|
22
|
+
".travis.yml",
|
23
|
+
"CHANGELOG",
|
24
|
+
"Gemfile",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.markdown",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"activemodel_unique_errors.gemspec",
|
30
|
+
"lib/activemodel_unique_errors.rb",
|
31
|
+
"spec/activemodel_unique_errors_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/soemo/activemodel_unique_errors"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = "1.8.24"
|
38
|
+
s.summary = "Uniqe model errors"
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<activemodel>, ["~> 4.1.4"])
|
45
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
47
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<activemodel>, ["~> 4.1.4"])
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
52
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<activemodel>, ["~> 4.1.4"])
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
58
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
module ActiveModelUniqueErrors
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.alias_method_chain :"[]=", :unique_check
|
8
|
+
base.alias_method_chain :add, :unique_check
|
9
|
+
end
|
10
|
+
|
11
|
+
define_method "[]_with_unique_check=" do |attribute, error|
|
12
|
+
unless self.added?(attribute, error)
|
13
|
+
self.send("[]_without_unique_check=", attribute, error)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_with_unique_check(attribute, message = :invalid, options = {})
|
18
|
+
unless self.added?(attribute, message, options)
|
19
|
+
add_without_unique_check(attribute, message, options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
if defined? ActiveModel::Errors
|
27
|
+
ActiveModel::Errors.send(:include,ActiveModelUniqueErrors)
|
28
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_model'
|
3
|
+
require 'activemodel_unique_errors'
|
4
|
+
|
5
|
+
describe 'ActivemodelUniqueErrors' do
|
6
|
+
|
7
|
+
class User
|
8
|
+
include ActiveModel::Validations
|
9
|
+
#include ActiveModel::Conversion
|
10
|
+
#extend ActiveModel::Naming
|
11
|
+
|
12
|
+
attr_accessor :login, :notice
|
13
|
+
end
|
14
|
+
|
15
|
+
before(:each) do
|
16
|
+
@user = User.new
|
17
|
+
@user.errors.size.should == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
it 'should add only errors if this error not exists - []' do
|
22
|
+
msg = 'This error text must be uniq!'
|
23
|
+
@user.errors[:login]= msg
|
24
|
+
@user.errors[:login].should == [msg]
|
25
|
+
|
26
|
+
# next add with same error msg on same attr. will not insert the error
|
27
|
+
@user.errors[:login]= msg
|
28
|
+
@user.errors[:login].should == [msg]
|
29
|
+
@user.errors['login']= msg
|
30
|
+
@user.errors['login'].should == [msg]
|
31
|
+
@user.errors.size.should == 1
|
32
|
+
|
33
|
+
# on other attr should work
|
34
|
+
@user.errors[:notice]= msg
|
35
|
+
@user.errors[:notice].should == [msg]
|
36
|
+
|
37
|
+
# a new error msg could also add
|
38
|
+
other_msg = 'new error text'
|
39
|
+
@user.errors[:login]= other_msg
|
40
|
+
@user.errors[:login].should == [msg, other_msg]
|
41
|
+
|
42
|
+
# final
|
43
|
+
@user.errors.size.should == 3
|
44
|
+
@user.errors.full_messages.should == ['Login This error text must be uniq!',
|
45
|
+
'Login new error text',
|
46
|
+
'Notice This error text must be uniq!']
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should add errors the normal way - []' do
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should add only errors if this error not exists - add()' do
|
55
|
+
msg = 'This error text must be uniq!'
|
56
|
+
@user.errors.add(:login, msg)
|
57
|
+
@user.errors[:login].should == [msg]
|
58
|
+
|
59
|
+
# next add with same error msg on same attr. will not insert the error
|
60
|
+
@user.errors.add(:login, msg)
|
61
|
+
@user.errors[:login].should == [msg]
|
62
|
+
@user.errors['login'].should == [msg]
|
63
|
+
@user.errors.size.should == 1
|
64
|
+
|
65
|
+
# on other attr should work
|
66
|
+
@user.errors.add(:notice, msg)
|
67
|
+
@user.errors[:notice].should == [msg]
|
68
|
+
|
69
|
+
# a new error msg could also add
|
70
|
+
other_msg = 'new error text'
|
71
|
+
@user.errors.add(:login, other_msg)
|
72
|
+
@user.errors[:login].should == [msg, other_msg]
|
73
|
+
|
74
|
+
# final
|
75
|
+
@user.errors.size.should == 3
|
76
|
+
@user.errors.full_messages.should == ['Login This error text must be uniq!',
|
77
|
+
'Login new error text',
|
78
|
+
'Notice This error text must be uniq!']
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should add errors the normal way' do
|
83
|
+
msg = 'This error text must be uniq!'
|
84
|
+
@user.errors.add_without_unique_check(:login, msg)
|
85
|
+
@user.errors[:login].should == [msg]
|
86
|
+
|
87
|
+
@user.errors.add_without_unique_check(:login, msg)
|
88
|
+
@user.errors[:login].should == [msg, msg]
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'activemodel_unique_errors'
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activemodel_unique_errors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- soeren mothes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.1.4
|
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.1.4
|
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.0'
|
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.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.0.1
|
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: 2.0.1
|
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: 2.8.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: 2.8.0
|
78
|
+
description: Only add a error messages for one model attribute if this error message
|
79
|
+
not exists for the model attribute
|
80
|
+
email: soeren@mothes.org
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files:
|
84
|
+
- CHANGELOG
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.markdown
|
87
|
+
files:
|
88
|
+
- .rspec
|
89
|
+
- .travis.yml
|
90
|
+
- CHANGELOG
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.markdown
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- activemodel_unique_errors.gemspec
|
97
|
+
- lib/activemodel_unique_errors.rb
|
98
|
+
- spec/activemodel_unique_errors_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: http://github.com/soemo/activemodel_unique_errors
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
hash: 859635265
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.24
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Uniqe model errors
|
128
|
+
test_files: []
|