amistad 0.7.5 → 0.9.0

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 (41) hide show
  1. data/.gitignore +4 -1
  2. data/Gemfile.lock +56 -39
  3. data/README.markdown +29 -126
  4. data/Rakefile +35 -1
  5. data/amistad.gemspec +8 -3
  6. data/lib/amistad.rb +14 -9
  7. data/lib/amistad/active_record_friend_model.rb +157 -0
  8. data/lib/amistad/active_record_friendship_model.rb +52 -0
  9. data/lib/amistad/config.rb +21 -0
  10. data/lib/amistad/friend_model.rb +10 -4
  11. data/lib/amistad/friendship_model.rb +2 -2
  12. data/lib/amistad/friendships.rb +19 -0
  13. data/lib/amistad/mongo_friend_model.rb +168 -0
  14. data/lib/amistad/mongo_mapper_friend_model.rb +58 -0
  15. data/lib/amistad/mongoid_friend_model.rb +58 -0
  16. data/lib/amistad/version.rb +1 -1
  17. data/lib/generators/amistad/install/install_generator.rb +0 -1
  18. data/lib/generators/amistad/install/templates/create_friendships.rb +2 -2
  19. data/spec/activerecord/activerecord_spec_helper.rb +11 -24
  20. data/spec/activerecord/friend_custom_model_spec.rb +17 -0
  21. data/spec/activerecord/friend_spec.rb +16 -0
  22. data/spec/activerecord/friendship_spec.rb +13 -0
  23. data/spec/activerecord/friendship_with_custom_friend_model_spec.rb +18 -0
  24. data/spec/mongo_mapper/friend_custom_model_spec.rb +27 -0
  25. data/spec/mongo_mapper/friend_spec.rb +17 -0
  26. data/spec/mongo_mapper/mongo_mapper_spec_helper.rb +9 -0
  27. data/spec/mongoid/friend_custom_model_spec.rb +27 -0
  28. data/spec/mongoid/friend_spec.rb +17 -0
  29. data/spec/mongoid/mongoid_spec_helper.rb +8 -7
  30. data/spec/spec_helper.rb +33 -4
  31. data/spec/{activerecord/activerecord_friendship_model_spec.rb → support/activerecord/friendship_examples.rb} +11 -23
  32. data/spec/support/activerecord/schema.rb +40 -0
  33. data/spec/support/friend_examples.rb +2 -12
  34. data/spec/support/parameterized_models.rb +19 -0
  35. metadata +177 -36
  36. data/lib/amistad/active_record/friend_model.rb +0 -146
  37. data/lib/amistad/active_record/friendship_model.rb +0 -50
  38. data/lib/amistad/mongoid/friend_model.rb +0 -195
  39. data/lib/generators/amistad/install/templates/friendship.rb +0 -3
  40. data/spec/activerecord/activerecord_friend_model_spec.rb +0 -13
  41. data/spec/mongoid/mongoid_friend_model_spec.rb +0 -47
@@ -1,33 +1,20 @@
1
1
  require 'spec_helper'
2
2
  require 'active_record'
3
3
 
4
- ActiveRecord::Base.establish_connection(
5
- :adapter => "sqlite3",
6
- :database => ":memory:"
7
- )
4
+ Dir[File.expand_path('../../support/activerecord/*.rb', __FILE__)].each{|f| require f }
8
5
 
9
- ActiveRecord::Migration.verbose = false
10
-
11
- ActiveRecord::Schema.define do
12
- create_table :users, :force => true do |t|
13
- t.string :name, :null => false
6
+ RSpec.configure do |config|
7
+ config.before(:suite) do
8
+ CreateSchema.suppress_messages{ CreateSchema.migrate(:up) }
9
+ DatabaseCleaner.strategy = :transaction
10
+ DatabaseCleaner.clean_with(:truncation)
14
11
  end
15
12
 
16
- create_table :friendships, :force => true do |t|
17
- t.integer :user_id
18
- t.integer :friend_id
19
- t.integer :blocker_id
20
- t.boolean :pending, :default => true
13
+ config.before(:each) do
14
+ DatabaseCleaner.start
21
15
  end
22
16
 
23
- add_index :friendships, [:user_id, :friend_id], :unique => true
24
- end
25
-
26
- class User < ActiveRecord::Base
27
- include Amistad::FriendModel
28
- end
29
-
30
- class Friendship < ActiveRecord::Base
31
- include Amistad::FriendshipModel
17
+ config.after(:each) do
18
+ DatabaseCleaner.clean
19
+ end
32
20
  end
