redis_orm 0.7 → 0.8

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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +1 -1
  3. data/lib/redis_orm.rb +8 -13
  4. data/lib/redis_orm/associations/has_many.rb +7 -3
  5. data/lib/redis_orm/redis_orm.rb +3 -5
  6. metadata +64 -124
  7. data/Gemfile +0 -10
  8. data/Manifest +0 -74
  9. data/Rakefile +0 -25
  10. data/benchmarks/sortable_benchmark.rb +0 -45
  11. data/redis_orm.gemspec +0 -45
  12. data/spec/generators/model_generator_spec.rb +0 -29
  13. data/spec/spec_helper.rb +0 -17
  14. data/test/association_indices_test.rb +0 -168
  15. data/test/associations_test.rb +0 -294
  16. data/test/atomicity_test.rb +0 -36
  17. data/test/basic_functionality_test.rb +0 -204
  18. data/test/callbacks_test.rb +0 -49
  19. data/test/changes_array_test.rb +0 -25
  20. data/test/classes/album.rb +0 -6
  21. data/test/classes/article.rb +0 -7
  22. data/test/classes/article_with_comments.rb +0 -8
  23. data/test/classes/book.rb +0 -6
  24. data/test/classes/catalog_item.rb +0 -5
  25. data/test/classes/category.rb +0 -7
  26. data/test/classes/city.rb +0 -7
  27. data/test/classes/comment.rb +0 -26
  28. data/test/classes/country.rb +0 -5
  29. data/test/classes/custom_user.rb +0 -8
  30. data/test/classes/cutout.rb +0 -20
  31. data/test/classes/cutout_aggregator.rb +0 -5
  32. data/test/classes/default_user.rb +0 -10
  33. data/test/classes/dynamic_finder_user.rb +0 -8
  34. data/test/classes/empty_person.rb +0 -2
  35. data/test/classes/expire_user.rb +0 -8
  36. data/test/classes/expire_user_with_predicate.rb +0 -13
  37. data/test/classes/giftcard.rb +0 -6
  38. data/test/classes/jigsaw.rb +0 -4
  39. data/test/classes/location.rb +0 -5
  40. data/test/classes/message.rb +0 -4
  41. data/test/classes/note.rb +0 -5
  42. data/test/classes/omni_user.rb +0 -8
  43. data/test/classes/person.rb +0 -6
  44. data/test/classes/photo.rb +0 -21
  45. data/test/classes/profile.rb +0 -9
  46. data/test/classes/sortable_user.rb +0 -11
  47. data/test/classes/timestamp.rb +0 -3
  48. data/test/classes/user.rb +0 -39
  49. data/test/classes/uuid_default_user.rb +0 -12
  50. data/test/classes/uuid_timestamp.rb +0 -5
  51. data/test/classes/uuid_user.rb +0 -13
  52. data/test/dynamic_finders_test.rb +0 -51
  53. data/test/exceptions_test.rb +0 -47
  54. data/test/expire_records_test.rb +0 -64
  55. data/test/has_one_has_many_test.rb +0 -42
  56. data/test/indices_test.rb +0 -63
  57. data/test/modules/belongs_to_model_within_module.rb +0 -6
  58. data/test/modules/has_many_model_within_module.rb +0 -11
  59. data/test/options_test.rb +0 -226
  60. data/test/polymorphic_test.rb +0 -65
  61. data/test/redis.conf +0 -417
  62. data/test/sortable_test.rb +0 -116
  63. data/test/test_helper.rb +0 -37
  64. data/test/uuid_as_id_test.rb +0 -178
  65. data/test/validations_test.rb +0 -20
