couchrest_model 1.0.0.beta7
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +320 -0
- data/Rakefile +71 -0
- data/THANKS.md +19 -0
- data/examples/model/example.rb +144 -0
- data/history.txt +180 -0
- data/lib/couchrest/model.rb +10 -0
- data/lib/couchrest/model/associations.rb +207 -0
- data/lib/couchrest/model/attribute_protection.rb +74 -0
- data/lib/couchrest/model/attributes.rb +75 -0
- data/lib/couchrest/model/base.rb +111 -0
- data/lib/couchrest/model/callbacks.rb +27 -0
- data/lib/couchrest/model/casted_array.rb +39 -0
- data/lib/couchrest/model/casted_model.rb +68 -0
- data/lib/couchrest/model/class_proxy.rb +122 -0
- data/lib/couchrest/model/collection.rb +260 -0
- data/lib/couchrest/model/design_doc.rb +126 -0
- data/lib/couchrest/model/document_queries.rb +82 -0
- data/lib/couchrest/model/errors.rb +23 -0
- data/lib/couchrest/model/extended_attachments.rb +73 -0
- data/lib/couchrest/model/persistence.rb +141 -0
- data/lib/couchrest/model/properties.rb +144 -0
- data/lib/couchrest/model/property.rb +96 -0
- data/lib/couchrest/model/support/couchrest.rb +19 -0
- data/lib/couchrest/model/support/hash.rb +9 -0
- data/lib/couchrest/model/typecast.rb +170 -0
- data/lib/couchrest/model/validations.rb +68 -0
- data/lib/couchrest/model/validations/casted_model.rb +14 -0
- data/lib/couchrest/model/validations/locale/en.yml +5 -0
- data/lib/couchrest/model/validations/uniqueness.rb +45 -0
- data/lib/couchrest/model/views.rb +167 -0
- data/lib/couchrest_model.rb +56 -0
- data/spec/couchrest/assocations_spec.rb +213 -0
- data/spec/couchrest/attachment_spec.rb +148 -0
- data/spec/couchrest/attribute_protection_spec.rb +153 -0
- data/spec/couchrest/base_spec.rb +463 -0
- data/spec/couchrest/casted_model_spec.rb +424 -0
- data/spec/couchrest/casted_spec.rb +75 -0
- data/spec/couchrest/class_proxy_spec.rb +132 -0
- data/spec/couchrest/inherited_spec.rb +40 -0
- data/spec/couchrest/persistence_spec.rb +409 -0
- data/spec/couchrest/property_spec.rb +804 -0
- data/spec/couchrest/subclass_spec.rb +99 -0
- data/spec/couchrest/validations.rb +73 -0
- data/spec/couchrest/view_spec.rb +463 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/base.rb +139 -0
- data/spec/fixtures/more/article.rb +35 -0
- data/spec/fixtures/more/card.rb +17 -0
- data/spec/fixtures/more/cat.rb +19 -0
- data/spec/fixtures/more/course.rb +25 -0
- data/spec/fixtures/more/event.rb +8 -0
- data/spec/fixtures/more/invoice.rb +14 -0
- data/spec/fixtures/more/person.rb +9 -0
- data/spec/fixtures/more/question.rb +7 -0
- data/spec/fixtures/more/service.rb +10 -0
- data/spec/fixtures/more/user.rb +22 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +48 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +232 -0
data/LICENSE
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,320 @@
|
|
1
|
+
# CouchRest Model: CouchDB, close to shiny metal with rounded edges
|
2
|
+
|
3
|
+
CouchRest Models adds additional functionality to the standard CouchRest Document class such as
|
4
|
+
setting properties, callbacks, typecasting, and validations.
|
5
|
+
|
6
|
+
Originally called ExtendedDocument, the new Model structure uses ActiveModel, part of Rails 3,
|
7
|
+
for validations and callbacks.
|
8
|
+
|
9
|
+
If your project is still running Rails 2.3, you'll have to continue using ExtendedDocument as
|
10
|
+
it is not possible to load ActiveModel into programs that do not use ActiveSupport 3.0.
|
11
|
+
|
12
|
+
CouchRest Model only supports CouchDB 0.10.0 or newer.
|
13
|
+
|
14
|
+
## Install
|
15
|
+
|
16
|
+
$ sudo gem install couchrest_model
|
17
|
+
|
18
|
+
## General Usage
|
19
|
+
|
20
|
+
require 'couchrest_model'
|
21
|
+
|
22
|
+
class Cat < CouchRest::Model::Base
|
23
|
+
|
24
|
+
property :name, String
|
25
|
+
property :lives, Integer, :default => 9
|
26
|
+
|
27
|
+
property :nicknames, [String]
|
28
|
+
|
29
|
+
timestamps!
|
30
|
+
|
31
|
+
view_by :name
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
@cat = Cat.new(:name => 'Felix', :nicknames => ['so cute', 'sweet kitty'])
|
36
|
+
|
37
|
+
@cat.new? # true
|
38
|
+
@cat.save
|
39
|
+
|
40
|
+
@cat['name'] # "Felix"
|
41
|
+
|
42
|
+
@cat.nicknames << 'getoffdamntable'
|
43
|
+
|
44
|
+
@cat = Cat.new
|
45
|
+
@cat.update_attributes(:name => 'Felix', :random_text => 'feline')
|
46
|
+
@cat.new? # false
|
47
|
+
@cat.random_text # Raises error!
|
48
|
+
|
49
|
+
|
50
|
+
## Properties
|
51
|
+
|
52
|
+
Only attributes with a property definition will be stored be CouchRest Model (as opposed
|
53
|
+
to a normal CouchRest Document which will store everything). To help prevent confusion,
|
54
|
+
a property should be considered as the definition of an attribute. An attribute must be associated
|
55
|
+
with a property, but a property may not have any attributes associated if none have been set.
|
56
|
+
|
57
|
+
|
58
|
+
In its simplest form, a property
|
59
|
+
will only create a getter and setter passing all attribute data directly to the database. Assuming the attribute
|
60
|
+
provided responds to +to_json+, there will not be any problems saving it, but when loading the
|
61
|
+
data back it will either be a string, number, array, or hash:
|
62
|
+
|
63
|
+
class Cat < CouchRest::Model::Base
|
64
|
+
property :name
|
65
|
+
property :birthday
|
66
|
+
end
|
67
|
+
|
68
|
+
@cat = Cat.new(:name => 'Felix', :birthday => 2.years.ago)
|
69
|
+
@cat.name # 'Felix'
|
70
|
+
@cat.birthday.is_a?(Time) # True!
|
71
|
+
@cat.save
|
72
|
+
@cat = Cat.find(@cat.id)
|
73
|
+
@cat.name # 'Felix'
|
74
|
+
@cat.birthday.is_a?(Time) # False!
|
75
|
+
|
76
|
+
Properties create getters and setters similar to the following:
|
77
|
+
|
78
|
+
def name
|
79
|
+
read_attribute('name')
|
80
|
+
end
|
81
|
+
|
82
|
+
def name=(value)
|
83
|
+
write_attribute('name', value)
|
84
|
+
end
|
85
|
+
|
86
|
+
Properties can also have a type which
|
87
|
+
will be used for casting data retrieved from CouchDB when the attribute is set:
|
88
|
+
|
89
|
+
class Cat < CouchRest::Model::Base
|
90
|
+
property :name, String
|
91
|
+
property :last_fed_at, Time
|
92
|
+
end
|
93
|
+
|
94
|
+
@cat = Cat.new(:name => 'Felix', :last_fed_at => 10.minutes.ago)
|
95
|
+
@cat.last_fed_at.is_a?(Time) # True!
|
96
|
+
@cat.save
|
97
|
+
@cat = Cat.find(@cat.id)
|
98
|
+
@cat.last_fed_at < 20.minutes.ago # True!
|
99
|
+
|
100
|
+
|
101
|
+
Booleans or TrueClass will also create a getter with question mark at the end:
|
102
|
+
|
103
|
+
class Cat < CouchRest::Model::Base
|
104
|
+
property :awake, TrueClass, :default => true
|
105
|
+
end
|
106
|
+
|
107
|
+
@cat.awake? # true
|
108
|
+
|
109
|
+
Adding the +:default+ option will ensure the attribute always has a value.
|
110
|
+
|
111
|
+
Defining a property as read-only will mean that its value is set only when read from the
|
112
|
+
database and that it will not have a setter method. You can however update a read-only
|
113
|
+
attribute using the +write_attribute+ method:
|
114
|
+
|
115
|
+
class Cat < CouchRest::Model::Base
|
116
|
+
property :name, String
|
117
|
+
property :lives, Integer, :default => 9, :readonly => true
|
118
|
+
|
119
|
+
def fall_off_balcony!
|
120
|
+
write_attribute(:lives, lives - 1)
|
121
|
+
save
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
@cat = Cat.new(:name => "Felix")
|
126
|
+
@cat.fall_off_balcony!
|
127
|
+
@cat.lives # Now 8!
|
128
|
+
|
129
|
+
|
130
|
+
## Property Arrays
|
131
|
+
|
132
|
+
An attribute may also contain an array of data. CouchRest Model handles this, along
|
133
|
+
with casting, by defining the class of the child attributes inside an Array:
|
134
|
+
|
135
|
+
class Cat < CouchRest::Model::Base
|
136
|
+
property :name, String
|
137
|
+
property :nicknames, [String]
|
138
|
+
end
|
139
|
+
|
140
|
+
By default, the array will be ready to use from the moment the object as been instantiated:
|
141
|
+
|
142
|
+
@cat = Cat.new(:name => 'Fluffy')
|
143
|
+
@cat.nicknames << 'Buffy'
|
144
|
+
|
145
|
+
@cat.nicknames == ['Buffy']
|
146
|
+
|
147
|
+
When anything other than a string is set as the class of a property, the array will be converted
|
148
|
+
into special wrapper called a CastedArray. If the child objects respond to the 'casted_by' method
|
149
|
+
(such as those created with CastedModel, below) it will contain a reference to the parent.
|
150
|
+
|
151
|
+
## Casted Models
|
152
|
+
|
153
|
+
CouchRest Model allows you to take full advantage of CouchDB's ability to store complex
|
154
|
+
documents and retrieve them using the CastedModel module. Simply include the module in
|
155
|
+
a Hash (or other model that responds to the [] and []= methods) and set any properties
|
156
|
+
you'd like to use. For example:
|
157
|
+
|
158
|
+
class CatToy << Hash
|
159
|
+
include CouchRest::Model::CastedModel
|
160
|
+
|
161
|
+
property :name, String
|
162
|
+
property :purchased, Date
|
163
|
+
end
|
164
|
+
|
165
|
+
class Cat << CouchRest::Model::Base
|
166
|
+
property :name, String
|
167
|
+
property :toys, [CatToy]
|
168
|
+
end
|
169
|
+
|
170
|
+
@cat = Cat.new(:name => 'Felix', :toys => [{:name => 'mouse', :purchases => 1.month.ago}])
|
171
|
+
@cat.toys.first.class == CatToy
|
172
|
+
@cat.toys.first.name == 'mouse'
|
173
|
+
|
174
|
+
Additionally, any hashes sent to the property will automatically be converted:
|
175
|
+
|
176
|
+
@cat.toys << {:name => 'catnip ball'}
|
177
|
+
@cat.toys.last.is_a?(CatToy) # True!
|
178
|
+
|
179
|
+
Of course, to use your own classes they *must* be defined before the parent uses them otherwise
|
180
|
+
Ruby will bring up a missing constant error. To avoid this, or if you have a really simple array of data
|
181
|
+
you'd like to model, the latest version of CouchRest Model (> 1.0.0) supports creating
|
182
|
+
anonymous classes:
|
183
|
+
|
184
|
+
class Cat << CouchRest::Model::Base
|
185
|
+
property :name, String
|
186
|
+
|
187
|
+
property :toys do |toy|
|
188
|
+
toy.property :name, String
|
189
|
+
toy.property :rating, Integer
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
@cat = Cat.new(:name => 'Felix', :toys => [{:name => 'mouse', :rating => 3}, {:name => 'catnip ball', :rating => 5}])
|
194
|
+
@cat.toys.last.rating == 5
|
195
|
+
@cat.toys.last.name == 'catnip ball'
|
196
|
+
|
197
|
+
Using this method of anonymous classes will *only* create arrays of objects.
|
198
|
+
|
199
|
+
|
200
|
+
## Assocations
|
201
|
+
|
202
|
+
Two types at the moment:
|
203
|
+
|
204
|
+
belongs_to :person
|
205
|
+
|
206
|
+
collection_of :tags
|
207
|
+
|
208
|
+
TODO: Document properly!
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
## Validations
|
213
|
+
|
214
|
+
CouchRest Model automatically includes the new ActiveModel validations, so they should work just as the traditional Rails
|
215
|
+
validations. For more details, please see the ActiveModel::Validations documentation.
|
216
|
+
|
217
|
+
CouchRest Model adds the possibility to check the uniqueness of attributes using the @validates_uniqueness_of@ class method, for example:
|
218
|
+
|
219
|
+
class Person < CouchRest::Model::Base
|
220
|
+
property :title, String
|
221
|
+
|
222
|
+
validates_uniqueness_of :title
|
223
|
+
end
|
224
|
+
|
225
|
+
The uniqueness validation creates a new view for the attribute or uses one that already exists. You can
|
226
|
+
specify a different view using the +:view+ option, useful for when the @unique_id@ is specified and
|
227
|
+
you'd like to avoid the typical RestClient Conflict error:
|
228
|
+
|
229
|
+
unique_id :code
|
230
|
+
validates_uniqueness_of :code, :view => 'all'
|
231
|
+
|
232
|
+
Given that the uniqueness check performs a request to the database, it is also possible
|
233
|
+
to include a @:proxy@ parameter. This allows you to
|
234
|
+
call a method on the document and provide an alternate proxy object.
|
235
|
+
|
236
|
+
Examples:
|
237
|
+
|
238
|
+
# Same as not including proxy:
|
239
|
+
validates_uniqueness_of :title, :proxy => 'class'
|
240
|
+
|
241
|
+
# Person#company.people provides a proxy object for people
|
242
|
+
validates_uniqueness_of :title, :proxy => 'company.people'
|
243
|
+
|
244
|
+
|
245
|
+
A really interesting use of +:proxy+ and +:view+ together could be where
|
246
|
+
you'd like to ensure the ID is unique between several types of document. For example:
|
247
|
+
|
248
|
+
class Product < CouchRest::Model::Base
|
249
|
+
property :code
|
250
|
+
|
251
|
+
validates_uniqueness_of :code, :view => 'by_product_code'
|
252
|
+
|
253
|
+
view_by :product_code, :map => "
|
254
|
+
function(doc) {
|
255
|
+
if (doc['couchrest-type'] == 'Product' || doc['couchrest-type'] == 'Project') {
|
256
|
+
emit(doc['code']);
|
257
|
+
}
|
258
|
+
}
|
259
|
+
"
|
260
|
+
end
|
261
|
+
|
262
|
+
class Project < CouchRest::Model::Base
|
263
|
+
property :code
|
264
|
+
|
265
|
+
validates_uniqueness_of :code, :view => 'by_product_code', :proxy => 'Product'
|
266
|
+
end
|
267
|
+
|
268
|
+
Pretty cool!
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
## Notable Issues
|
273
|
+
|
274
|
+
CouchRest Model uses active_support for some of its internals. Ensure you have a stable active support gem installed
|
275
|
+
or at least 3.0.0.beta4.
|
276
|
+
|
277
|
+
JSON gem versions 1.4.X are kown to cause problems with stack overflows and general badness. Version 1.2.4 appears to work fine.
|
278
|
+
|
279
|
+
## Ruby on Rails
|
280
|
+
|
281
|
+
CouchRest Model is compatible with rails and provides some ActiveRecord-like methods.
|
282
|
+
|
283
|
+
The CouchRest companion rails project
|
284
|
+
[http://github.com/hpoydar/couchrest-rails](http://github.com/hpoydar/couchrest-rails) is great
|
285
|
+
for provided default connection details for your database. At the time of writting however it
|
286
|
+
does not provide explicit support for CouchRest Model.
|
287
|
+
|
288
|
+
CouchRest Model and the original CouchRest ExtendedDocument do not share the same namespace,
|
289
|
+
as such you should not have any problems using them both at the same time. This might
|
290
|
+
help with migrations.
|
291
|
+
|
292
|
+
|
293
|
+
### Rails 3.0
|
294
|
+
|
295
|
+
In your Gemfile require the gem with a simple line:
|
296
|
+
|
297
|
+
gem "couchrest_model"
|
298
|
+
|
299
|
+
|
300
|
+
## Testing
|
301
|
+
|
302
|
+
The most complete documentation is the spec/ directory. To validate your
|
303
|
+
CouchRest install, from the project root directory run `rake`, or `autotest`
|
304
|
+
(requires RSpec and optionally ZenTest for autotest support).
|
305
|
+
|
306
|
+
## Docs
|
307
|
+
|
308
|
+
API: [http://rdoc.info/projects/couchrest/couchrest_model](http://rdoc.info/projects/couchrest/couchrest_model)
|
309
|
+
|
310
|
+
Check the wiki for documentation and examples [http://wiki.github.com/couchrest/couchrest](http://wiki.github.com/couchrest/couchrest)
|
311
|
+
|
312
|
+
|
313
|
+
## Contact
|
314
|
+
|
315
|
+
Please post bugs, suggestions and patches to the bug tracker at [http://github.com/couchrest/couchrest/issues](http://github.com/couchrest/couchrest/issues).
|
316
|
+
|
317
|
+
Follow us on Twitter: [http://twitter.com/couchrest](http://twitter.com/couchrest)
|
318
|
+
|
319
|
+
Also, check [http://twitter.com/#search?q=%23couchrest](http://twitter.com/#search?q=%23couchrest)
|
320
|
+
|