33
-
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + "/activerecord_spec_helper"
2
+
3
+ describe 'Custom friend model' do
4
+ before(:all) do
5
+ reload_environment
6
+
7
+ Amistad.configure do |config|
8
+ config.friend_model = 'Profile'
9
+ end
10
+
11
+ Profile = Class.new(ActiveRecord::Base)
12
+ end
13
+
14
+ it_should_behave_like "friend with parameterized models" do
15
+ let(:friend_model_param) { Profile }
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + "/activerecord_spec_helper"
2
+
3
+ describe "The friend model" do
4
+ before(:all) do
5
+ reload_environment
6
+ User = Class.new(ActiveRecord::Base)
7
+
8
+ ActiveSupport.on_load(:active_record) do
9
+ attr_accessible(nil)
10
+ end
11
+ end
12
+
13
+ it_should_behave_like "friend with parameterized models" do
14
+ let(:friend_model_param) { User }
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + "/activerecord_spec_helper"
2
+
3
+ describe 'Amistad friendship model' do
4
+
5
+ before(:all) do
6
+ reload_environment
7
+ User = Class.new(ActiveRecord::Base)
8
+ activate_amistad(User)
9
+ create_users(User)
10
+ end
11
+
12
+ it_should_behave_like "the friendship model"
13
+ end
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + "/activerecord_spec_helper"
2
+
3
+ describe 'Amistad friendship model whith custom friend model' do
4
+
5
+ before(:all) do
6
+ reload_environment
7
+
8
+ Amistad.configure do |config|
9
+ config.friend_model = 'Profile'
10
+ end
11
+
12
+ Profile = Class.new(ActiveRecord::Base)
13
+ activate_amistad(Profile)
14
+ create_users(Profile)
15
+ end
16
+
17
+ it_should_behave_like "the friendship model"
18
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/mongo_mapper_spec_helper'
2
+
3
+ describe 'Custom friend model' do
4
+ def reset_friendships
5
+ %w(john jane david james peter mary victoria elisabeth).each do |var|
6
+ eval "@#{var}.delete_all_friendships.should be_true"
7
+ end
8
+ end
9
+
10
+ before(:all) do
11
+ reload_environment
12
+
13
+ Amistad.configure do |config|
14
+ config.friend_model = 'Profile'
15
+ end
16
+
17
+ Profile = Class.new
18
+ Profile.class_exec do
19
+ include MongoMapper::Document
20
+ key :name, String
21
+ end
22
+ end
23
+
24
+ it_should_behave_like "friend with parameterized models" do
25
+ let(:friend_model_param) { Profile }
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/mongo_mapper_spec_helper'
2
+
3
+ describe "The friend model" do
4
+ before(:all) do
5
+ reload_environment
6
+
7
+ User = Class.new
8
+ User.class_exec do
9
+ include MongoMapper::Document
10
+ key :name, String
11
+ end
12
+ end
13
+
14
+ it_should_behave_like "friend with parameterized models" do
15
+ let(:friend_model_param) { User }
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+ require 'mongo_mapper'
3
+
4
+ MongoMapper.database = 'amistad_mongo_mapper_test'
5
+
6
+ RSpec.configure do |config|
7
+ config.before(:each) do
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/mongoid_spec_helper'
2
+
3
+ describe 'Custom friend model' do
4
+ def reset_friendships
5
+ %w(john jane david james peter mary victoria elisabeth).each do |var|
6
+ eval "@#{var}.delete_all_friendships.should be_true"
7
+ end
8
+ end
9
+
10
+ before(:all) do
11
+ reload_environment
12
+
13
+ Amistad.configure do |config|
14
+ config.friend_model = 'Profile'
15
+ end
16
+
17
+ Profile = Class.new
18
+ Profile.class_exec do
19
+ include Mongoid::Document
20
+ field :name
21
+ end
22
+ end
23
+
24
+ it_should_behave_like "friend with parameterized models" do
25
+ let(:friend_model_param) { Profile }
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/mongoid_spec_helper'
2
+
3
+ describe "The friend model" do
4
+ before(:all) do
5
+ reload_environment
6
+
7
+ User = Class.new
8
+ User.class_exec do
9
+ include Mongoid::Document
10
+ field :name
11
+ end
12
+ end
13
+
14
+ it_should_behave_like "friend with parameterized models" do
15
+ let(:friend_model_param) { User }
16
+ end
17
+ end
@@ -2,10 +2,11 @@ require "spec_helper"
2
2
  require 'mongoid'
3
3
 
4
4
  Mongoid.configure do |config|
5
- name = "amistad_test"
6
- host = "localhost"
7
- config.master = Mongo::Connection.new.db(name)
8
- config.slaves = [
9
- Mongo::Connection.new(host, 27017, :slave_ok => true).db(name)
10
- ]
11
- end
5
+ config.connect_to "amistad_mongoid_test"
6
+ end
7
+
8
+ RSpec.configure do |config|
9
+ config.before(:each) do
10
+ Mongoid.purge!
11
+ end
12
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,41 @@
1
1
  require 'rspec'
