couchrest_model 1.1.0.beta4 → 1.1.0.beta5
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/.gitignore +1 -0
- data/README.md +41 -546
- data/VERSION +1 -1
- data/couchrest_model.gemspec +2 -2
- data/{history.txt → history.md} +31 -20
- data/lib/couchrest/model/base.rb +2 -1
- data/lib/couchrest/model/configuration.rb +17 -1
- data/lib/couchrest/model/connection.rb +70 -0
- data/lib/couchrest/model/designs.rb +11 -0
- data/lib/couchrest/model/persistence.rb +0 -1
- data/lib/couchrest/model/properties.rb +1 -1
- data/lib/couchrest/model/proxyable.rb +28 -3
- data/lib/couchrest/railtie.rb +13 -4
- data/lib/couchrest_model.rb +4 -1
- data/spec/couchrest/configuration_spec.rb +1 -1
- data/spec/couchrest/connection_spec.rb +148 -0
- data/spec/couchrest/design_doc_spec.rb +2 -2
- data/spec/couchrest/designs/view_spec.rb +1 -1
- data/spec/couchrest/designs_spec.rb +14 -0
- data/spec/couchrest/property_spec.rb +5 -1
- data/spec/couchrest/proxyable_spec.rb +55 -21
- data/spec/couchrest/view_spec.rb +6 -2
- data/spec/fixtures/config/couchdb.yml +10 -0
- metadata +11 -8
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.0.
|
1
|
+
1.1.0.beta5
|
data/couchrest_model.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["J. Chris Anderson", "Matt Aimonetti", "Marcos Tapajos", "Will Leinweber", "Sam Lown"]
|
9
|
-
s.date = %q{2011-
|
9
|
+
s.date = %q{2011-04-29}
|
10
10
|
s.description = %q{CouchRest Model provides aditional features to the standard CouchRest Document class such as properties, view designs, associations, callbacks, typecasting and validations.}
|
11
11
|
s.email = %q{jchris@apache.org}
|
12
12
|
s.extra_rdoc_files = [
|
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
|
26
26
|
s.add_dependency(%q<couchrest>, "1.1.0.pre2")
|
27
27
|
s.add_dependency(%q<mime-types>, "~> 1.15")
|
28
|
-
s.add_dependency(%q<activemodel>, "~> 3.0.
|
28
|
+
s.add_dependency(%q<activemodel>, "~> 3.0.0")
|
29
29
|
s.add_dependency(%q<tzinfo>, "~> 0.3.22")
|
30
30
|
s.add_dependency(%q<railties>, "~> 3.0.0")
|
31
31
|
s.add_development_dependency(%q<rspec>, ">= 2.0.0")
|
data/{history.txt → history.md}
RENAMED
@@ -1,4 +1,15 @@
|
|
1
|
-
|
1
|
+
# CouchRest Model Change History
|
2
|
+
|
3
|
+
## 1.1.0.beta5 - 2011-04-30
|
4
|
+
|
5
|
+
* Major changes:
|
6
|
+
* Database auto configuration, with connection options!
|
7
|
+
* Changed default CouchRest Model type to 'type' to be more consistent with ActiveRecord's reserverd words we're all used to (sorry for the change again!!)
|
8
|
+
|
9
|
+
* Minor changes
|
10
|
+
* Added filter option to designs (Used with CouchDB _changes feeds)
|
11
|
+
|
12
|
+
## 1.1.0.beta4
|
2
13
|
|
3
14
|
* Major changes:
|
4
15
|
* Fast Dirty Tracking! Many thanks to @sobakasu (Andrew Williams)
|
@@ -10,11 +21,11 @@
|
|
10
21
|
* Added "auto_update_design_doc" configuration option.
|
11
22
|
* Using #descending on View object will automatically swap startkey with endkey.
|
12
23
|
|
13
|
-
|
24
|
+
## 1.1.0.beta3
|
14
25
|
|
15
26
|
* Removed
|
16
27
|
|
17
|
-
|
28
|
+
## 1.1.0.beta2
|
18
29
|
|
19
30
|
* Minor enhancements:
|
20
31
|
* Time handling improved in accordance with CouchRest 1.1.0. Always set to UTC.
|
@@ -23,7 +34,7 @@
|
|
23
34
|
* Unique Validation now supports scopes!
|
24
35
|
* Added support for #keys with list on Design View.
|
25
36
|
|
26
|
-
|
37
|
+
## 1.1.0.beta
|
27
38
|
|
28
39
|
* Epic enhancements:
|
29
40
|
* Added "View" object for dynamic view queries
|
@@ -37,7 +48,7 @@
|
|
37
48
|
* Numeric types can be casted from strings with leading or trailing whitespace (thanks chrisdurtschi)
|
38
49
|
* CollectionProxy no longer provided by default with simple views (pending deprication)
|
39
50
|
|
40
|
-
|
51
|
+
## CouchRest Model 1.0.0
|
41
52
|
|
42
53
|
* Major enhancements
|
43
54
|
* Support for configuration module and "model_type_key" option for overriding model's type key
|
@@ -56,7 +67,7 @@ Notes:
|
|
56
67
|
Until these have been resolved (if possible?!) they should not be included. See the
|
57
68
|
'active_model_attrs' if you'd like to test.
|
58
69
|
|
59
|
-
|
70
|
+
## CouchRest Model 1.0.0.beta8
|
60
71
|
|
61
72
|
* Major enhancements
|
62
73
|
* Added model generator
|
@@ -67,7 +78,7 @@ Notes:
|
|
67
78
|
* Parsing times without zone
|
68
79
|
* Using latest rspec (2.0.0.beta.19)
|
69
80
|
|
70
|
-
|
81
|
+
## CouchRest Model 1.0.0.beta7
|
71
82
|
|
72
83
|
* Major enhancements
|
73
84
|
* Renamed ExtendedDocument to CouchRest::Model
|
@@ -83,7 +94,7 @@ Notes:
|
|
83
94
|
* Removed support for auto_validate! and :length on properties
|
84
95
|
|
85
96
|
|
86
|
-
|
97
|
+
## 1.0.0.beta6
|
87
98
|
|
88
99
|
* Major enhancements
|
89
100
|
* Added support for anonymous CastedModels defined in Documents
|
@@ -97,12 +108,12 @@ Notes:
|
|
97
108
|
* Setting a property of type Array (or keyed hash) must be an array or an error will be raised.
|
98
109
|
* Now possible to set Array attribute from hash where keys determine order.
|
99
110
|
|
100
|
-
|
111
|
+
## 1.0.0.beta5
|
101
112
|
|
102
113
|
* Minor enhancements
|
103
114
|
* Added 'find' alias for 'get' for easier rails transition
|
104
115
|
|
105
|
-
|
116
|
+
## 1.0.0.beta3
|
106
117
|
|
107
118
|
* Minor enhancements
|
108
119
|
* Removed Validation by default, requires too many structure changes (FAIL)
|
@@ -112,12 +123,12 @@ Notes:
|
|
112
123
|
* Added support for setting type directly on property (Sam Lown)
|
113
124
|
|
114
125
|
|
115
|
-
|
126
|
+
## 1.0.0.beta2
|
116
127
|
|
117
128
|
* Minor enhancements
|
118
129
|
* Enable Validation by default and refactored location (Sam Lown)
|
119
130
|
|
120
|
-
|
131
|
+
## 1.0.0.beta
|
121
132
|
|
122
133
|
* Major enhancements
|
123
134
|
* Separated ExtendedDocument from main CouchRest gem (Sam Lown)
|
@@ -125,12 +136,12 @@ Notes:
|
|
125
136
|
* Minor enhancements
|
126
137
|
* active_support included by default
|
127
138
|
|
128
|
-
|
139
|
+
## 0.37
|
129
140
|
|
130
141
|
* Minor enhancements
|
131
142
|
* Added gemspec (needed for Bundler install) (Tapajós)
|
132
143
|
|
133
|
-
|
144
|
+
## 0.36
|
134
145
|
|
135
146
|
* Major enhancements
|
136
147
|
* Adds support for continuous replication (sauy7)
|
@@ -140,7 +151,7 @@ Notes:
|
|
140
151
|
* Minor enhancements
|
141
152
|
* Provide a description of the timeout error (John Wood)
|
142
153
|
|
143
|
-
|
154
|
+
## 0.35
|
144
155
|
|
145
156
|
* Major enhancements
|
146
157
|
* CouchRest::ExtendedDocument allow chaining the inherit class callback (Kenneth Kalmer) - http://github.com/couchrest/couchrest/issues#issue/8
|
@@ -156,7 +167,7 @@ Notes:
|
|
156
167
|
* Added an update_doc method to database to handle conflicts during atomic updates. (Pierre Larochelle)
|
157
168
|
* Bug fix: http://github.com/couchrest/couchrest/issues/#issue/2 (Luke Burton)
|
158
169
|
|
159
|
-
|
170
|
+
## 0.34
|
160
171
|
|
161
172
|
* Major enhancements
|
162
173
|
|
@@ -181,7 +192,7 @@ Notes:
|
|
181
192
|
* Fix Initialization of ExtendentDocument model shouldn't failed on a nil value in argument (deepj)
|
182
193
|
* Change to use Jeweler and Gemcutter (Marcos Tapajós)
|
183
194
|
|
184
|
-
|
195
|
+
## 0.33
|
185
196
|
|
186
197
|
* Major enhancements
|
187
198
|
|
@@ -196,7 +207,7 @@ Notes:
|
|
196
207
|
* Created a new abstraction layer for the REST API (Matt Aimonetti)
|
197
208
|
* Bug fix: made ExtendedDocument#all compatible with Couch 0.10 (tc)
|
198
209
|
|
199
|
-
|
210
|
+
## 0.32
|
200
211
|
|
201
212
|
* Major enhancements
|
202
213
|
|
@@ -211,7 +222,7 @@ Notes:
|
|
211
222
|
* Bug fix: the count method on the proxy collection was missing (Daniel Kirsch)
|
212
223
|
* Added #amount_pages to a paginated collection. (Matt Aimonetti)
|
213
224
|
|
214
|
-
|
225
|
+
## 0.31
|
215
226
|
|
216
227
|
* Major enhancements
|
217
228
|
|
@@ -225,7 +236,7 @@ Notes:
|
|
225
236
|
* Optimized ExtendedDocument.count to run about 3x faster (Matt Aimonetti)
|
226
237
|
* Added Float casting (Ryan Felton & Matt Aimonetti)
|
227
238
|
|
228
|
-
|
239
|
+
## 0.30
|
229
240
|
|
230
241
|
* Major enhancements
|
231
242
|
|
data/lib/couchrest/model/base.rb
CHANGED
@@ -5,9 +5,10 @@ module CouchRest
|
|
5
5
|
extend ActiveModel::Naming
|
6
6
|
|
7
7
|
include CouchRest::Model::Configuration
|
8
|
+
include CouchRest::Model::Connection
|
8
9
|
include CouchRest::Model::Persistence
|
9
10
|
include CouchRest::Model::Callbacks
|
10
|
-
include CouchRest::Model::DocumentQueries
|
11
|
+
include CouchRest::Model::DocumentQueries
|
11
12
|
include CouchRest::Model::Views
|
12
13
|
include CouchRest::Model::DesignDoc
|
13
14
|
include CouchRest::Model::ExtendedAttachments
|
@@ -11,11 +11,27 @@ module CouchRest
|
|
11
11
|
add_config :model_type_key
|
12
12
|
add_config :mass_assign_any_attribute
|
13
13
|
add_config :auto_update_design_doc
|
14
|
+
add_config :environment
|
15
|
+
add_config :connection
|
16
|
+
add_config :connection_config_file
|
14
17
|
|
15
18
|
configure do |config|
|
16
|
-
config.model_type_key = '
|
19
|
+
config.model_type_key = 'type' # was 'couchrest-type'
|
17
20
|
config.mass_assign_any_attribute = false
|
18
21
|
config.auto_update_design_doc = true
|
22
|
+
|
23
|
+
config.environment = :development
|
24
|
+
config.connection_config_file = File.join(Dir.pwd, 'config', 'couchdb.yml')
|
25
|
+
config.connection = {
|
26
|
+
:protocol => 'http',
|
27
|
+
:host => 'localhost',
|
28
|
+
:port => '5984',
|
29
|
+
:prefix => 'couchrest',
|
30
|
+
:suffix => nil,
|
31
|
+
:join => '_',
|
32
|
+
:username => nil,
|
33
|
+
:password => nil
|
34
|
+
}
|
19
35
|
end
|
20
36
|
end
|
21
37
|
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module CouchRest
|
2
|
+
module Model
|
3
|
+
module Connection
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def server
|
7
|
+
self.class.server
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Overwrite the normal use_database method so that a database
|
13
|
+
# name can be provided instead of a full connection.
|
14
|
+
def use_database(db)
|
15
|
+
@database = prepare_database(db)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Overwrite the default database method so that it always
|
19
|
+
# provides something from the configuration
|
20
|
+
def database
|
21
|
+
super || (@database ||= prepare_database)
|
22
|
+
end
|
23
|
+
|
24
|
+
def server
|
25
|
+
@server ||= CouchRest::Server.new(prepare_server_uri)
|
26
|
+
end
|
27
|
+
|
28
|
+
def prepare_database(db = nil)
|
29
|
+
unless db.is_a?(CouchRest::Database)
|
30
|
+
conf = connection_configuration
|
31
|
+
db = [conf[:prefix], db.to_s, conf[:suffix]].reject{|s| s.to_s.empty?}.join(conf[:join])
|
32
|
+
self.server.database!(db)
|
33
|
+
else
|
34
|
+
db
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def prepare_server_uri
|
41
|
+
conf = connection_configuration
|
42
|
+
userinfo = [conf[:username], conf[:password]].compact.join(':')
|
43
|
+
userinfo += '@' unless userinfo.empty?
|
44
|
+
"#{conf[:protocol]}://#{userinfo}#{conf[:host]}:#{conf[:port]}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def connection_configuration
|
48
|
+
@connection_configuration ||=
|
49
|
+
self.connection.update(
|
50
|
+
(load_connection_config_file[environment.to_sym] || {}).symbolize_keys
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
def load_connection_config_file
|
55
|
+
file = connection_config_file
|
56
|
+
connection_config_cache[file] ||=
|
57
|
+
(File.exists?(file) ?
|
58
|
+
YAML::load(ERB.new(IO.read(file)).result) :
|
59
|
+
{ }).symbolize_keys
|
60
|
+
end
|
61
|
+
|
62
|
+
def connection_config_cache
|
63
|
+
Thread.current[:connection_config_cache] ||= {}
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -65,6 +65,17 @@ module CouchRest
|
|
65
65
|
create_view_method(name)
|
66
66
|
end
|
67
67
|
|
68
|
+
# Really simple design function that allows a filter
|
69
|
+
# to be added. Filters are simple functions used when listening
|
70
|
+
# to the _changes feed.
|
71
|
+
#
|
72
|
+
# No methods are created here, the design is simply updated.
|
73
|
+
# See the CouchDB API for more information on how to use this.
|
74
|
+
def filter(name, function)
|
75
|
+
filters = (self.model.design_doc['filters'] ||= {})
|
76
|
+
filters[name.to_s] = function
|
77
|
+
end
|
78
|
+
|
68
79
|
def create_view_method(name)
|
69
80
|
model.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
70
81
|
def self.#{name}(opts = {})
|
@@ -140,7 +140,7 @@ module CouchRest
|
|
140
140
|
module ClassMethods
|
141
141
|
|
142
142
|
def property(name, *options, &block)
|
143
|
-
raise "Invalid property definition, '#{name}' already used for CouchRest Model type field" if name.to_s == model_type_key.to_s
|
143
|
+
raise "Invalid property definition, '#{name}' already used for CouchRest Model type field" if name.to_s == model_type_key.to_s && CouchRest::Model::Base >= self
|
144
144
|
opts = { }
|
145
145
|
type = options.shift
|
146
146
|
if type.class != Hash
|
@@ -4,8 +4,14 @@ module CouchRest
|
|
4
4
|
module Proxyable
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
+
def proxy_database
|
8
|
+
raise StandardError, "Please set the #proxy_database_method" if self.class.proxy_database_method.nil?
|
9
|
+
@proxy_database ||= self.class.prepare_database(self.send(self.class.proxy_database_method))
|
10
|
+
end
|
11
|
+
|
7
12
|
module ClassMethods
|
8
13
|
|
14
|
+
|
9
15
|
# Define a collection that will use the base model for the database connection
|
10
16
|
# details.
|
11
17
|
def proxy_for(assoc_name, options = {})
|
@@ -13,19 +19,19 @@ module CouchRest
|
|
13
19
|
options[:class_name] ||= assoc_name.to_s.singularize.camelize
|
14
20
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
15
21
|
def #{assoc_name}
|
16
|
-
unless respond_to?('#{db_method}')
|
17
|
-
raise "Missing ##{db_method} method for proxy"
|
18
|
-
end
|
19
22
|
@#{assoc_name} ||= CouchRest::Model::Proxyable::ModelProxy.new(::#{options[:class_name]}, self, self.class.to_s.underscore, #{db_method})
|
20
23
|
end
|
21
24
|
EOS
|
22
25
|
end
|
23
26
|
|
27
|
+
# Tell this model which other model to use a base for the database
|
28
|
+
# connection to use.
|
24
29
|
def proxied_by(model_name, options = {})
|
25
30
|
raise "Model can only be proxied once or ##{model_name} already defined" if method_defined?(model_name) || !proxy_owner_method.nil?
|
26
31
|
self.proxy_owner_method = model_name
|
27
32
|
attr_accessor :model_proxy
|
28
33
|
attr_accessor model_name
|
34
|
+
overwrite_database_reader(model_name)
|
29
35
|
end
|
30
36
|
|
31
37
|
# Define an a class variable accessor ready to be inherited and unique
|
@@ -34,6 +40,25 @@ module CouchRest
|
|
34
40
|
def proxy_owner_method=(name); @proxy_owner_method = name; end
|
35
41
|
def proxy_owner_method; @proxy_owner_method; end
|
36
42
|
|
43
|
+
# Define the name of a method to call to determine the name of
|
44
|
+
# the database to use as a proxy.
|
45
|
+
def proxy_database_method(name = nil)
|
46
|
+
@proxy_database_method = name if name
|
47
|
+
@proxy_database_method
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Ensure that no attempt is made to autoload a database connection
|
53
|
+
# by overwriting it to provide a basic accessor.
|
54
|
+
def overwrite_database_reader(model_name)
|
55
|
+
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
56
|
+
def self.database
|
57
|
+
raise StandardError, "#{self.to_s} database must be accessed via '#{model_name}' proxy"
|
58
|
+
end
|
59
|
+
EOS
|
60
|
+
end
|
61
|
+
|
37
62
|
end
|
38
63
|
|
39
64
|
class ModelProxy
|
data/lib/couchrest/railtie.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
require "rails"
|
2
2
|
require "active_model/railtie"
|
3
3
|
|
4
|
-
module
|
4
|
+
module CouchRest
|
5
5
|
# = Active Record Railtie
|
6
|
-
class
|
6
|
+
class ModelRailtie < Rails::Railtie
|
7
7
|
config.generators.orm :couchrest_model
|
8
8
|
config.generators.test_framework :test_unit, :fixture => false
|
9
|
+
|
10
|
+
initializer "couchrest_model.configure_default_connection" do
|
11
|
+
CouchRest::Model::Base.configure do |conf|
|
12
|
+
conf.environment = Rails.env
|
13
|
+
conf.connection_config_file = File.join(Rails.root, 'config', 'couchdb.yml')
|
14
|
+
conf.connection[:prefix] =
|
15
|
+
Rails.application.class.to_s.underscore.gsub(/\/.*/, '')
|
16
|
+
end
|
17
|
+
end
|
9
18
|
end
|
10
|
-
|
19
|
+
|
11
20
|
end
|
12
|
-
|
21
|
+
|
data/lib/couchrest_model.rb
CHANGED
@@ -46,6 +46,7 @@ require "couchrest/model/proxyable"
|
|
46
46
|
require "couchrest/model/collection"
|
47
47
|
require "couchrest/model/associations"
|
48
48
|
require "couchrest/model/configuration"
|
49
|
+
require "couchrest/model/connection"
|
49
50
|
require "couchrest/model/designs"
|
50
51
|
require "couchrest/model/designs/view"
|
51
52
|
|
@@ -61,4 +62,6 @@ require "couchrest/model/casted_model"
|
|
61
62
|
require "couchrest/model/base"
|
62
63
|
# Add rails support *after* everything has loaded
|
63
64
|
|
64
|
-
|
65
|
+
if defined?(Rails)
|
66
|
+
require "couchrest/railtie"
|
67
|
+
end
|