detect_or_create 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +1 -0
- data/detect_or_create.gemspec +26 -0
- data/lib/detect_or_create.rb +14 -0
- data/lib/detect_or_create/class_methods.rb +50 -0
- data/lib/detect_or_create/version.rb +3 -0
- data/spec/detect_or_create_spec.rb +27 -0
- data/spec/spec_helper.rb +11 -0
- metadata +179 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 jnwheeler44
|
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,64 @@
|
|
1
|
+
# DetectOrCreate
|
2
|
+
|
3
|
+
Rails 2.3.9 introduces a change which makes find_or_create_by unusable
|
4
|
+
with a hash as an argument. This gem gives similar functionality, with
|
5
|
+
the added ability to overwrite the existing record that is found.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'detect_or_create'
|
12
|
+
|
13
|
+
Add after the Rails::Initializer block:
|
14
|
+
|
15
|
+
require 'detect_or_create'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install detect_or_create
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Find user with email "user@domain.com" or create a new user with that
|
28
|
+
email and additional attributes:
|
29
|
+
|
30
|
+
```
|
31
|
+
User.detect_or_create(:by => { :email => "user@domain.com" },
|
32
|
+
:with => { :name => "User", :active => true })
|
33
|
+
|
34
|
+
```
|
35
|
+
|
36
|
+
Just including the `by` argument will also work if you don't need
|
37
|
+
additional attributes.
|
38
|
+
|
39
|
+
```
|
40
|
+
User.detect_or_create(:by => { :email => "user@domain.com" })
|
41
|
+
```
|
42
|
+
|
43
|
+
You can also modify what was found:
|
44
|
+
|
45
|
+
```
|
46
|
+
User.detect_or_create(:by => { :email => "user@domain.com" },
|
47
|
+
:with => { :name => "Domain User 1",
|
48
|
+
:email => "user1@domain.com"},
|
49
|
+
:overwrite_existing => true)
|
50
|
+
```
|
51
|
+
|
52
|
+
## Run specs
|
53
|
+
|
54
|
+
|
55
|
+
$ rspec spec/detect_or_create_spec.rb
|
56
|
+
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it ( http://github.com/<my-github-username>/detect_or_create/fork )
|
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
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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 'detect_or_create/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "detect_or_create"
|
8
|
+
spec.version = DetectOrCreate::VERSION
|
9
|
+
spec.authors = ["jnwheeler44"]
|
10
|
+
spec.email = ["jnwheeler44@gmail.com"]
|
11
|
+
spec.summary = %q{Gem to replace find_or_create_by functionality in Rails 2.3}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
spec.add_development_dependency "rspec"
|
22
|
+
spec.add_development_dependency "rspec-rails"
|
23
|
+
spec.add_development_dependency 'rails', '~> 2.3'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
spec.add_runtime_dependency 'rails', '~> 2.3'
|
26
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module DetectOrCreate
|
2
|
+
module ClassMethods
|
3
|
+
def detect_or_create(args={})
|
4
|
+
if errors = invalid_arguments(args) and errors.present?
|
5
|
+
raise StandardError, "#{errors}"
|
6
|
+
end
|
7
|
+
|
8
|
+
found_object = self.first(:conditions => args[:by]) || self.create(args[:by].merge(args[:with]))
|
9
|
+
|
10
|
+
if args[:overwrite_existing] && !found_object.new_record?
|
11
|
+
found_object.update_attributes(args[:with])
|
12
|
+
end
|
13
|
+
|
14
|
+
found_object
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def required_keys
|
20
|
+
[:by]
|
21
|
+
end
|
22
|
+
|
23
|
+
def required_types
|
24
|
+
{ :by => Hash,
|
25
|
+
:with => Hash }
|
26
|
+
end
|
27
|
+
|
28
|
+
def missing_keys(args)
|
29
|
+
required_keys - args.keys
|
30
|
+
end
|
31
|
+
|
32
|
+
def mismatched_types(args)
|
33
|
+
required_types.collect { |key, type|
|
34
|
+
"`#{key}` must be a #{type}" if args[key].present? && !args[key].is_a?(type)
|
35
|
+
}.compact.join("\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
def invalid_arguments(args)
|
39
|
+
case
|
40
|
+
when missing_keys(args).present?
|
41
|
+
"Missing Arguments: `#{missing_keys(args).join(",")}`"
|
42
|
+
when mismatched_types(args).present?
|
43
|
+
mismatched_types(args)
|
44
|
+
when !args[:by].keys.present?
|
45
|
+
"Missing keys to filter by"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DetectOrCreate do
|
4
|
+
before(:all) { class User < ActiveRecord::Base; end; }
|
5
|
+
|
6
|
+
it 'validates required arguments exist' do
|
7
|
+
expect { User.detect_or_create() }.
|
8
|
+
to raise_error(StandardError, "Missing Arguments: `by`")
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'validates :by type' do
|
12
|
+
expect { User.detect_or_create(:by => ['id = ?', 1]) }.
|
13
|
+
to raise_error(StandardError, "`by` must be a Hash")
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'validates :with type' do
|
17
|
+
expect { User.detect_or_create(:by => {:name => 'name'},
|
18
|
+
:with => 1) }.
|
19
|
+
to raise_error(StandardError, "`with` must be a Hash")
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'validates :by values' do
|
23
|
+
expect { User.detect_or_create(:by => {}) }.
|
24
|
+
to raise_error(StandardError, "Missing keys to filter by")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: detect_or_create
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- jnwheeler44
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2014-03-04 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 5
|
33
|
+
version: "1.5"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :development
|
63
|
+
version_requirements: *id003
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: rspec-rails
|
66
|
+
prerelease: false
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
type: :development
|
77
|
+
version_requirements: *id004
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rails
|
80
|
+
prerelease: false
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ~>
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 5
|
87
|
+
segments:
|
88
|
+
- 2
|
89
|
+
- 3
|
90
|
+
version: "2.3"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: pry
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
107
|
+
- !ruby/object:Gem::Dependency
|
108
|
+
name: rails
|
109
|
+
prerelease: false
|
110
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ~>
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 5
|
116
|
+
segments:
|
117
|
+
- 2
|
118
|
+
- 3
|
119
|
+
version: "2.3"
|
120
|
+
type: :runtime
|
121
|
+
version_requirements: *id007
|
122
|
+
description:
|
123
|
+
email:
|
124
|
+
- jnwheeler44@gmail.com
|
125
|
+
executables: []
|
126
|
+
|
127
|
+
extensions: []
|
128
|
+
|
129
|
+
extra_rdoc_files: []
|
130
|
+
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- Gemfile
|
134
|
+
- LICENSE.txt
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- detect_or_create.gemspec
|
138
|
+
- lib/detect_or_create.rb
|
139
|
+
- lib/detect_or_create/class_methods.rb
|
140
|
+
- lib/detect_or_create/version.rb
|
141
|
+
- spec/detect_or_create_spec.rb
|
142
|
+
- spec/spec_helper.rb
|
143
|
+
has_rdoc: true
|
144
|
+
homepage: ""
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options: []
|
149
|
+
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
hash: 3
|
158
|
+
segments:
|
159
|
+
- 0
|
160
|
+
version: "0"
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
none: false
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
hash: 3
|
167
|
+
segments:
|
168
|
+
- 0
|
169
|
+
version: "0"
|
170
|
+
requirements: []
|
171
|
+
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 1.6.2
|
174
|
+
signing_key:
|
175
|
+
specification_version: 3
|
176
|
+
summary: Gem to replace find_or_create_by functionality in Rails 2.3
|
177
|
+
test_files:
|
178
|
+
- spec/detect_or_create_spec.rb
|
179
|
+
- spec/spec_helper.rb
|