2
2
  require 'active_record'
3
3
  require 'mongoid'
4
- require 'amistad'
4
+ require 'ap'
5
+ require 'database_cleaner'
5
6
  Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|f| require f}
6
7
 
7
- def create_users
8
- User.delete_all
8
+ def create_users(friend_model)
9
+ friend_model.delete_all
9
10
  %w(John Jane David James Peter Mary Victoria Elisabeth).each do |name|
10
- instance_variable_set("@#{name.downcase}".to_sym, User.create(:name => name))
11
+ instance_variable_set("@#{name.downcase}".to_sym, friend_model.create{ |fm| fm.name = name})
11
12
  end
12
13
  end
14
+
15
+ def activate_amistad(friend_model)
16
+ friend_model.class_exec do
17
+ include Amistad::FriendModel
18
+ end
19
+ end
20
+
21
+ def reload_environment
22
+ # ensure that the gem will be always required
23
+ $".grep(/.*lib\/amistad.*/).each do |file|
24
+ $".delete(file)
25
+ end
26
+
27
+ # delete all the classes
28
+ if Object.const_defined?(:Amistad)
29
+ Amistad.constants.each do |constant|
30
+ Amistad.send(:remove_const, constant)
31
+ end
32
+
33
+ Object.send(:remove_const, :Amistad)
34
+ end
35
+
36
+ Object.send(:remove_const, :User) if Object.const_defined?(:User)
37
+ Object.send(:remove_const, :Profile) if Object.const_defined?(:Profile)
38
+
39
+ # require the gem
40
+ require "amistad"
41
+ end
@@ -1,26 +1,16 @@
1
- require File.dirname(__FILE__) + "/activerecord_spec_helper"
2
-
3
- describe Amistad::ActiveRecord::FriendshipModel do
4
-
5
- before(:all) do
6
- %w(Jane David).each do |name|
7
- instance_variable_set("@#{name.downcase}".to_sym, User.create(:name => name))
8
- end
9
- end
10
-
1
+ shared_examples_for "the friendship model" do
11
2
  it "should validate presence of the user's id and the friend's id" do
12
- friendship = Friendship.new
3
+ friendship = Amistad.friendship_class.new
13
4
  friendship.valid?.should be_false
14
- friendship.errors.should include(:user_id)
5
+ friendship.errors.should include(:friendable_id)
15
6
  friendship.errors.should include(:friend_id)
16
7
  friendship.errors.size.should == 2
17
8
  end
18
9
 
19
10
  context "when creating friendship" do
20
11
  before do
21
- Friendship.delete_all
22
12
  @jane.invite(@david)
23
- @friendship = Friendship.first
13
+ @friendship = Amistad.friendship_class.first
24
14
  end
25
15
 
26
16
  it "should be pending" do
@@ -41,10 +31,10 @@ describe Amistad::ActiveRecord::FriendshipModel do
41
31
 
42
32
  it "should be available to block only by invited user" do
43
33
  @friendship.can_block?(@david).should be_true
44
- @friendship.can_block?(@sane).should be_false
34
+ @friendship.can_block?(@jane).should be_false
45
35
  end
46
36
 
47
- it "should not be availabel to unblock" do
37
+ it "should not be available to unblock" do
48
38
  @friendship.can_unblock?(@jane).should be_false
49
39
  @friendship.can_unblock?(@david).should be_false
50
40
  end
@@ -52,10 +42,9 @@ describe Amistad::ActiveRecord::FriendshipModel do
52
42
 
53
43
  context "when approving friendship" do
54
44
  before do
55
- Friendship.delete_all
56
45
  @jane.invite(@david)
57
46
  @david.approve(@jane)
58
- @friendship = Friendship.first
47
+ @friendship = Amistad.friendship_class.first
59
48
  end
60
49
 
61
50
  it "should be approved" do
@@ -75,7 +64,7 @@ describe Amistad::ActiveRecord::FriendshipModel do
75
64
  end
76
65
 
77
66
  it "should be available to block by both users" do
78
- @friendship.can_block?(@sane).should be_true
67
+ @friendship.can_block?(@jane).should be_true
79
68
  @friendship.can_block?(@david).should be_true
80
69
  end
81
70
 
@@ -87,10 +76,9 @@ describe Amistad::ActiveRecord::FriendshipModel do
87
76
 
88
77
  context "when blocking friendship" do
89
78
  before do
90
- Friendship.delete_all
91
79
  @jane.invite(@david)
92
80
  @david.block(@jane)
93
- @friendship = Friendship.first
81
+ @friendship = Amistad.friendship_class.first
94
82
  end
95
83
 
96
84
  it "should not be approved" do
