find_or_create_on_scopes 1.0.0 → 1.1.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.
- data/LICENSE +1 -1
- data/README.textile +12 -5
- data/find_or_create_on_scopes.gemspec +29 -29
- data/lib/find_or_create_on_scopes.rb +24 -26
- metadata +87 -61
- data/.document +0 -5
- data/.gitignore +0 -26
- data/.rspec +0 -1
- data/Gemfile +0 -13
- data/Gemfile.lock +0 -56
- data/Rakefile +0 -36
- data/VERSION +0 -1
- data/spec/find_or_create_on_scopes_spec.rb +0 -87
- data/spec/spec_helper.rb +0 -22
data/LICENSE
CHANGED
data/README.textile
CHANGED
@@ -1,18 +1,24 @@
|
|
1
1
|
h1. find_or_create_on_scopes -- Conditional creators for ActiveRecord
|
2
2
|
|
3
3
|
| *Author* | Tim Morgan |
|
4
|
-
| *Version* | 1.
|
4
|
+
| *Version* | 1.1 (Sep 1, 2011) |
|
5
5
|
| *License* | Released under the MIT license. |
|
6
6
|
|
7
7
|
h2. About
|
8
8
|
|
9
|
-
@find_or_create_on_scopes@ adds handy @find_or_create@-type methods to
|
9
|
+
@find_or_create_on_scopes@ adds handy @find_or_create@-type methods to
|
10
|
+
@ActiveRecord@. You can use these methods to conditionally create, initialize,
|
11
|
+
or update records depending on the presence of similar records. They work on
|
12
|
+
your base model class and any scopes derived therefrom. (See _Usage_ for
|
13
|
+
examples.)
|
10
14
|
|
11
15
|
h2. Installation
|
12
16
|
|
13
|
-
*Important Note:* This gem requires Ruby 1.9. Ruby 1.8 is not and will never be
|
17
|
+
*Important Note:* This gem requires Ruby 1.9. Ruby 1.8 is not and will never be
|
18
|
+
supported.
|
14
19
|
|
15
|
-
To install, simply add the @find_or_create_on_scopes@ gem to your Rails
|
20
|
+
To install, simply add the @find_or_create_on_scopes@ gem to your Rails
|
21
|
+
application's @Gemfile@:
|
16
22
|
|
17
23
|
<pre><code>
|
18
24
|
gem 'find_or_create_on_scopes'
|
@@ -20,7 +26,8 @@ gem 'find_or_create_on_scopes'
|
|
20
26
|
|
21
27
|
h2. Usage
|
22
28
|
|
23
|
-
See the
|
29
|
+
See the {FindOrCreateOnScopes} module documentation for the complete list of
|
30
|
+
conditional creators/updaters. Some basic examples to give you an overview:
|
24
31
|
|
25
32
|
<pre><code>
|
26
33
|
# Tries to find a user named 'riscfuture'. If such a User exists, returns it. If
|
@@ -1,58 +1,58 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{find_or_create_on_scopes}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date = %q{
|
11
|
+
s.authors = [%q{Tim Morgan}]
|
12
|
+
s.date = %q{2011-09-01}
|
13
13
|
s.description = %q{Adds methods to ActiveRecord for conditionally finding, creating, or updating records.}
|
14
14
|
s.email = %q{git@timothymorgan.info}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.textile"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
"
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
"Gemfile.lock",
|
25
|
-
"LICENSE",
|
26
|
-
"README.textile",
|
27
|
-
"Rakefile",
|
28
|
-
"VERSION",
|
29
|
-
"find_or_create_on_scopes.gemspec",
|
30
|
-
"lib/find_or_create_on_scopes.rb",
|
31
|
-
"spec/find_or_create_on_scopes_spec.rb",
|
32
|
-
"spec/spec_helper.rb"
|
20
|
+
"LICENSE",
|
21
|
+
"README.textile",
|
22
|
+
"find_or_create_on_scopes.gemspec",
|
23
|
+
"lib/find_or_create_on_scopes.rb"
|
33
24
|
]
|
34
25
|
s.homepage = %q{http://github.com/riscfuture/find_or_create_on_scopes}
|
35
|
-
s.
|
36
|
-
s.require_paths = ["lib"]
|
26
|
+
s.require_paths = [%q{lib}]
|
37
27
|
s.required_ruby_version = Gem::Requirement.new(">= 1.9")
|
38
|
-
s.rubygems_version = %q{1.
|
28
|
+
s.rubygems_version = %q{1.8.9}
|
39
29
|
s.summary = %q{find_or_create-type methods on ActiveRecord scopes}
|
40
|
-
s.test_files = [
|
41
|
-
"spec/find_or_create_on_scopes_spec.rb",
|
42
|
-
"spec/spec_helper.rb"
|
43
|
-
]
|
44
30
|
|
45
31
|
if s.respond_to? :specification_version then
|
46
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
32
|
s.specification_version = 3
|
48
33
|
|
49
34
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
-
s.add_runtime_dependency(%q<activerecord>, [">=
|
35
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.1"])
|
36
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
37
|
+
s.add_development_dependency(%q<yard>, [">= 0"])
|
38
|
+
s.add_development_dependency(%q<RedCloth>, [">= 0"])
|
39
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
40
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
51
41
|
else
|
52
|
-
s.add_dependency(%q<activerecord>, [">=
|
42
|
+
s.add_dependency(%q<activerecord>, [">= 3.1"])
|
43
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
44
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
45
|
+
s.add_dependency(%q<RedCloth>, [">= 0"])
|
46
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
47
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
53
48
|
end
|
54
49
|
else
|
55
|
-
s.add_dependency(%q<activerecord>, [">=
|
50
|
+
s.add_dependency(%q<activerecord>, [">= 3.1"])
|
51
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
52
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
53
|
+
s.add_dependency(%q<RedCloth>, [">= 0"])
|
54
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
55
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -3,26 +3,25 @@
|
|
3
3
|
module FindOrCreateOnScopes
|
4
4
|
|
5
5
|
# Locates a record according to the current scope. Returns the record if
|
6
|
-
# found. If not found, creates a new record with the
|
7
|
-
# scope and
|
6
|
+
# found. If not found, creates a new record with the attributes of the current
|
7
|
+
# scope and the provided attributes.
|
8
8
|
#
|
9
|
-
# @param [
|
10
|
-
# created.
|
9
|
+
# @param [Array] *args Arguments to pass to the @create@ method.
|
11
10
|
# @yield [record] Yields the record before it is saved.
|
12
|
-
# @yieldparam [ActiveRecord::Base] The found or created record before
|
13
|
-
# saved.
|
11
|
+
# @yieldparam [ActiveRecord::Base] record The found or created record before
|
12
|
+
# it is saved.
|
14
13
|
# @return [ActiveRecord::Base] The found or created record.
|
15
14
|
|
16
|
-
def find_or_create(
|
17
|
-
find_or_initialize_and_do :save,
|
15
|
+
def find_or_create(*args, &block)
|
16
|
+
find_or_initialize_and_do :save, *args, &block
|
18
17
|
end
|
19
18
|
|
20
19
|
# Same as {#find_or_create} but calls @save!@ instead of @save@ on the record.
|
21
20
|
#
|
22
21
|
# @see #find_or_create
|
23
22
|
|
24
|
-
def find_or_create!(
|
25
|
-
find_or_initialize_and_do :save!,
|
23
|
+
def find_or_create!(*args, &block)
|
24
|
+
find_or_initialize_and_do :save!, *args, &block
|
26
25
|
end
|
27
26
|
|
28
27
|
# Same as {#find_or_create} but does not save the record. Please note that
|
@@ -31,23 +30,22 @@ module FindOrCreateOnScopes
|
|
31
30
|
#
|
32
31
|
# @see #find_or_create
|
33
32
|
|
34
|
-
def find_or_initialize(
|
35
|
-
find_or_initialize_and_do nil,
|
33
|
+
def find_or_initialize(*args, &block)
|
34
|
+
find_or_initialize_and_do nil, *args, &block
|
36
35
|
end
|
37
36
|
|
38
37
|
# Locates a record according to the current scope. Updates the record with
|
39
|
-
#
|
40
|
-
# current scope's
|
38
|
+
# the provided attributes if found. If not found, creates a new record with
|
39
|
+
# the current scope's attributes and the provided attributes.
|
41
40
|
#
|
42
|
-
# @param [
|
43
|
-
# not it's newly created.
|
41
|
+
# @param [Array] *args Arguments to pass to the @assign_attributes@ method.
|
44
42
|
# @yield [record] Yields the record before it is saved.
|
45
|
-
# @yieldparam [ActiveRecord::Base] The found or created record before
|
46
|
-
# saved.
|
43
|
+
# @yieldparam [ActiveRecord::Base] record The found or created record before
|
44
|
+
# it is saved.
|
47
45
|
# @return [ActiveRecord::Base] The found or created record.
|
48
46
|
|
49
|
-
def create_or_update(
|
50
|
-
create_or_update_and_do :save,
|
47
|
+
def create_or_update(*args, &block)
|
48
|
+
create_or_update_and_do :save, *args, &block
|
51
49
|
end
|
52
50
|
|
53
51
|
# Same as {#create_or_update} but calls @save!@ instead of @save@ on the
|
@@ -55,27 +53,27 @@ module FindOrCreateOnScopes
|
|
55
53
|
#
|
56
54
|
# @see #create_or_update
|
57
55
|
|
58
|
-
def create_or_update!(
|
59
|
-
create_or_update_and_do :save!,
|
56
|
+
def create_or_update!(*args, &block)
|
57
|
+
create_or_update_and_do :save!, *args, &block
|
60
58
|
end
|
61
59
|
|
62
60
|
private
|
63
61
|
|
64
|
-
def find_or_initialize_and_do(meth,
|
62
|
+
def find_or_initialize_and_do(meth, *args)
|
65
63
|
record = nil
|
66
64
|
transaction do
|
67
|
-
record = first || new(
|
65
|
+
record = first || new(*args)
|
68
66
|
yield record if block_given?
|
69
67
|
record.send(meth) if meth
|
70
68
|
end
|
71
69
|
return record
|
72
70
|
end
|
73
71
|
|
74
|
-
def create_or_update_and_do(meth,
|
72
|
+
def create_or_update_and_do(meth, *args)
|
75
73
|
record = nil
|
76
74
|
transaction do
|
77
75
|
record = first || new
|
78
|
-
record.
|
76
|
+
record.assign_attributes *args
|
79
77
|
yield record if block_given?
|
80
78
|
record.send(meth) if meth
|
81
79
|
end
|
metadata
CHANGED
@@ -1,91 +1,117 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_or_create_on_scopes
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
version: 1.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Tim Morgan
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-09-01 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: activerecord
|
22
|
-
requirement: &
|
16
|
+
requirement: &2167556560 !ruby/object:Gem::Requirement
|
23
17
|
none: false
|
24
|
-
requirements:
|
25
|
-
- -
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
- 0
|
29
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
30
22
|
type: :runtime
|
31
23
|
prerelease: false
|
32
|
-
version_requirements: *
|
33
|
-
|
24
|
+
version_requirements: *2167556560
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jeweler
|
27
|
+
requirement: &2167556020 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2167556020
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: yard
|
38
|
+
requirement: &2167555520 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2167555520
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: RedCloth
|
49
|
+
requirement: &2167555020 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2167555020
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: sqlite3
|
60
|
+
requirement: &2167554520 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2167554520
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: &2167554040 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2167554040
|
80
|
+
description: Adds methods to ActiveRecord for conditionally finding, creating, or
|
81
|
+
updating records.
|
34
82
|
email: git@timothymorgan.info
|
35
83
|
executables: []
|
36
|
-
|
37
84
|
extensions: []
|
38
|
-
|
39
|
-
extra_rdoc_files:
|
85
|
+
extra_rdoc_files:
|
40
86
|
- LICENSE
|
41
87
|
- README.textile
|
42
|
-
files:
|
43
|
-
- .document
|
44
|
-
- .gitignore
|
45
|
-
- .rspec
|
46
|
-
- Gemfile
|
47
|
-
- Gemfile.lock
|
88
|
+
files:
|
48
89
|
- LICENSE
|
49
90
|
- README.textile
|
50
|
-
- Rakefile
|
51
|
-
- VERSION
|
52
91
|
- find_or_create_on_scopes.gemspec
|
53
92
|
- lib/find_or_create_on_scopes.rb
|
54
|
-
- spec/find_or_create_on_scopes_spec.rb
|
55
|
-
- spec/spec_helper.rb
|
56
|
-
has_rdoc: true
|
57
93
|
homepage: http://github.com/riscfuture/find_or_create_on_scopes
|
58
94
|
licenses: []
|
59
|
-
|
60
95
|
post_install_message:
|
61
|
-
rdoc_options:
|
62
|
-
|
63
|
-
require_paths:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
64
98
|
- lib
|
65
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
100
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
|
72
|
-
- 9
|
73
|
-
version: "1.9"
|
74
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '1.9'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
106
|
none: false
|
76
|
-
requirements:
|
77
|
-
- -
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
|
80
|
-
- 0
|
81
|
-
version: "0"
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
82
111
|
requirements: []
|
83
|
-
|
84
112
|
rubyforge_project:
|
85
|
-
rubygems_version: 1.
|
113
|
+
rubygems_version: 1.8.9
|
86
114
|
signing_key:
|
87
115
|
specification_version: 3
|
88
116
|
summary: find_or_create-type methods on ActiveRecord scopes
|
89
|
-
test_files:
|
90
|
-
- spec/find_or_create_on_scopes_spec.rb
|
91
|
-
- spec/spec_helper.rb
|
117
|
+
test_files: []
|
data/.document
DELETED
data/.gitignore
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
## MAC OS
|
2
|
-
.DS_Store
|
3
|
-
|
4
|
-
## TEXTMATE
|
5
|
-
*.tmproj
|
6
|
-
tmtags
|
7
|
-
|
8
|
-
## EMACS
|
9
|
-
*~
|
10
|
-
\#*
|
11
|
-
.\#*
|
12
|
-
|
13
|
-
## VIM
|
14
|
-
*.swp
|
15
|
-
|
16
|
-
## PROJECT::GENERAL
|
17
|
-
coverage
|
18
|
-
rdoc
|
19
|
-
pkg
|
20
|
-
.bundle
|
21
|
-
.rvmrc
|
22
|
-
test.sqlite
|
23
|
-
|
24
|
-
## PROJECT::DOCUMENTATION
|
25
|
-
.yardoc
|
26
|
-
doc
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
-cfs
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
RedCloth (4.2.3)
|
5
|
-
activemodel (3.0.1)
|
6
|
-
activesupport (= 3.0.1)
|
7
|
-
builder (~> 2.1.2)
|
8
|
-
i18n (~> 0.4.1)
|
9
|
-
activerecord (3.0.1)
|
10
|
-
activemodel (= 3.0.1)
|
11
|
-
activesupport (= 3.0.1)
|
12
|
-
arel (~> 1.0.0)
|
13
|
-
tzinfo (~> 0.3.23)
|
14
|
-
activesupport (3.0.1)
|
15
|
-
arel (1.0.1)
|
16
|
-
activesupport (~> 3.0.0)
|
17
|
-
builder (2.1.2)
|
18
|
-
diff-lcs (1.1.2)
|
19
|
-
ffi (0.6.3)
|
20
|
-
rake (>= 0.8.7)
|
21
|
-
gemcutter (0.6.1)
|
22
|
-
git (1.2.5)
|
23
|
-
i18n (0.4.2)
|
24
|
-
jeweler (1.4.0)
|
25
|
-
gemcutter (>= 0.1.0)
|
26
|
-
git (>= 1.2.5)
|
27
|
-
rubyforge (>= 2.0.0)
|
28
|
-
json_pure (1.4.6)
|
29
|
-
rake (0.8.7)
|
30
|
-
rspec (2.0.1)
|
31
|
-
rspec-core (~> 2.0.1)
|
32
|
-
rspec-expectations (~> 2.0.1)
|
33
|
-
rspec-mocks (~> 2.0.1)
|
34
|
-
rspec-core (2.0.1)
|
35
|
-
rspec-expectations (2.0.1)
|
36
|
-
diff-lcs (>= 1.1.2)
|
37
|
-
rspec-mocks (2.0.1)
|
38
|
-
rspec-core (~> 2.0.1)
|
39
|
-
rspec-expectations (~> 2.0.1)
|
40
|
-
rubyforge (2.0.4)
|
41
|
-
json_pure (>= 1.1.7)
|
42
|
-
sqlite3 (0.1.1)
|
43
|
-
ffi (>= 0.6.3)
|
44
|
-
tzinfo (0.3.23)
|
45
|
-
yard (0.6.1)
|
46
|
-
|
47
|
-
PLATFORMS
|
48
|
-
ruby
|
49
|
-
|
50
|
-
DEPENDENCIES
|
51
|
-
RedCloth
|
52
|
-
activerecord
|
53
|
-
jeweler
|
54
|
-
rspec
|
55
|
-
sqlite3
|
56
|
-
yard
|
data/Rakefile
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
begin
|
3
|
-
require 'bundler'
|
4
|
-
rescue LoadError
|
5
|
-
puts "Bundler is not installed; install with `gem install bundler`."
|
6
|
-
exit 1
|
7
|
-
end
|
8
|
-
|
9
|
-
Bundler.require :default
|
10
|
-
|
11
|
-
Jeweler::Tasks.new do |gem|
|
12
|
-
gem.name = "find_or_create_on_scopes"
|
13
|
-
gem.summary = %Q{find_or_create-type methods on ActiveRecord scopes}
|
14
|
-
gem.description = %Q{Adds methods to ActiveRecord for conditionally finding, creating, or updating records.}
|
15
|
-
gem.email = "git@timothymorgan.info"
|
16
|
-
gem.homepage = "http://github.com/riscfuture/find_or_create_on_scopes"
|
17
|
-
gem.authors = [ "Tim Morgan" ]
|
18
|
-
gem.required_ruby_version = '>= 1.9'
|
19
|
-
gem.add_dependency "activerecord", ">= 0"
|
20
|
-
end
|
21
|
-
Jeweler::GemcutterTasks.new
|
22
|
-
|
23
|
-
require 'rspec/core/rake_task'
|
24
|
-
RSpec::Core::RakeTask.new
|
25
|
-
|
26
|
-
YARD::Rake::YardocTask.new('doc') do |doc|
|
27
|
-
doc.options << "-m" << "textile"
|
28
|
-
doc.options << "--protected"
|
29
|
-
doc.options << "-r" << "README.textile"
|
30
|
-
doc.options << "-o" << "doc"
|
31
|
-
doc.options << "--title" << "find_or_create_on_scopes Documentation".inspect
|
32
|
-
|
33
|
-
doc.files = [ 'lib/**/*', 'README.textile' ]
|
34
|
-
end
|
35
|
-
|
36
|
-
task(default: :spec)
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.0
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe FindOrCreateOnScopes do
|
4
|
-
{
|
5
|
-
:find_or_create => :save,
|
6
|
-
:find_or_create! => :save!,
|
7
|
-
:find_or_initialize => nil
|
8
|
-
}.each do |meth, saver|
|
9
|
-
describe "##{meth}" do
|
10
|
-
it "should find an object if a matching one exists" do
|
11
|
-
record = Option.create!(name: 'foo', value: 'bar')
|
12
|
-
Option.where(name: 'foo').send(meth, value: 'bar2').should eql(record)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should create an object if a matching one does not exist" do
|
16
|
-
Option.create!(name: 'foo2', value: 'bar2')
|
17
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar2')
|
18
|
-
record.name.should eql('foo')
|
19
|
-
record.value.should eql('bar2')
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should do so in a transaction" do
|
23
|
-
Option.should_receive(:transaction)
|
24
|
-
Option.where(name: 'foo').send(meth, value: 'bar')
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should yield the object to the block" do
|
28
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar') { |u| u.field = "foobar" }
|
29
|
-
record.field.should eql('foobar')
|
30
|
-
|
31
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar') { |u| u.field = "foobar2" }
|
32
|
-
record.field.should eql('foobar2')
|
33
|
-
end
|
34
|
-
|
35
|
-
if saver then
|
36
|
-
it "should call #{saver}" do
|
37
|
-
record = Option.new
|
38
|
-
Option.stub!(:new).and_return(record)
|
39
|
-
record.should_receive(saver).once.and_return(true)
|
40
|
-
Option.where(name: 'foo').send(meth, value: 'bar').should eql(record)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
{
|
47
|
-
:create_or_update => :save,
|
48
|
-
:create_or_update! => :save!,
|
49
|
-
}.each do |meth, saver|
|
50
|
-
describe "##{meth}" do
|
51
|
-
it "should find an object and update it if a matching one exists" do
|
52
|
-
record = Option.create!(name: 'foo', value: 'bar')
|
53
|
-
Option.where(name: 'foo').send(meth, value: 'bar2').should eql(record)
|
54
|
-
record.reload.value.should eql('bar2')
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should create an object if a matching one does not exist" do
|
58
|
-
Option.create!(name: 'foo2', value: 'bar2')
|
59
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar')
|
60
|
-
record.name.should eql('foo')
|
61
|
-
record.value.should eql('bar')
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should do so in a transaction" do
|
65
|
-
Option.should_receive(:transaction)
|
66
|
-
Option.where(name: 'foo').send(meth, value: 'bar')
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should yield the object to the block" do
|
70
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar') { |u| u.field = "foobar" }
|
71
|
-
record.field.should eql('foobar')
|
72
|
-
|
73
|
-
record = Option.where(name: 'foo').send(meth, value: 'bar') { |u| u.field = "foobar2" }
|
74
|
-
record.field.should eql('foobar2')
|
75
|
-
end
|
76
|
-
|
77
|
-
if saver then
|
78
|
-
it "should call #{saver}" do
|
79
|
-
record = Option.new
|
80
|
-
Option.stub!(:new).and_return(record)
|
81
|
-
record.should_receive(saver).and_return(true)
|
82
|
-
Option.where(name: 'foo').send(meth, value: 'bar').should eql(record)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Bundler.require :default, :test
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
-
|
6
|
-
require 'find_or_create_on_scopes'
|
7
|
-
|
8
|
-
ActiveRecord::Base.establish_connection(
|
9
|
-
adapter: 'sqlite3',
|
10
|
-
database: 'test.sqlite'
|
11
|
-
)
|
12
|
-
|
13
|
-
class Option < ActiveRecord::Base
|
14
|
-
attr_accessor :field
|
15
|
-
end
|
16
|
-
|
17
|
-
RSpec.configure do |config|
|
18
|
-
config.before(:each) do
|
19
|
-
Option.connection.execute "DROP TABLE IF EXISTS options"
|
20
|
-
Option.connection.execute "CREATE TABLE options (id INTEGER PRIMARY KEY ASC, name VARCHAR(127) NOT NULL, value VARCHAR(255))"
|
21
|
-
end
|
22
|
-
end
|