data/Rakefile DELETED
@@ -1,25 +0,0 @@
1
- #require 'rubygems'
2
- require 'rake'
3
- require 'rake/testtask'
4
-
5
- #=begin
6
- require 'echoe'
7
-
8
- Echoe.new('redis_orm', '0.7') do |p|
9
- p.description = "ORM for Redis (advanced key-value storage) with ActiveRecord API"
10
- p.url = "https://github.com/german/redis_orm"
11
- p.author = "Dmitrii Samoilov"
12
- p.email = "germaninthetown@gmail.com"
13
- p.dependencies = ["activesupport >=3.0.0", "activemodel >=3.0.0", "redis >=2.2.0", "uuid >=2.3.2"]
14
- p.development_dependencies = ["rspec >=2.5.0"]
15
- end
16
- #=end
17
-
18
- task :default => :test
19
-
20
- desc 'Test the redis_orm functionality'
21
- Rake::TestTask.new(:test) do |t|
22
- t.libs << 'lib'
23
- t.test_files = FileList['test/**/*_test.rb', 'spec/**/*_spec.rb']
24
- t.verbose = true
25
- end
@@ -1,45 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../lib/redis_orm')
2
- require 'benchmark'
3
-
4
- class User < RedisOrm::Base
5
- property :name, String
6
- property :age, Integer
7
- property :wage, Float
8
-
9
- index :name
10
- index :age
11
- end
12
-
13
- class SortableUser < RedisOrm::Base
14
- property :name, String, :sortable => true
15
- property :age, Integer, :sortable => true
16
- property :wage, Float, :sortable => true
17
-
18
- index :name
19
- index :age
20
- end
21
-
22
- path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/../test/redis.conf"
23
- $redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
24
- sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
25
- path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
26
- begin
27
- $redis = Redis.new(:host => 'localhost', :path => path_to_socket)
28
- rescue => e
29
- puts 'Unable to create connection to the redis server: ' + e.message.inspect
30
- Process.kill 9, $redis_pid.to_i if $redis_pid
31
- end
32
-
33
- n = 100
34
- Benchmark.bmbm do |x|
35
- x.report("creating regular user:") { for i in 1..n; u = User.create(:name => "user#{i}", :age => i, :wage => 100*i); end}
36
- x.report("creating user w/ sortable attrs:") { for i in 1..n; u = SortableUser.create(:name => "user#{i}", :age => i, :wage => 100*i); end }
37
- end
38
-
39
- Benchmark.bmbm do |x|
40
- x.report("finding regular users:") { User.find(:all, :limit => 5, :offset => 10) }
41
- x.report("finding users w/ sortable attrs:") { SortableUser.find(:all, :limit => 5, :offset => 10, :order => [:name, :asc]) }
42
- end
43
-
44
- $redis.flushall if $redis
45
- Process.kill 9, $redis_pid.to_i if $redis_pid
data/redis_orm.gemspec DELETED
@@ -1,45 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- Gem::Specification.new do |s|
4
- s.name = "redis_orm"
5
- s.version = "0.7"
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Dmitrii Samoilov"]
9
- s.date = "2013-05-03"
10
- s.description = "ORM for Redis (advanced key-value storage) with ActiveRecord API"
11
- s.email = "germaninthetown@gmail.com"
12
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "TODO", "lib/rails/generators/redis_orm/model/model_generator.rb", "lib/rails/generators/redis_orm/model/templates/model.rb.erb", "lib/redis_orm.rb", "lib/redis_orm/active_model_behavior.rb", "lib/redis_orm/associations/belongs_to.rb", "lib/redis_orm/associations/has_many.rb", "lib/redis_orm/associations/has_many_helper.rb", "lib/redis_orm/associations/has_many_proxy.rb", "lib/redis_orm/associations/has_one.rb", "lib/redis_orm/redis_orm.rb", "lib/redis_orm/utils.rb"]
13
- s.files = ["CHANGELOG", "Gemfile", "LICENSE", "Manifest", "README.md", "Rakefile", "TODO", "benchmarks/sortable_benchmark.rb", "lib/rails/generators/redis_orm/model/model_generator.rb", "lib/rails/generators/redis_orm/model/templates/model.rb.erb", "lib/redis_orm.rb", "lib/redis_orm/active_model_behavior.rb", "lib/redis_orm/associations/belongs_to.rb", "lib/redis_orm/associations/has_many.rb", "lib/redis_orm/associations/has_many_helper.rb", "lib/redis_orm/associations/has_many_proxy.rb", "lib/redis_orm/associations/has_one.rb", "lib/redis_orm/redis_orm.rb", "lib/redis_orm/utils.rb", "redis_orm.gemspec", "spec/generators/model_generator_spec.rb", "spec/spec_helper.rb", "test/association_indices_test.rb", "test/associations_test.rb", "test/atomicity_test.rb", "test/basic_functionality_test.rb", "test/callbacks_test.rb", "test/changes_array_test.rb", "test/classes/album.rb", "test/classes/article.rb", "test/classes/article_with_comments.rb", "test/classes/book.rb", "test/classes/catalog_item.rb", "test/classes/category.rb", "test/classes/city.rb", "test/classes/comment.rb", "test/classes/country.rb", "test/classes/custom_user.rb", "test/classes/cutout.rb", "test/classes/cutout_aggregator.rb", "test/classes/default_user.rb", "test/classes/dynamic_finder_user.rb", "test/classes/empty_person.rb", "test/classes/expire_user.rb", "test/classes/expire_user_with_predicate.rb", "test/classes/giftcard.rb", "test/classes/jigsaw.rb", "test/classes/location.rb", "test/classes/message.rb", "test/classes/note.rb", "test/classes/omni_user.rb", "test/classes/person.rb", "test/classes/photo.rb", "test/classes/profile.rb", "test/classes/sortable_user.rb", "test/classes/timestamp.rb", "test/classes/user.rb", "test/classes/uuid_default_user.rb", "test/classes/uuid_timestamp.rb", "test/classes/uuid_user.rb", "test/dynamic_finders_test.rb", "test/exceptions_test.rb", "test/expire_records_test.rb", "test/has_one_has_many_test.rb", "test/indices_test.rb", "test/modules/belongs_to_model_within_module.rb", "test/modules/has_many_model_within_module.rb", "test/options_test.rb", "test/polymorphic_test.rb", "test/redis.conf", "test/sortable_test.rb", "test/test_helper.rb", "test/uuid_as_id_test.rb", "test/validations_test.rb"]
14
- s.homepage = "https://github.com/german/redis_orm"
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Redis_orm", "--main", "README.md"]
16
- s.require_paths = ["lib"]
17
- s.rubyforge_project = "redis_orm"
18
- s.rubygems_version = "1.8.25"
19
- s.summary = "ORM for Redis (advanced key-value storage) with ActiveRecord API"
20
- s.test_files = ["test/association_indices_test.rb", "test/associations_test.rb", "test/atomicity_test.rb", "test/basic_functionality_test.rb", "test/callbacks_test.rb", "test/changes_array_test.rb", "test/dynamic_finders_test.rb", "test/exceptions_test.rb", "test/expire_records_test.rb", "test/has_one_has_many_test.rb", "test/indices_test.rb", "test/options_test.rb", "test/polymorphic_test.rb", "test/sortable_test.rb", "test/test_helper.rb", "test/uuid_as_id_test.rb", "test/validations_test.rb"]
21
-
22
- if s.respond_to? :specification_version then
23
- s.specification_version = 3
24
-
25
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
- s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
27
- s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
28
- s.add_runtime_dependency(%q<redis>, [">= 2.2.0"])
29
- s.add_runtime_dependency(%q<uuid>, [">= 2.3.2"])
30
- s.add_development_dependency(%q<rspec>, [">= 2.5.0"])
31
- else
32
- s.add_dependency(%q<activesupport>, [">= 3.0.0"])
33
- s.add_dependency(%q<activemodel>, [">= 3.0.0"])
34
- s.add_dependency(%q<redis>, [">= 2.2.0"])
35
- s.add_dependency(%q<uuid>, [">= 2.3.2"])
36
- s.add_dependency(%q<rspec>, [">= 2.5.0"])
37
- end
38
- else
39
- s.add_dependency(%q<activesupport>, [">= 3.0.0"])
40
- s.add_dependency(%q<activemodel>, [">= 3.0.0"])
41
- s.add_dependency(%q<redis>, [">= 2.2.0"])
42
- s.add_dependency(%q<uuid>, [">= 2.3.2"])
43
- s.add_dependency(%q<rspec>, [">= 2.5.0"])
44
- end
45
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'rails/generators/redis_orm/model/model_generator'
4
-
5
- describe RedisOrm::Generators::ModelGenerator do
6
- destination File.expand_path(File.join(File.dirname(__FILE__),
7
- '..', '..', 'tmp'))
8
-
9
- before do
10
- prepare_destination
11
- run_generator args
12
- end
13
- subject { file('app/models/post.rb') }
14
-
15
- context "Given only model's name" do
16
- let(:args) { %w[post] }
17
-
18
- it { should exist }
19
- end
20
- context "Given model's name and attributes" do
21
- let(:args) { %w[post title:string created_at:time] }
22
-
23
- it { should exist }
24
- it "should define properties" do
25
- should contain /property\s+\:title,\sString/
26
- end
27
- end
28
-
29
- end
data/spec/spec_helper.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'rails/all'
2
- require 'rspec'
3
- require 'rspec/autorun'
4
-
5
- $: << File.dirname(File.expand_path(__FILE__)) + '/../lib/'
6
-
7
- module RedisOrmRails
8
- class Application < ::Rails::Application
9
- end
10
- end
11
-
12
- require 'rspec/rails'
13
- require 'ammeter/init'
14
-
15
- RSpec.configure do |config|
16
- config.mock_with :rspec
17
- end
@@ -1,168 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- describe "check indices for associations" do
4
- before(:each) do
5
- @article = Article.new :title => "DHH drops OpenID on 37signals"
6
- @article.save
7
-
8
- @article.should be
9
- @article.title.should == "DHH drops OpenID on 37signals"
10
-
11
- @comment1 = Comment.new :body => "test"
12
- @comment1.save
13
- @comment1.should be
14
- @comment1.body.should == "test"
15
- @comment1.moderated.should == false
16
-
17
- @comment2 = Comment.new :body => "test #2", :moderated => true
18
- @comment2.save
19
- @comment2.should be
20
- @comment2.body.should == "test #2"
21
- @comment2.moderated.should == true
22
- end
23
-
24
- it "should properly find associated records (e.g. with :conditions, :order, etc options) '<<' used for association" do
25
- @article.comments << [@comment1, @comment2]
26
- @article.comments.count.should == 2
27
-
28
- @article.comments.all(:limit => 1).size.should == 1
29
- @article.comments.find(:first).should be
30
- @article.comments.find(:first).id.should == @comment1.id
31
- @article.comments.find(:last).should be
32
- @article.comments.find(:last).id.should == @comment2.id
33
-
34
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 1
35
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 1
36
- @article.comments.find(:all, :conditions => {:moderated => true})[0].id.should == @comment2.id
37
- @article.comments.find(:all, :conditions => {:moderated => false})[0].id.should == @comment1.id
38
-
39
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1).size.should == 1
40
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1).size.should == 1
41
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1)[0].id.should == @comment2.id
42
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1)[0].id.should == @comment1.id
43
-
44
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1, :order => :desc).size.should == 1
45
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1, :order => :asc).size.should == 1
46
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1, :order => :desc)[0].id.should == @comment2.id
47
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1, :order => :asc)[0].id.should == @comment1.id
48
-
49
- @comment1.update_attribute :moderated, true
50
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 2
51
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 0
52
-
53
- @comment1.destroy
54
- $redis.zrange("article:#{@article.id}:comments:moderated:true", 0, -1).size.should == 1
55
- $redis.zrange("article:#{@article.id}:comments:moderated:true", 0, -1)[0].should == @comment2.id.to_s
56
- $redis.zrange("article:#{@article.id}:comments:moderated:false", 0, -1).size.should == 0
57
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 1
58
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 0
59
- end
60
-
61
- it "should properly find associated records (e.g. with :conditions, :order, etc options) '=' used for association" do
62
- @article.comments = [@comment1, @comment2]
63
- @article.comments.count.should == 2
64
-
65
- @article.comments.all(:limit => 1).size.should == 1
66
- @article.comments.find(:first).should be
67
- @article.comments.find(:first).id.should == @comment1.id
68
- @article.comments.find(:last).should be
69
- @article.comments.find(:last).id.should == @comment2.id
70
-
71
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 1
72
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 1
73
- @article.comments.find(:all, :conditions => {:moderated => true})[0].id.should == @comment2.id
74
- @article.comments.find(:all, :conditions => {:moderated => false})[0].id.should == @comment1.id
75
-
76
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1).size.should == 1
77
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1).size.should == 1
78
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1)[0].id.should == @comment2.id
79
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1)[0].id.should == @comment1.id
80
-
81
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1, :order => :desc).size.should == 1
82
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1, :order => :asc).size.should == 1
83
- @article.comments.find(:all, :conditions => {:moderated => true}, :limit => 1, :order => :desc)[0].id.should == @comment2.id
84
- @article.comments.find(:all, :conditions => {:moderated => false}, :limit => 1, :order => :asc)[0].id.should == @comment1.id
85
-
86
- @comment1.update_attribute :moderated, true
87
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 2
88
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 0
89
-
90
- @comment1.destroy
91
- @article.comments.find(:all, :conditions => {:moderated => true}).size.should == 1
92
- @article.comments.find(:all, :conditions => {:moderated => false}).size.should == 0
93
- $redis.zrange("article:#{@article.id}:comments:moderated:true", 0, -1).size.should == 1
94
- $redis.zrange("article:#{@article.id}:comments:moderated:true", 0, -1)[0].should == @comment2.id.to_s
95
- $redis.zrange("article:#{@article.id}:comments:moderated:false", 0, -1).size.should == 0
96
- end
97
-
98
- it "should check compound indices for associations" do
99
- friend1 = User.create :name => "Director", :moderator => true, :moderated_area => "films"
100
- friend2 = User.create :name => "Admin", :moderator => true, :moderated_area => "all"
101
- friend3 = User.create :name => "Gena", :moderator => false
102
-
103
- me = User.create :name => "german"
104
-
105
- me.friends << [friend1, friend2, friend3]
106
-
107
- me.friends.count.should == 3
108
- me.friends.find(:all, :conditions => {:moderator => true}).size.should == 2
109
- me.friends.find(:all, :conditions => {:moderator => false}).size.should == 1
110
-
111
- me.friends.find(:all, :conditions => {:moderator => true, :moderated_area => "films"}).size.should == 1
112
- me.friends.find(:all, :conditions => {:moderator => true, :moderated_area => "films"})[0].id.should == friend1.id
113
-
114
- # reverse key's order in :conditions hash
115
- me.friends.find(:all, :conditions => {:moderated_area => "all", :moderator => true}).size.should == 1
116
- me.friends.find(:all, :conditions => {:moderated_area => "all", :moderator => true})[0].id.should == friend2.id
117
- end
118
-
119
- # TODO check that index assoc shouldn't be created while no assoc_record is provided
120
-
121
- it "should return first model if it exists, when conditions contain associated object" do
122
- user = User.create :name => "Dmitrii Samoilov", :age => 99, :wage => 35_000, :first_name => "Dmitrii", :last_name => "Samoilov"
123
- note = Note.create :body => "a test to test"
124
- note2 = Note.create :body => "aero"
125
-
126
- note.owner = user
127
-
128
- User.count.should == 1
129
- Note.count.should == 2
130
- $redis.zcard("note:owner:1").should == 1
131
- note.owner.should == user
132
- Note.find(:all, :conditions => {:owner => user}).should == [note]
133
- Note.find(:first, :conditions => {:owner => user}).should == note
134
-
135
- note.owner = nil
136
- Note.find(:all, :conditions => {:owner => user}).should == []
137
- Note.find(:first, :conditions => {:owner => user}).should == nil
138
- $redis.zcard("note:owner:1").should == 0
139
- end
140
-
141
- it "should return first model if it exists when conditions contain associated object (belongs_to assoc established when creating object)" do
142
- user = User.create :name => "Dmitrii Samoilov", :age => 99, :wage => 35_000, :first_name => "Dmitrii", :last_name => "Samoilov"
143
- note = Note.create :body => "a test to test", :owner => user
144
- Note.create :body => "aero" # just test what would *find* return if 2 exemplars of Note are created
145
-
146
- User.count.should == 1
147
- Note.count.should == 2
148
-
149
- note.owner.should == user
150
-
151
- Note.find(:all, :conditions => {:owner => user}).should == [note]
152
- Note.find(:first, :conditions => {:owner => user}).should == note
153
- end
154
-
155
- it "should return first model if it exists when conditions contain associated object (has_one assoc established when creating object)" do
156
- profile = Profile.create :title => "a test to test", :name => "german"
157
- user = User.create :name => "Dmitrii Samoilov", :age => 99, :wage => 35_000, :first_name => "Dmitrii", :last_name => "Samoilov", :profile => profile
158
- User.create :name => "Warren Buffet", :age => 399, :wage => 12_235_000, :first_name => "Warren", :last_name => "Buffet"
159
-
160
- User.count.should == 2
161
- Profile.count.should == 1
162
-
163
- profile.user.should == user
164
-
165
- User.find(:all, :conditions => {:profile => profile}).should == [user]
166
- User.find(:first, :conditions => {:profile => profile}).should == user
167
- end
168
- end
@@ -1,294 +0,0 @@
1
- require File.dirname(File.expand_path(__FILE__)) + '/test_helper.rb'
2
-
3
- describe "check associations" do
4
- before(:each) do
5
- @article = Article.new :title => "DHH drops OpenID on 37signals"
6
- @article.save
7
-
8
- @article.should be
9
- @article.title.should == "DHH drops OpenID on 37signals"
10
-
11
- @comment1 = Comment.new :body => "test"
12
- @comment1.save
13
- @comment1.should be
14
- @comment1.body.should == "test"
15
-
16
- @comment2 = Comment.new :body => "test #2"
17
- @comment2.save
18
- @comment2.should be
19
- @comment2.body.should == "test #2"
20
- end
21
-
22
- it "should assign properly from belongs_to side" do
23
- @comment1.article.should == nil
24
- @comment1.article = @article
25
- @comment1.article.id.should == @article.id
26
- @article.comments.count.should == 1
27
- @article.comments[0].id.should == @comment1.id
28
-
29
- @comment2.article.should == nil
30
- @comment2.article = @article
31
- @comment2.article.id.should == @article.id
32
- @article.comments.count.should == 2
33
- @article.comments[0].id.should == @comment2.id
34
- end
35
-
36
- it "should correctly resets associations when nil/[] provided" do
37
- # from has_many proxy side
38
- @article.comments << [@comment1, @comment2]
39
- @article.comments.count.should == 2
40
- @comment1.article.id.should == @article.id
41
- @comment2.article.id.should == @article.id
42
-
43
- # clear
44
- @article.comments = []
45
- @article.comments.count.should == 0
46
- @comment1.article.should == nil
47
- @comment2.article.should == nil
48
-
49
- # from belongs_to side
50
- @article.comments << [@comment1, @comment2]
51
- @article.comments.count.should == 2
52
- @comment1.article.id.should == @article.id
53
-
54
- # clear
55
- @comment1.article = nil
56
- @article.comments.count.should == 1
57
- @comment1.article.should == nil
58
-
59
- # from has_one side
60
- profile = Profile.create :title => "test"
61
- chicago = City.create :name => "Chicago"
62
-
63
- profile.city = chicago
64
- profile.city.name.should == "Chicago"
65
- chicago.profiles.count.should == 1
66
- chicago.profiles[0].id.should == profile.id
67
-
68
- # clear
69
- profile.city = nil
70
- profile.city.should == nil
71
- chicago.profiles.count.should == 0
72
- end
73
-
74
- it "should return array of records for has_many association" do
75
- @article.comments << []
76
- @article.comments.count.should == 0
77
-
78
- @article.comments = []
79
- @article.comments.count.should == 0
80
-
81
- @article.comments << [@comment1, @comment2]
82
- #@article.comments.should be_kind_of(Array)
83
-
84
- @article.comments.count.should == 2
85
- @article.comments.size.should == 2
86
-
87
- @comment1.article.should be
88
- @comment2.article.should be
89
-
90
- @comment1.article.id.should == @comment2.article.id
91
- end
92
-
93
- it "should behave as active_record (proxy couldn't return records w/o #all call) += and << behave differently" do
94
- @article.comments << @comment1 << @comment2
95
- @article.comments.count.should == 2
96
-
97
- comments = @article.comments
98
- comments.count.should == 2
99
-
100
- comments = []
101
- comments += @article.comments
102
- comments.count.should == 2
103
- comments.collect{|c| c.id}.should include(@comment1.id)
104
- comments.collect{|c| c.id}.should include(@comment2.id)
105
-
106
- comments = []
107
- comments << @article.comments.all
108
- comments.flatten.count.should == 2
109
-
110
- comments = []
111
- comments << @article.comments
112
- comments.count.should == 1
113
- end
114
-
115
- it "should return 1 comment when second was deleted" do
116
- Comment.count.should == 2
117
- @article.comments << [@comment1, @comment2]
118
- #@article.comments.should be_kind_of(Array)
119
- @article.comments.size.should == 2
120
-
121
- @comment1.destroy
122
-
123
- @article.comments.size.should == 1
124
- @article.comments.count.should == 1
125
- Comment.count.should == 1
126
- end
127
-
128
- it "should leave associations when parent has been deleted (nullify assocs)" do
129
- Comment.count.should == 2
130
- @article.comments << [@comment1, @comment2]
131
- @comment1.article.id.should == @article.id
132
- @comment2.article.id.should == @article.id
133
- #@article.comments.should be_kind_of(Array)
134
- @article.comments.size.should == 2
135
- @article.comments.count.should == 2
136
-
137
- @article.destroy
138
-
139
- Article.count.should == 0
140
- Comment.count.should == 2
141
- end
142
-
143
- it "should replace associations when '=' is used instead of '<<' " do
144
- Comment.count.should == 2
145
- @article.comments << [@comment1, @comment2]
146
- @comment1.article.id.should == @article.id
147
- @comment2.article.id.should == @article.id
148
- @article.comments.size.should == 2
149
- @article.comments.count.should == 2
150
-
151
- @article.comments = [@comment1]
152
- @article.comments.count.should == 1
153
- @article.comments.first.id.should == @comment1.id
154
-
155
- @comment1.article.id.should == @article.id
156
- end
157
-
158
- it "should correctly use many-to-many associations both with '=' and '<<' " do
159
- @cat1 = Category.create :name => "Nature"
160
- @cat2 = Category.create :name => "Art"
161
- @cat3 = Category.create :name => "Web"
162
-
163
- @cat1.name.should == "Nature"
164
- @cat2.name.should == "Art"
165
- @cat3.name.should == "Web"
166
-
167
- @article.categories << [@cat1, @cat2]
168
-
169
- @cat1.articles.count.should == 1
170
- @cat1.articles[0].should == @article
171
- @cat2.articles.count.should == 1
172
- @cat2.articles[0].should == @article
173
-
174
- @article.categories.size.should == 2
175
- @article.categories.count.should == 2
176
-
177
- @article.categories = [@cat1, @cat3]
178
- @article.categories.count.should == 2
179
- @article.categories.map{|c| c.id}.include?(@cat1.id).should be
180
- @article.categories.map{|c| c.id}.include?(@cat3.id).should be
181
-
182
- @cat1.articles.count.should == 1
183
- @cat1.articles[0].should == @article
184
-
185
- @cat3.articles.count.should == 1
186
- @cat3.articles[0].should == @article
187
-
188
- @cat2.articles.count.should == 0
189
-
190
- @cat1.destroy
191
- Category.count.should == 2
192
- @article.categories.count.should == 1
193
- end
194
-
195
- it "should remove old associations and create new ones" do
196
- profile = Profile.new
197
- profile.title = "test"
198
- profile.save
199
-
200
- chicago = City.new
201
- chicago.name = "Chicago"
202
- chicago.save
203
-
204
- washington = City.new
205
- washington.name = "Washington"
206
- washington.save
207
-
208
- profile.city = chicago
209
- profile.city.name.should == "Chicago"
210
- chicago.profiles.count.should == 1
211
- washington.profiles.count.should == 0
212
- chicago.profiles[0].id.should == profile.id
213
-
214
- profile.city = washington
215
- profile.city.name.should == "Washington"
216
- chicago.profiles.count.should == 0
217
- washington.profiles.count.should == 1
218
- washington.profiles[0].id.should == profile.id
219
- end
220
-
221
- it "should maintain correct self referencing link" do
222
- me = User.create :name => "german"
223
- friend1 = User.create :name => "friend1"
224
- friend2 = User.create :name => "friend2"
225
-
226
- me.friends << [friend1, friend2]
227
-
228
- me.friends.count.should == 2
229
- friend1.friends.count.should == 0
230
- friend2.friends.count.should == 0
231
- end
232
-
233
- it "should delete one specific record from an array with associated records" do
234
- me = User.create :name => "german"
235
- friend1 = User.create :name => "friend1"
236
- friend2 = User.create :name => "friend2"
237
-
238
- me.friends << [friend1, friend2]
239
-
240
- me = User.find_by_name 'german'
241
- me.friends.count.should == 2
242
- friend1 = User.find_by_name 'friend1'
243
- friend1.friends.count.should == 0
244
- friend2 = User.find_by_name 'friend2'
245
- friend2.friends.count.should == 0
246
-
247
- me.friends.delete(friend1.id)
248
- me.friends.count.should == 1
249
- me.friends[0].id == friend2.id
250
- User.count.should == 3
251
- end
252
-
253
- it "should create self-referencing link for has_one association" do
254
- m = Message.create :text => "it should create self-referencing link for has_one association"
255
-
256
- r = Message.create :text => "replay"
257
-
258
- r.replay_to = m
259
-
260
- Message.count.should == 2
261
- r.replay_to.should be
262
- r.replay_to.id.should == m.id
263
-
264
- rf = Message.last
265
- rf.replay_to.should be
266
- rf.replay_to.id.should == Message.first.id
267
- end
268
-
269
- it "should find associations within modules" do
270
- BelongsToModelWithinModule::Reply.count.should == 0
271
- essay = Article.create :title => "Red is cluster"
272
- BelongsToModelWithinModule::Reply.create :essay => essay
273
- BelongsToModelWithinModule::Reply.count.should == 1
274
- reply = BelongsToModelWithinModule::Reply.last
275
- reply.essay.should == essay
276
-
277
- HasManyModelWithinModule::SpecialComment.count.should == 0
278
- book = HasManyModelWithinModule::Brochure.create :title => "Red is unstable"
279
- HasManyModelWithinModule::SpecialComment.create :book => book
280
- HasManyModelWithinModule::Brochure.count.should == 1
281
- HasManyModelWithinModule::SpecialComment.count.should == 1
282
- end
283
-
284
- it "should properly handle self-referencing model both belongs_to and has_many/has_one associations" do
285
- comment1 = Comment.create :body => "comment1"
286
- comment11 = Comment.create :body => "comment1.1"
287
- comment12 = Comment.create :body => "comment1.2"
288
-
289
- comment1.replies = [comment11, comment12]
290
- comment1.replies.count.should == 2
291
- comment11.reply_to.should == comment1
292
- comment12.reply_to.should == comment1
293
- end
294
- end