@@ -110,7 +98,7 @@ describe Amistad::ActiveRecord::FriendshipModel do
110
98
  end
111
99
 
112
100
  it "should not be available to block" do
113
- @friendship.can_block?(@sane).should be_false
101
+ @friendship.can_block?(@jane).should be_false
114
102
  @friendship.can_block?(@david).should be_false
115
103
  end
116
104
 
@@ -119,4 +107,4 @@ describe Amistad::ActiveRecord::FriendshipModel do
119
107
  @friendship.can_unblock?(@jane).should be_false
120
108
  end
121
109
  end
122
- end
110
+ end
@@ -0,0 +1,40 @@
1
+ def db_config(type)
2
+ YAML.load_file(File.expand_path('../database.yml', __FILE__))[type]
3
+ end
4
+
5
+ def connect_server(type)
6
+ config = db_config type
7
+ database = config['database']
8
+
9
+ ActiveRecord::Base.establish_connection config.merge('database' => (type == 'postgresql' ? 'postgres' : nil))
10
+ ActiveRecord::Base.connection.recreate_database(database)
11
+ ActiveRecord::Base.establish_connection(config)
12
+ end
13
+
14
+ case ENV['RDBM']
15
+ when 'mysql' then connect_server 'mysql'
16
+ when 'postgresql' then connect_server 'postgresql'
17
+ else
18
+ ActiveRecord::Base.establish_connection db_config('sqlite')
19
+ end
20
+
21
+ class CreateSchema < ActiveRecord::Migration
22
+ def self.up
23
+ create_table :users, :force => true do |t|
24
+ t.string :name, :null => false
25
+ end
26
+
27
+ create_table :profiles, :force => true do |t|
28
+ t.string :name, :null => false
29
+ end
30
+
31
+ create_table :friendships, :force => true do |t|
32
+ t.integer :friendable_id
33
+ t.integer :friend_id
34
+ t.integer :blocker_id
35
+ t.boolean :pending, :default => true
36
+ end
37
+
38
+ add_index :friendships, [:friendable_id, :friend_id], :unique => true
39
+ end
40
+ end
@@ -1,9 +1,5 @@
1
1
  shared_examples_for "a friend model" do
2
2
  context "when creating friendships" do
3
- before(:each) do
4
- reset_friendships
5
- end
6
-
7
3
  it "should invite other users to friends" do
8
4
  @john.invite(@jane).should be_true
9
5
  @victoria.invite(@john).should be_true
@@ -54,7 +50,6 @@ shared_examples_for "a friend model" do
54
50
 
55
51
  context "when listing friendships" do
56
52
  before(:each) do
57
- reset_friendships
58
53
  @john.invite(@jane).should be_true
59
54
  @peter.invite(@john).should be_true
60
55
  @john.invite(@james).should be_true
@@ -152,7 +147,6 @@ shared_examples_for "a friend model" do
152
147
 
153
148
  context "when removing friendships" do
154
149
  before(:each) do
155
- reset_friendships
156
150
  @jane.invite(@james).should be_true
157
151
  @james.approve(@jane).should be_true
158
152
  @james.invite(@victoria).should be_true
@@ -226,7 +220,6 @@ shared_examples_for "a friend model" do
226
220
 
227
221
  context "when blocking friendships" do
228
222
  before(:each) do
229
- reset_friendships
230
223
  @john.invite(@james).should be_true
231
224
  @james.approve(@john).should be_true
232
225
  @james.block(@john).should be_true
@@ -316,7 +309,6 @@ shared_examples_for "a friend model" do
316
309
 
317
310
  context "when unblocking friendships" do
318
311
  before(:each) do
319
- reset_friendships
320
312
  @john.invite(@james).should be_true
321
313
  @james.approve(@john).should be_true
322
314
  @john.block(@james).should be_true
@@ -379,18 +371,16 @@ shared_examples_for "a friend model" do
379
371
  end
380
372
 
381
373
  it "should list unblocked users in pending invited by" do
382
- @david.reload
383
374
  @david.pending_invited_by.should == [@victoria]
384
375
  end
385
376
  end
386
377
 
387
378
  context "when counting friendships and blocks" do
388
379
  before do
389
- reset_friendships
390
380
  @john.invite(@james).should be_true
391
381
  @james.approve(@john).should be_true
392
382
  @john.invite(@victoria).should be_true
393
- @victoria.approve(@john).should be_true
383
+ @victoria.approve(@john).should be_true
394
384
  @elisabeth.invite(@john).should be_true
395
385
  @john.approve(@elisabeth).should be_true
396
386
 
@@ -412,4 +402,4 @@ shared_examples_for "a friend model" do
412
402
  @victoria.total_blocked.should == 1
413
403
  end
414
404
  end
415
- end
405
+ end