couch_potato 0.2.30 → 0.2.31
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/CHANGES.md +4 -0
- data/README.md +3 -2
- data/VERSION.yml +1 -1
- data/lib/core_ext/string.rb +1 -12
- data/lib/couch_potato/persistence/callbacks.rb +9 -5
- data/lib/couch_potato/persistence/simple_property.rb +1 -0
- data/lib/couch_potato/persistence.rb +3 -3
- data/lib/couch_potato/railtie.rb +2 -4
- data/lib/couch_potato/validation/with_validatable.rb +6 -1
- data/lib/couch_potato/view/base_view_spec.rb +9 -1
- data/spec/callbacks_spec.rb +2 -1
- data/spec/property_spec.rb +12 -4
- data/spec/unit/callbacks_spec.rb +41 -15
- data/spec/unit/string_spec.rb +0 -10
- metadata +5 -17
data/CHANGES.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
## Changes
|
|
2
2
|
|
|
3
|
+
### 0.2.31
|
|
4
|
+
* Removed requirement for validatable gem. Allows for using more uptodate versions of the library, or doesn't install it when you're using ActiveModel. (mattmatt)
|
|
5
|
+
* fixed callbacks of super classes were not run (langalex)
|
|
6
|
+
|
|
3
7
|
### 0.2.30
|
|
4
8
|
* pass in multiple keys when querying a view (langalex)
|
|
5
9
|
|
data/README.md
CHANGED
|
@@ -269,14 +269,15 @@ If you need access to the database in a callback: Couch Potato automatically ass
|
|
|
269
269
|
|
|
270
270
|
#### Attachments
|
|
271
271
|
|
|
272
|
-
There is basic attachment support: if you want to store any attachments set
|
|
272
|
+
There is basic attachment support: if you want to store any attachments set the _attachments attribute of a model before saving like this:
|
|
273
273
|
|
|
274
274
|
class User
|
|
275
275
|
include CouchPotato::Persistence
|
|
276
276
|
end
|
|
277
277
|
|
|
278
|
+
data = File.read('some_file.text') # or from upload
|
|
278
279
|
user = User.new
|
|
279
|
-
user._attachments = {'photo' => {'data' =>
|
|
280
|
+
user._attachments = {'photo' => {'data' => data, 'content_type' => 'image/png'}}
|
|
280
281
|
|
|
281
282
|
When saving this object an attachment with the name _photo_ will be uploaded into CouchDB. It will be available under the url of the user object + _/photo_. When loading the user at a later time you still have access to the _content_type_ and additionally to the _length_ of the attachment:
|
|
282
283
|
|
data/VERSION.yml
CHANGED
data/lib/core_ext/string.rb
CHANGED
|
@@ -5,15 +5,4 @@ module ActiveSupportMethods
|
|
|
5
5
|
end
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
|
-
String.send :include, ActiveSupportMethods unless String.new.respond_to?(:underscore)
|
|
9
|
-
|
|
10
|
-
class String
|
|
11
|
-
# inspired by http://github.com/rails/rails/blob/b600bf2cd728c90d50cc34456c944b2dfefe8c8d/activesupport/lib/active_support/inflector.rb
|
|
12
|
-
def snake_case(seperator = '/')
|
|
13
|
-
string = seperator == '::' ? dup : gsub(/::/, '/')
|
|
14
|
-
string.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
|
15
|
-
string.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
|
16
|
-
string.tr!("-", "_")
|
|
17
|
-
string.downcase
|
|
18
|
-
end
|
|
19
|
-
end
|
|
8
|
+
String.send :include, ActiveSupportMethods unless String.new.respond_to?(:underscore)
|
|
@@ -6,9 +6,8 @@ module CouchPotato
|
|
|
6
6
|
|
|
7
7
|
base.class_eval do
|
|
8
8
|
attr_accessor :skip_callbacks
|
|
9
|
-
def self.callbacks
|
|
10
|
-
@callbacks ||= {
|
|
11
|
-
@callbacks[self.name] ||= {:before_validation => [], :before_validation_on_create => [],
|
|
9
|
+
def self.callbacks #:nodoc:
|
|
10
|
+
@callbacks ||= {:before_validation => [], :before_validation_on_create => [],
|
|
12
11
|
:before_validation_on_update => [], :before_validation_on_save => [], :before_create => [],
|
|
13
12
|
:after_create => [], :before_update => [], :after_update => [],
|
|
14
13
|
:before_save => [], :after_save => [],
|
|
@@ -17,12 +16,17 @@ module CouchPotato
|
|
|
17
16
|
end
|
|
18
17
|
end
|
|
19
18
|
|
|
20
|
-
# Runs all callbacks on a model with the given name,
|
|
19
|
+
# Runs all callbacks on a model with the given name, e.g. :after_create.
|
|
21
20
|
#
|
|
22
21
|
# This method is called by the CouchPotato::Database object when saving/destroying an object
|
|
23
22
|
def run_callbacks(name)
|
|
24
23
|
return if skip_callbacks
|
|
25
|
-
|
|
24
|
+
|
|
25
|
+
callbacks = self.class.ancestors.map do |clazz|
|
|
26
|
+
clazz.callbacks[name] if clazz.respond_to?(:callbacks)
|
|
27
|
+
end.flatten.compact.uniq
|
|
28
|
+
|
|
29
|
+
callbacks.each do |callback|
|
|
26
30
|
if callback.is_a?(Symbol)
|
|
27
31
|
send callback
|
|
28
32
|
elsif callback.is_a?(Proc)
|
|
@@ -73,7 +73,7 @@ module CouchPotato
|
|
|
73
73
|
# book.attributes # => {:title => nil, :year => 2009}
|
|
74
74
|
def attributes
|
|
75
75
|
self.class.properties.inject({}) do |res, property|
|
|
76
|
-
property.
|
|
76
|
+
property.value(res, self)
|
|
77
77
|
res
|
|
78
78
|
end
|
|
79
79
|
end
|
|
@@ -96,8 +96,8 @@ module CouchPotato
|
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
def inspect
|
|
99
|
-
attributes_as_string = attributes.map {|attribute, value| "#{attribute}:
|
|
100
|
-
|
|
99
|
+
attributes_as_string = attributes.map {|attribute, value| "#{attribute}: #{value.inspect}"}.join(", ")
|
|
100
|
+
%Q{#<#{self.class} _id: "#{_id}", _rev: "#{_rev}", #{attributes_as_string}>}
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
end
|
data/lib/couch_potato/railtie.rb
CHANGED
|
@@ -6,10 +6,8 @@ module CouchPotato
|
|
|
6
6
|
CouchPotato::Config.database_name = YAML::load(File.read(Rails.root.join('config/couchdb.yml')))[Rails.env]
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
if Rails
|
|
10
|
-
class Railtie < Rails::Railtie
|
|
11
|
-
railtie_name :couch_potato
|
|
12
|
-
|
|
9
|
+
if defined?(::Rails::Railtie)
|
|
10
|
+
class Railtie < ::Rails::Railtie
|
|
13
11
|
config.after_initialize do |app|
|
|
14
12
|
CouchPotato.rails_init
|
|
15
13
|
end
|
|
@@ -2,7 +2,12 @@ module CouchPotato
|
|
|
2
2
|
module Validation
|
|
3
3
|
module WithValidatable
|
|
4
4
|
def self.included(base)
|
|
5
|
-
|
|
5
|
+
begin
|
|
6
|
+
require 'validatable'
|
|
7
|
+
rescue LoadError
|
|
8
|
+
puts "Please install the gem validatable using 'gem install validatable'"
|
|
9
|
+
raise
|
|
10
|
+
end
|
|
6
11
|
base.send :include, ::Validatable
|
|
7
12
|
base.class_eval do
|
|
8
13
|
# Override the validate method to first run before_validation callback
|
|
@@ -8,7 +8,7 @@ module CouchPotato
|
|
|
8
8
|
normalized_view_parameters = normalize_view_parameters view_parameters
|
|
9
9
|
assert_valid_view_parameters normalized_view_parameters
|
|
10
10
|
@klass = klass
|
|
11
|
-
@design_document = klass.to_s
|
|
11
|
+
@design_document = translate_to_design_doc_name(klass.to_s)
|
|
12
12
|
@view_name = view_name
|
|
13
13
|
@options = options
|
|
14
14
|
@view_parameters = {}
|
|
@@ -56,6 +56,14 @@ module CouchPotato
|
|
|
56
56
|
def valid_view_parameters
|
|
57
57
|
%w(key keys startkey startkey_docid endkey endkey_docid limit stale descending skip group group_level reduce include_docs inclusive_end)
|
|
58
58
|
end
|
|
59
|
+
|
|
60
|
+
def translate_to_design_doc_name(klass_name)
|
|
61
|
+
klass_name = klass_name.dup
|
|
62
|
+
klass_name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
|
63
|
+
klass_name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
|
64
|
+
klass_name.tr!("-", "_")
|
|
65
|
+
klass_name.downcase
|
|
66
|
+
end
|
|
59
67
|
end
|
|
60
68
|
end
|
|
61
69
|
end
|
data/spec/callbacks_spec.rb
CHANGED
|
@@ -55,9 +55,10 @@ describe "multiple callbacks at once" do
|
|
|
55
55
|
self.eaten_apple = true
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
|
+
|
|
58
59
|
it "should run all callback methods given to the callback method call" do
|
|
59
60
|
monkey = Monkey.new
|
|
60
|
-
|
|
61
|
+
monkey.run_callbacks :before_create
|
|
61
62
|
monkey.eaten_banana.should be_true
|
|
62
63
|
monkey.eaten_apple.should be_true
|
|
63
64
|
end
|
data/spec/property_spec.rb
CHANGED
|
@@ -287,21 +287,29 @@ describe 'properties' do
|
|
|
287
287
|
end
|
|
288
288
|
|
|
289
289
|
it "should include the normal persistent variables" do
|
|
290
|
-
comment.inspect.should include(
|
|
290
|
+
comment.inspect.should include('title: "title"')
|
|
291
291
|
end
|
|
292
292
|
|
|
293
293
|
it "should include the id" do
|
|
294
|
-
comment.inspect.should include(
|
|
294
|
+
comment.inspect.should include(%Q{_id: "123456abcdef",})
|
|
295
295
|
end
|
|
296
296
|
|
|
297
297
|
it "should include the revision" do
|
|
298
|
-
comment.inspect.should include(
|
|
298
|
+
comment.inspect.should include(%Q{_rev: "1-654321fedcba",})
|
|
299
299
|
end
|
|
300
300
|
|
|
301
301
|
it "should return a complete string" do
|
|
302
302
|
# stub to work around (un)sorted hash on different rubies
|
|
303
303
|
comment.stub!(:attributes).and_return([['created_at', ''], ['updated_at', ''], ['title', 'title']])
|
|
304
|
-
comment.inspect.should ==
|
|
304
|
+
comment.inspect.should == %Q{#<Comment _id: "123456abcdef", _rev: "1-654321fedcba", created_at: "", updated_at: "", title: "title">}
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
it "should include complex datatypes fully inspected" do
|
|
308
|
+
comment.title = {'en' => 'Blog post'}
|
|
309
|
+
comment.inspect.should include('title: {"en"=>"Blog post"}')
|
|
310
|
+
|
|
311
|
+
comment.title = nil
|
|
312
|
+
comment.inspect.should include('title: nil')
|
|
305
313
|
end
|
|
306
314
|
end
|
|
307
315
|
end
|
data/spec/unit/callbacks_spec.rb
CHANGED
|
@@ -1,32 +1,58 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
describe '
|
|
4
|
-
before(:each) do
|
|
5
|
-
@tree = Tree.new(:leaf_count => 1, :root_count => 1)
|
|
6
|
-
end
|
|
7
|
-
|
|
3
|
+
describe 'callbacks' do
|
|
8
4
|
class Tree
|
|
9
5
|
include CouchPotato::Persistence
|
|
10
6
|
|
|
11
|
-
before_validation :
|
|
7
|
+
before_validation :grow_leaf, lambda {|tree| tree.root_count ||= 0; tree.root_count += 1 }
|
|
12
8
|
|
|
13
9
|
property :leaf_count
|
|
14
10
|
property :root_count
|
|
15
11
|
|
|
16
|
-
def
|
|
12
|
+
def grow_leaf
|
|
13
|
+
self.leaf_count ||= 0
|
|
17
14
|
self.leaf_count += 1
|
|
18
15
|
end
|
|
19
16
|
end
|
|
17
|
+
|
|
18
|
+
class AppleTree < Tree
|
|
19
|
+
attr_accessor :watered
|
|
20
|
+
|
|
21
|
+
before_validation :water
|
|
22
|
+
|
|
23
|
+
def water
|
|
24
|
+
self.watered = true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def watered?
|
|
28
|
+
watered
|
|
29
|
+
end
|
|
30
|
+
end
|
|
20
31
|
|
|
21
|
-
it "should call
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
it "should call a method when validated" do
|
|
33
|
+
tree = Tree.new(:leaf_count => 1, :root_count => 1)
|
|
34
|
+
tree.valid?
|
|
35
|
+
tree.leaf_count.should == 2
|
|
25
36
|
end
|
|
26
37
|
|
|
27
|
-
it "should call lambda when validated" do
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
it "should call a lambda when validated" do
|
|
39
|
+
tree = Tree.new(:leaf_count => 1, :root_count => 1)
|
|
40
|
+
tree.valid?
|
|
41
|
+
tree.root_count.should == 2
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'inheritance' do
|
|
45
|
+
it "should call the callbacks of the super class" do
|
|
46
|
+
tree = AppleTree.new :leaf_count => 1
|
|
47
|
+
tree.valid?
|
|
48
|
+
tree.leaf_count.should == 2
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "should call the callbacks of the child class" do
|
|
52
|
+
tree = AppleTree.new :leaf_count => 1
|
|
53
|
+
tree.valid?
|
|
54
|
+
tree.should be_watered
|
|
55
|
+
end
|
|
31
56
|
end
|
|
57
|
+
|
|
32
58
|
end
|
data/spec/unit/string_spec.rb
CHANGED
|
@@ -5,13 +5,3 @@ describe String, 'camelize' do
|
|
|
5
5
|
'my_string'.camelize.should == 'MyString'
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
|
-
|
|
9
|
-
describe String, 'snake_case' do
|
|
10
|
-
it "should snake_case a string" do
|
|
11
|
-
'MyString'.snake_case.should == 'my_string'
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "should snake_case a string using a custom separator" do
|
|
15
|
-
'My::String'.snake_case('::').should == 'my::string'
|
|
16
|
-
end
|
|
17
|
-
end
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
7
|
- 2
|
|
8
|
-
-
|
|
9
|
-
version: 0.2.
|
|
8
|
+
- 31
|
|
9
|
+
version: 0.2.31
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Alexander Lang
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2010-04
|
|
17
|
+
date: 2010-05-04 00:00:00 +02:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies:
|
|
20
20
|
- !ruby/object:Gem::Dependency
|
|
@@ -29,22 +29,10 @@ dependencies:
|
|
|
29
29
|
version: "0"
|
|
30
30
|
type: :runtime
|
|
31
31
|
version_requirements: *id001
|
|
32
|
-
- !ruby/object:Gem::Dependency
|
|
33
|
-
name: validatable
|
|
34
|
-
prerelease: false
|
|
35
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - ">="
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
segments:
|
|
40
|
-
- 0
|
|
41
|
-
version: "0"
|
|
42
|
-
type: :runtime
|
|
43
|
-
version_requirements: *id002
|
|
44
32
|
- !ruby/object:Gem::Dependency
|
|
45
33
|
name: couchrest
|
|
46
34
|
prerelease: false
|
|
47
|
-
requirement: &
|
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
48
36
|
requirements:
|
|
49
37
|
- - ">="
|
|
50
38
|
- !ruby/object:Gem::Version
|
|
@@ -53,7 +41,7 @@ dependencies:
|
|
|
53
41
|
- 24
|
|
54
42
|
version: "0.24"
|
|
55
43
|
type: :runtime
|
|
56
|
-
version_requirements: *
|
|
44
|
+
version_requirements: *id002
|
|
57
45
|
description: Ruby persistence layer for CouchDB
|
|
58
46
|
email: alex@upstream-berlin.com
|
|
59
47
|
executables: []
|