redis_orm 0.1
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 +19 -0
- data/Manifest +23 -0
- data/README.md +332 -0
- data/Rakefile +32 -0
- data/lib/redis_orm.rb +20 -0
- data/lib/redis_orm/active_model_behavior.rb +20 -0
- data/lib/redis_orm/associations/belongs_to.rb +48 -0
- data/lib/redis_orm/associations/has_many.rb +54 -0
- data/lib/redis_orm/associations/has_many_proxy.rb +118 -0
- data/lib/redis_orm/associations/has_one.rb +52 -0
- data/lib/redis_orm/redis_orm.rb +502 -0
- data/redis_orm.gemspec +42 -0
- data/test/associations_test.rb +226 -0
- data/test/basic_functionality_test.rb +166 -0
- data/test/callbacks_test.rb +140 -0
- data/test/changes_array_test.rb +47 -0
- data/test/dynamic_finders_test.rb +89 -0
- data/test/exceptions_test.rb +63 -0
- data/test/has_one_has_many_test.rb +75 -0
- data/test/indices_test.rb +76 -0
- data/test/options_test.rb +186 -0
- data/test/redis.conf +417 -0
- data/test/validations_test.rb +49 -0
- metadata +138 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class User < RedisOrm::Base
|
5
|
+
property :name, String
|
6
|
+
property :age, Integer, :default => 26
|
7
|
+
property :gender, RedisOrm::Boolean, :default => true
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "check associations" do
|
11
|
+
before(:all) do
|
12
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
13
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
14
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
15
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
16
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
17
|
+
end
|
18
|
+
|
19
|
+
before(:each) do
|
20
|
+
$redis.flushall if $redis
|
21
|
+
end
|
22
|
+
|
23
|
+
after(:each) do
|
24
|
+
$redis.flushall if $redis
|
25
|
+
end
|
26
|
+
|
27
|
+
after(:all) do
|
28
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return correct _changes array" do
|
32
|
+
user = User.new :name => "german"
|
33
|
+
|
34
|
+
user.name_changes.should == ["german"]
|
35
|
+
user.save
|
36
|
+
user.name_changes.should == ["german"]
|
37
|
+
user.name = "germaninthetown"
|
38
|
+
user.name_changes.should == ["german", "germaninthetown"]
|
39
|
+
user.save
|
40
|
+
|
41
|
+
user = User.first
|
42
|
+
user.name.should == "germaninthetown"
|
43
|
+
user.name_changes.should == ["germaninthetown"]
|
44
|
+
user.name = "german"
|
45
|
+
user.name_changes.should == ["germaninthetown", "german"]
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class User < RedisOrm::Base
|
5
|
+
property :first_name, String
|
6
|
+
property :last_name, String
|
7
|
+
|
8
|
+
index :first_name, :unique => true
|
9
|
+
index :last_name, :unique => true
|
10
|
+
index [:first_name, :last_name], :unique => false
|
11
|
+
end
|
12
|
+
|
13
|
+
class CustomUser < RedisOrm::Base
|
14
|
+
property :first_name, String
|
15
|
+
property :last_name, String
|
16
|
+
|
17
|
+
index :first_name, :unique => false
|
18
|
+
index :last_name, :unique => false
|
19
|
+
index [:first_name, :last_name], :unique => true
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "check associations" do
|
23
|
+
before(:all) do
|
24
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
25
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
26
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
27
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
28
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
29
|
+
end
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
$redis.flushall if $redis
|
33
|
+
end
|
34
|
+
|
35
|
+
after(:each) do
|
36
|
+
$redis.flushall if $redis
|
37
|
+
end
|
38
|
+
|
39
|
+
after(:all) do
|
40
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should create and use indexes to implement dynamic finders" do
|
44
|
+
user1 = User.new
|
45
|
+
user1.first_name = "Dmitrii"
|
46
|
+
user1.last_name = "Samoilov"
|
47
|
+
user1.save
|
48
|
+
|
49
|
+
User.find_by_first_name("John").should == nil
|
50
|
+
|
51
|
+
user = User.find_by_first_name "Dmitrii"
|
52
|
+
user.id.should == user1.id
|
53
|
+
|
54
|
+
User.find_all_by_first_name("Dmitrii").size.should == 1
|
55
|
+
|
56
|
+
user = User.find_by_first_name_and_last_name('Dmitrii', 'Samoilov')
|
57
|
+
user.should be
|
58
|
+
user.id.should == user1.id
|
59
|
+
|
60
|
+
User.find_all_by_first_name_and_last_name('Dmitrii', 'Samoilov').size.should == 1
|
61
|
+
|
62
|
+
lambda{User.find_all_by_last_name_and_first_name('Samoilov', 'Dmitrii')}.should raise_error
|
63
|
+
|
64
|
+
lambda{User.find_by_first_name_and_cast_name('Dmitrii', 'Samoilov')}.should raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should create and use indexes to implement dynamic finders" do
|
68
|
+
user1 = CustomUser.new
|
69
|
+
user1.first_name = "Dmitrii"
|
70
|
+
user1.last_name = "Samoilov"
|
71
|
+
user1.save
|
72
|
+
|
73
|
+
user2 = CustomUser.new
|
74
|
+
user2.first_name = "Dmitrii"
|
75
|
+
user2.last_name = "Nabaldyan"
|
76
|
+
user2.save
|
77
|
+
|
78
|
+
user = CustomUser.find_by_first_name "Dmitrii"
|
79
|
+
user.id.should == user1.id
|
80
|
+
|
81
|
+
CustomUser.find_by_last_name("Krassovkin").should == nil
|
82
|
+
|
83
|
+
CustomUser.find_all_by_first_name("Dmitrii").size.should == 2
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should properly delete indices when record was deleted" do
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class User < RedisOrm::Base
|
5
|
+
property :name, String
|
6
|
+
property :age, Integer
|
7
|
+
property :created_at, Time
|
8
|
+
|
9
|
+
has_one :profile
|
10
|
+
end
|
11
|
+
|
12
|
+
class Profile < RedisOrm::Base
|
13
|
+
property :title, String
|
14
|
+
|
15
|
+
belongs_to :user
|
16
|
+
end
|
17
|
+
|
18
|
+
class Jigsaw < RedisOrm::Base
|
19
|
+
property :title, String
|
20
|
+
|
21
|
+
belongs_to :user
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "exceptions test" do
|
25
|
+
before(:all) do
|
26
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
27
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
28
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
29
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
30
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
31
|
+
end
|
32
|
+
|
33
|
+
before(:each) do
|
34
|
+
$redis.flushall if $redis
|
35
|
+
end
|
36
|
+
|
37
|
+
after(:each) do
|
38
|
+
$redis.flushall if $redis
|
39
|
+
end
|
40
|
+
|
41
|
+
after(:all) do
|
42
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should raise an exception if association is provided with improper class" do
|
46
|
+
User.count.should == 0
|
47
|
+
|
48
|
+
user = User.new
|
49
|
+
user.name = "german"
|
50
|
+
user.save
|
51
|
+
|
52
|
+
user.should be
|
53
|
+
user.name.should == "german"
|
54
|
+
User.count.should == 1
|
55
|
+
|
56
|
+
jigsaw = Jigsaw.new
|
57
|
+
jigsaw.title = "123"
|
58
|
+
jigsaw.save
|
59
|
+
|
60
|
+
# RedisOrm::TypeMismatchError
|
61
|
+
lambda { user.profile = jigsaw }.should raise_error
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class Profile < RedisOrm::Base
|
5
|
+
property :name, String
|
6
|
+
|
7
|
+
has_one :location
|
8
|
+
end
|
9
|
+
|
10
|
+
class Location < RedisOrm::Base
|
11
|
+
property :coordinates, String
|
12
|
+
|
13
|
+
has_many :profiles
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "check associations" do
|
17
|
+
before(:all) do
|
18
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
19
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
20
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
21
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
22
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
23
|
+
end
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
$redis.flushall if $redis
|
27
|
+
end
|
28
|
+
|
29
|
+
after(:each) do
|
30
|
+
$redis.flushall if $redis
|
31
|
+
end
|
32
|
+
|
33
|
+
after(:all) do
|
34
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should save associations properly" do
|
38
|
+
@profile = Profile.new
|
39
|
+
@profile.name = "my profile"
|
40
|
+
@profile.save
|
41
|
+
|
42
|
+
@profile.should be
|
43
|
+
@profile.name.should == "my profile"
|
44
|
+
|
45
|
+
@location1 = Location.new
|
46
|
+
@location1.coordinates = "44.343456345 56.23341432"
|
47
|
+
@location1.save
|
48
|
+
@location1.should be
|
49
|
+
@location1.coordinates.should == "44.343456345 56.23341432"
|
50
|
+
|
51
|
+
@profile.location = @location1
|
52
|
+
|
53
|
+
@profile.location.should be
|
54
|
+
@profile.location.id.should == @location1.id
|
55
|
+
|
56
|
+
@location1.profiles.size.should == 1
|
57
|
+
@location1.profiles.first.id.should == @profile.id
|
58
|
+
|
59
|
+
# check second profile
|
60
|
+
@profile2 = Profile.new
|
61
|
+
@profile2.name = "someone else's profile"
|
62
|
+
@profile2.save
|
63
|
+
|
64
|
+
@profile2.should be
|
65
|
+
@profile2.name.should == "someone else's profile"
|
66
|
+
|
67
|
+
@profile2.location = @location1
|
68
|
+
|
69
|
+
@profile2.location.should be
|
70
|
+
@profile2.location.id.should == @location1.id
|
71
|
+
|
72
|
+
@location1.profiles.size.should == 2
|
73
|
+
@location1.profiles.collect{|p| p.id}.sort.should == [@profile.id, @profile2.id].sort
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class User < RedisOrm::Base
|
5
|
+
property :first_name, String
|
6
|
+
property :last_name, String
|
7
|
+
|
8
|
+
index :first_name
|
9
|
+
index :last_name
|
10
|
+
index [:first_name, :last_name]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "check indices" do
|
14
|
+
before(:all) do
|
15
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
16
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
17
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
18
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
19
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
20
|
+
end
|
21
|
+
|
22
|
+
before(:each) do
|
23
|
+
$redis.flushall if $redis
|
24
|
+
end
|
25
|
+
|
26
|
+
after(:each) do
|
27
|
+
$redis.flushall if $redis
|
28
|
+
end
|
29
|
+
|
30
|
+
after(:all) do
|
31
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should change index accordingly to the changes in the model" do
|
35
|
+
user = User.new :first_name => "Robert", :last_name => "Pirsig"
|
36
|
+
user.save
|
37
|
+
|
38
|
+
u = User.find_by_first_name("Robert")
|
39
|
+
u.id.should == user.id
|
40
|
+
|
41
|
+
u = User.find_by_first_name_and_last_name("Robert", "Pirsig")
|
42
|
+
u.id.should == user.id
|
43
|
+
|
44
|
+
u.first_name = "Chris"
|
45
|
+
u.save
|
46
|
+
|
47
|
+
User.find_by_first_name("Robert").should == nil
|
48
|
+
|
49
|
+
User.find_by_first_name_and_last_name("Robert", "Pirsig").should == nil
|
50
|
+
|
51
|
+
User.find_by_first_name("Chris").id.should == user.id
|
52
|
+
User.find_by_last_name("Pirsig").id.should == user.id
|
53
|
+
User.find_by_first_name_and_last_name("Chris", "Pirsig").id.should == user.id
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should change index accordingly to the changes in the model (test #update_attributes method)" do
|
57
|
+
user = User.new :first_name => "Robert", :last_name => "Pirsig"
|
58
|
+
user.save
|
59
|
+
|
60
|
+
u = User.find_by_first_name("Robert")
|
61
|
+
u.id.should == user.id
|
62
|
+
|
63
|
+
u = User.find_by_first_name_and_last_name("Robert", "Pirsig")
|
64
|
+
u.id.should == user.id
|
65
|
+
|
66
|
+
u.update_attributes :first_name => "Christofer", :last_name => "Robin"
|
67
|
+
|
68
|
+
User.find_by_first_name("Robert").should == nil
|
69
|
+
User.find_by_last_name("Pirsig").should == nil
|
70
|
+
User.find_by_first_name_and_last_name("Robert", "Pirsig").should == nil
|
71
|
+
|
72
|
+
User.find_by_first_name("Christofer").id.should == user.id
|
73
|
+
User.find_by_last_name("Robin").id.should == user.id
|
74
|
+
User.find_by_first_name_and_last_name("Christofer", "Robin").id.should == user.id
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require File.dirname(File.expand_path(__FILE__)) + '/../lib/redis_orm.rb'
|
3
|
+
|
4
|
+
class Album < RedisOrm::Base
|
5
|
+
property :title, String
|
6
|
+
|
7
|
+
has_one :photo, :as => :front_photo
|
8
|
+
has_many :photos, :dependant => :destroy
|
9
|
+
end
|
10
|
+
|
11
|
+
class Category < RedisOrm::Base
|
12
|
+
property :title, String
|
13
|
+
|
14
|
+
has_many :photos, :dependant => :nullify
|
15
|
+
end
|
16
|
+
|
17
|
+
class Photo < RedisOrm::Base
|
18
|
+
property :image, String
|
19
|
+
|
20
|
+
belongs_to :album
|
21
|
+
belongs_to :user
|
22
|
+
belongs_to :category
|
23
|
+
end
|
24
|
+
|
25
|
+
class User < RedisOrm::Base
|
26
|
+
property :name, String
|
27
|
+
|
28
|
+
has_one :photo, :dependant => :destroy
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "test options" do
|
32
|
+
before(:all) do
|
33
|
+
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
|
34
|
+
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
|
35
|
+
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
|
36
|
+
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
|
37
|
+
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
|
38
|
+
end
|
39
|
+
|
40
|
+
after(:all) do
|
41
|
+
Process.kill 9, $redis_pid.to_i if $redis_pid
|
42
|
+
end
|
43
|
+
|
44
|
+
after(:each) do
|
45
|
+
$redis.flushall if $redis
|
46
|
+
end
|
47
|
+
|
48
|
+
before(:each) do
|
49
|
+
$redis.flushall if $redis
|
50
|
+
@album = Album.new
|
51
|
+
@album.title = "my 1st album"
|
52
|
+
@album.save
|
53
|
+
|
54
|
+
@album.should be
|
55
|
+
@album.title.should == "my 1st album"
|
56
|
+
|
57
|
+
@photo1 = Photo.new
|
58
|
+
@photo1.image = "facepalm.jpg"
|
59
|
+
@photo1.save
|
60
|
+
@photo1.should be
|
61
|
+
@photo1.image.should == "facepalm.jpg"
|
62
|
+
|
63
|
+
@photo2 = Photo.new
|
64
|
+
@photo2.image = "boobs.jpg"
|
65
|
+
@photo2.save
|
66
|
+
@photo2.should be
|
67
|
+
@photo2.image.should == "boobs.jpg"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should return correct array when :limit and :offset options are provided" do
|
71
|
+
@album.photos.count.should == 0
|
72
|
+
|
73
|
+
@album.photos.all(:limit => 2, :offset => 0).should == []
|
74
|
+
|
75
|
+
@album.photos << [@photo1, @photo2]
|
76
|
+
|
77
|
+
@album.photos.all(:limit => 0, :offset => 0).should == []
|
78
|
+
@album.photos.all(:limit => 1, :offset => 0).size.should == 1
|
79
|
+
@album.photos.all(:limit => 2, :offset => 0).size.should == 2 #[@photo1, @photo2]
|
80
|
+
|
81
|
+
@album.photos.all(:limit => 0, :offset => 0).should == []
|
82
|
+
@album.photos.all(:limit => 1, :offset => 1).size.should == 1 # [@photo2]
|
83
|
+
@album.photos.all(:limit => 2, :offset => 2).should == []
|
84
|
+
|
85
|
+
@album.photos.find(:all, :limit => 1, :offset => 1).size.should == 1
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return correct array when :order option is provided" do
|
89
|
+
Photo.all(:order => "asc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
|
90
|
+
Photo.all(:order => "desc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
|
91
|
+
|
92
|
+
Photo.all(:order => "asc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
|
93
|
+
Photo.all(:order => "desc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
|
94
|
+
|
95
|
+
Photo.all(:order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
|
96
|
+
Photo.all(:order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
|
97
|
+
|
98
|
+
# testing #find method
|
99
|
+
Photo.find(:order => "asc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
|
100
|
+
Photo.find(:order => "desc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
|
101
|
+
|
102
|
+
Photo.find(:order => "asc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
|
103
|
+
Photo.find(:order => "desc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
|
104
|
+
|
105
|
+
Photo.find(:order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
|
106
|
+
Photo.find(:order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
|
107
|
+
|
108
|
+
@album.photos.count.should == 0
|
109
|
+
@album.photos.all(:limit => 2, :offset => 0).should == []
|
110
|
+
@album.photos << @photo2
|
111
|
+
@album.photos << @photo1
|
112
|
+
|
113
|
+
@album.photos.all(:order => "asc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
|
114
|
+
@album.photos.all(:order => "desc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
|
115
|
+
@album.photos.all(:order => "asc", :limit => 1).map{|p| p.id}.should == [@photo2.id]
|
116
|
+
@album.photos.all(:order => "desc", :limit => 1).map{|p| p.id}.should == [@photo1.id]
|
117
|
+
@album.photos.all(:order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
|
118
|
+
@album.photos.all(:order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
|
119
|
+
|
120
|
+
@album.photos.find(:all, :order => "asc").map{|p| p.id}.should == [@photo2.id, @photo1.id]
|
121
|
+
@album.photos.find(:all, :order => "desc").map{|p| p.id}.should == [@photo1.id, @photo2.id]
|
122
|
+
@album.photos.find(:first, :order => "asc").map{|p| p.id}.should == [@photo2.id]
|
123
|
+
@album.photos.find(:first, :order => "desc").map{|p| p.id}.should == [@photo1.id]
|
124
|
+
@album.photos.find(:all, :order => "asc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo1.id]
|
125
|
+
@album.photos.find(:all, :order => "desc", :limit => 1, :offset => 1).map{|p| p.id}.should == [@photo2.id]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should delete associated records when :dependant => :destroy in *has_many* assoc" do
|
129
|
+
@album.photos << [@photo1, @photo2]
|
130
|
+
|
131
|
+
@album.photos.count.should == 2
|
132
|
+
|
133
|
+
Photo.count.should == 2
|
134
|
+
@album.destroy
|
135
|
+
Photo.count.should == 0
|
136
|
+
Album.count.should == 0
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should *NOT* delete associated records when :dependant => :nullify or empty in *has_many* assoc" do
|
140
|
+
Photo.count.should == 2
|
141
|
+
|
142
|
+
category = Category.new
|
143
|
+
category.title = "cats"
|
144
|
+
category.save
|
145
|
+
|
146
|
+
Category.count.should == 1
|
147
|
+
|
148
|
+
category.photos << [@photo1, @photo2]
|
149
|
+
category.photos.count.should == 2
|
150
|
+
|
151
|
+
category.destroy
|
152
|
+
|
153
|
+
Photo.count.should == 2
|
154
|
+
Category.count.should == 0
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should delete associated records when :dependant => :destroy and leave them otherwise in *has_one* assoc" do
|
158
|
+
user = User.new
|
159
|
+
user.name = "Dmitrii Samoilov"
|
160
|
+
user.save
|
161
|
+
user.should be
|
162
|
+
|
163
|
+
user.photo = @photo1
|
164
|
+
|
165
|
+
user.photo.id.should == @photo1.id
|
166
|
+
|
167
|
+
User.count.should == 1
|
168
|
+
Photo.count.should == 2
|
169
|
+
user.destroy
|
170
|
+
Photo.count.should == 1
|
171
|
+
User.count.should == 0
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should delete link to associated record when record was deleted" do
|
175
|
+
@album.photos << [@photo1, @photo2]
|
176
|
+
|
177
|
+
@album.photos.count.should == 2
|
178
|
+
|
179
|
+
Photo.count.should == 2
|
180
|
+
@photo1.destroy
|
181
|
+
Photo.count.should == 1
|
182
|
+
|
183
|
+
@album.photos.count.should == 1
|
184
|
+
@album.photos.size.should == 1
|
185
|
+
end
|
186
|
+
end
|